[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio

I would like to summarize the commands and environment construction that I needed to create a portfolio with Rails. It is necessary at the portfolio creation level.

Overview

  1. Frequently used commands for handling docker-compose
  2. Description of various files (Dockerfile, docker-compose.yml, database.yml)

1. Frequently used commands for handling docker-compose

rails command (rails g model user, rails db: migrate, etc.)

terminal


$ docker compose run --rm web <rails command>

The --rm option is a command to delete the container after execution. If you do not attach this, an unnecessary container will be created and it will put pressure on the memory, so be sure to attach it.

Connect to standard I / O (what is displayed on the terminal when developing locally)

terminal


$ docker attach <web server container name>

You can check the communication status by connecting to the standard input / output on the server. This command is also used when debugging with binding.pry. Detach with proper processing. (If you ctrl + C, the container will drop)

Ctrl + P, Ctrl + Q

Connect to database (mysql)

terminal


$ docker exec -it <db container name> bash

From inside the container

terminal


$ mysql -u root -p

Create a Docker image, start and run the container.

terminal


$ docker-compose stop
#Dockerfile or docker-compose.after yml modification
$ docker-compose up -d --build

For when modifying Dockerfile or docker-compose.yml.

Delete command

Even with the --rm option, containers and images that you do not need to develop will accumulate. Here is the command to delete them.

Bulk delete dead containers, untagged images, unused volumes, unused networks

terminal


docker system prune

Bulk deletion of stopped containers

terminal


docker container prune

Delete unused images in bulk

terminal


docker image prune

Description of various files

Dockerfile First, the Dockerfile. First, read the official Document. https://docs.docker.com/compose/rails/

Dockerfile


FROM ruby:2.7.1

#Debian installation
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get update -qq && apt-get install -y nodejs yarn imagemagick mariadb-client vim

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

Yarn installation

From rails 6.0, the JavaScript compiler has been changed to webpacker, and yarn, the package manager required to install webpacker, is installed.

Install ImageMagick

Installed because I wanted to use MiniMagic for image upload. If you want to use MiniMagick with your Rails app, you need to install ImageMagick in the environment where your Rails app is running. The following is from the minimagic ReadMe

ImageMagick or GraphicsMagick command-line tool has to be installed. You can check if you have it installed by running

Installation of mariadb-client

Install if you want to use rails db: console. There is an article that says mysql-client, but be careful because it is now unified to mariadb-client. It may not be necessary because you can access the database with the above command.

install vim

Install the editor because you will need it. After deploying the app to the production environment, I wanted to edit credentials.yml.enc with the following command, but I used vim at that time.

terminal


$ docker-compose run -e EDITOR=vim web rails credentials:edit

docker-compose.yml Next is docker-compose.yml

docker-compose.yml


version: "3"
services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - 4306:3306
    
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql

  web:
    build: .
    tty: true
    stdin_open: true
    #A server is already running when rails s.Error may appear. To prevent this, rm-f tmp/pids/server.It is pid.
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
      - bundle:/usr/local/bundle
    ports:
      - "3100:3000"
    depends_on:
      - db

volumes:
  bundle:
    driver: local
  mysql-data:
    driver: local

Port number setting

docker-compose.yml


db
  ports:
    - 4306:3306
web
  ports:
    - 3100:3000

The port number of the host machine is on the left and the port number of the container is on the right. If MySQL is (LISTEN) on the host or you want to use localhost: 3000, set the port numbers used by the machine to 4306 and 3100, respectively, because the ports used are duplicated. If you don't wear it, you can use 3307 or 10000.

Changed mysql authentication method

docker-compose.yml


command: --default-authentication-plugin=mysql_native_password

The default authentication method has changed from MySQL 8.0 (caching_sha2_password), but since the Rails app does not yet support it, change it back to mysql_native_password.

Enable standard input

docker-compose.yml


tty: true
stdin_open: true

Very important for debugging. If you do not do this, binding.pry etc. will not be possible. tty: true A setting that enables pseudo-terminals, which corresponds to the -t option of docker run. Even if it is enabled, it cannot be entered, so stdin_open: true below is always included as a set. stdin_open: true A setting that corresponds to the -i option of docker run and keeps the standard input connected. This makes it possible to execute commands.

Control container boot order

docker-compose.yml


depends_on:
      - db

Control the order in which containers are started with depends_on (in this case, db → web)

Creating a Data Volume container

docker-compose.yml


volumes:
  bundle:
    driver: local
  mysql-data:
    driver: local

Normally, if you recreate the container, all the data in the bundle installed files and database will be deleted, so you will have to build again or register the data. It's too annoying to use a Data Volume container. Data Volume container is a container that has only data and uses a mechanism to share between containers. The settings to create this with the names bundle and mysql-data are as above.

Mount to volume

docker-compose.yml


volumes:
  - mysql-data:/var/lib/mysql
volumes:
  - .:/myapp
  - bundle:/usr/local/bundle

Persist data using the Data Volume container created earlier. Now, even if you destroy the container and rebuild it, the data in the database will remain as it is, and Gem will be installed and ready to use.

database.yml Finally database.yml

database.yml


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USERNAME", "root") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD", "password") %>
  host: <%= ENV.fetch("MYSQL_HOST", "db") %>

development:
  <<: *default
  database: myapp_development

Character code

database.yml


encoding: utf8mb4

I wanted to register pictograms in the database, so the character code is utf8mb4 instead of utf8.

Please let me know if you have any other useful settings or commands ^^

reference

https://docs.docker.com/compose/rails/ Introduction to Docker / Kubernetes Practical Container Development https://qiita.com/neko-neko/items/abe912eba9c113fd527e https://qiita.com/chisaki0606/items/68e21d9a31f1eaaeac00 https://qiita.com/nsy_13/items/9fbc929f173984c30b5d

Recommended Posts

[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
I tried to create a padrino development environment with Docker
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
I tried migrating the portfolio created on Vagrant to the Docker development environment
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I tried to summarize the state transition of docker
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to summarize the basics of kotlin and java
I want to create a form to select the [Rails] category
I tried to create React.js × TypeScript × Material-UI on docker environment
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried to create a Spring MVC development environment on Mac
I tried to summarize the methods of Java String and StringBuilder
Install Rails in the development environment and create a new application
I tried to build a laravel operating environment while remembering Docker
I tried to summarize the methods used
I tried to summarize the Stream API
What is Docker? I tried to summarize
I tried to take a look at the flow of Android development environment construction with Android Studio
I built a rails environment with docker and mysql, but I got stuck
I tried to summarize the key points of gRPC design and development
I was a little addicted to running old Ruby environment and old Rails
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
I tried to build a Firebase application development environment with Docker in 2020
[Rails] How to create a table, add a column, and change the column type
[Rails] I tried to summarize the passion and functions of the beginners who created the share house search site!
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
A reminder of Docker and development environment construction
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Rails Docker environment construction
I tried to decorate the simple calendar a little
I tried to create a Clova skill in Java
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
I tried to create a shopping site administrator function / screen with Java and Spring
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
I tried to create a log reproduction script at the time of apt install
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Docker command to create Rails project with a single blow in environment without Ruby
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I tried node-jt400 (Environment construction)
I want to call a method and count the number
I tried JAX-RS and made a note of the procedure
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried the Docker tutorial!
A series of steps to create portfolio deliverables with Rails
I tried to summarize the basic grammar of Ruby briefly
[Docker] Rails 5.2 environment construction with docker
I tried to summarize personally useful apps and development tools (development tools)
Build a Node-RED environment with Docker to move and understand
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried to summarize personally useful apps and development tools (Apps)