[GO] Solved the trap that Rails commands could not be used after using the development environment with Docker

I was thinking of making something in earnest, so I finished building the Rails 6 environment in the development environment. By the way, the article I referred to is -Steps for building a Ruby on Rails environment with Docker [Rails 6 compatible] --Qiita

For other parts that didn't work, ・ Rails6 + MySQL + Nginx + Unicorn + Docker environment construction memo --Qiita -Rails (Docker) can no longer connect to PostgreSQL (Docker), so I checked it. (Could not translate host name "db" to address: Name or service not known) --Qiita

I referred to the above article.

And this time, I stumbled a little at the beginning of the elementary course, so I will talk a little about it.

Let's make the top page of the website immediately!

I wanted to make a top page with rails,

Terminal


$ rails g controller home top

Enter, Then ...

Output result


Rails is not currently installed on this system. To get the latest version, simply type:

    $ sudo gem install rails

With the help of Google Translate ↓ __ Rails is not currently installed on this system. To get the latest version, enter $ sudo gem install rails. __

Hmmm, why is Rails 6 definitely installed? I was worried about 0.5 seconds, but the answer was simple.

Rails exists only within Docker

First of all, what is Docker?

Docker is open source software or an open platform for developing, deploying, and executing applications using container virtualization. Docker isolates applications from the development / execution environment by OS-level virtualization using container virtualization, enabling quick application delivery. Moreover, the environment itself can be managed as a code (image) in the same way as an application. By using Docker for development, testing, and deployment, the time gap between "writing code" and "code is executed as a product" can be greatly reduced. Quote: Docker-Wikipedia

As you can see from wikipedia, Docker is a virtualization technology. In other words, I installed Rails using docker-compose, but now I can use it within Docker! It's good to recognize about __.

So __Rails is not currently installed on this system. To get the latest version, enter $ sudo gem install rails. It became __!

The solution is the Docker command

Terminal


$ docker-compose run web

This is a magical spell.

After this, if you add the previous command ...

Terminal


$ docker-compose run web rails g contoller home top

Output result


Starting guminoki_db_1 ... done
Could not find generator 'contoller'. Maybe you meant "controller"?
Run `rails generate --help` for more options.

This seems to be a typo in the controller, so start over.

Terminal


$ docker-compose run web rails g contoroller home top

Output result


Starting guminoki_db_1 ... done
      create  app/controllers/home_controller.rb
       route  get 'home/top'
      invoke  erb
      create    app/views/home
      create    app/views/home/top.html.erb
      invoke  test_unit
      create    test/controllers/home_controller_test.rb
      invoke  helper
      create    app/helpers/home_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/home.scss

It's done! You did it!

at the end

Previously, I also used __ $ docker-compose run web__ when writing applications in Django. I completely forgot (sweat)

Django development server startup example


$ docker-compose run web python3 manage.py runserver

In addition

Terminal


$ docker-compose run --rm web rails g contoroller home top

If you add the --rm option like this, the container will be deleted automatically when the container is closed, which is very convenient.

Recommended Posts

Solved the trap that Rails commands could not be used after using the development environment with Docker
Install Docker on CentOS8. The problem that the name could not be resolved was resolved.
About the matter that localhost: 4040 cannot be accessed after running Spark with Docker
Commands often used in the development environment during Python implementation
Rebuild Django's development environment with Docker! !! !! !!
Prepare the development environment with anyenv
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2
Behind the flyer: Using Docker with Python
About the development environment you are using
I tried to summarize the operations that are likely to be used with numpy-stl
The story that 5GHz band access point could not be created on Ubuntu