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.
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.
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
terminal
$ docker exec -it <db container name> bash
From inside the container
terminal
$ mysql -u root -p
terminal
$ docker-compose stop
#Dockerfile or docker-compose.after yml modification
$ docker-compose up -d --build
For when modifying Dockerfile or docker-compose.yml.
Even with the --rm option, containers and images that you do not need to develop will accumulate. Here is the command to delete them.
terminal
docker system prune
terminal
docker container prune
terminal
docker image prune
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"]
From rails 6.0, the JavaScript compiler has been changed to webpacker, and yarn, the package manager required to install webpacker, is installed.
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
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 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
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.
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.
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.
docker-compose.yml
depends_on:
- db
Control the order in which containers are started with depends_on (in this case, db → web)
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.
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
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 ^^
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