This article is the procedure when I introduced Docker
to the local environment of the existing portfolio app.
Since the official document of Docker
is the procedure explanation in PostgreSQL
, there is no procedure explanation of the official document when it comes to MySQL
.
Also, the information on Rails6
is still less than that on Rails5
, and as a result, the information on the combination of Rails6
+ MySQL
was very little.
As a result, I got various errors and tried and errored myself, so I hope this article will help those who introduce Docker
with the combination of Rails6
+ MySQL
.
Since the author is a beginner who has no practical experience in job hunting, there may be some mistakes.
In that case, I would appreciate it if you could let me know in the comments.
Docker Desktop
for Mac` has been completed.Docker
is already startedDocker
.Create a Dockerfile
in the root directory of the existing application and write as follows.
** * This time, the directory name of the application is sample_app.
Replace this with the directory name of your app. ** **
Dockerfile
FROM ruby:2.6.5
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 build-essential libpq-dev nodejs yarn
WORKDIR /sample_app
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock
RUN gem install bundler
RUN bundle install
COPY . /sample_app
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"]
As in step 1, create docker-compose.yml
in the root directory of the existing application and write it as follows.
docker-compose.yml
version: '3' #docker-compose version
services:
db:
image: mysql:8.0.21 #Combine with existing apps. To the terminal[$ mysql --version]Confirm with
environment:
MYSQL_ROOT_PASSWORD: vxgbizakqc #Your password
MYSQL_DATABASE: root
ports:
- "4306:3306"
volumes:
- ./mysql-confd:/etc/mysql/conf.d
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/sample_app
ports:
- 3000:3000
depends_on:
- db
tty: true
stdin_open: true
volumes:
mysql-data:
As with steps 1 and 2, create entrypoint.sh
in the root directory of the existing application and write it as follows.
entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /sample_app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Edit database.yml
in the config
directory as follows.
config/database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: vxgbizakqc #Your password
socket: /tmp/mysql.sock
host: db
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
Execute the following command in the terminal to create a Docker image
.
Terminal
MacBook-Pro:sample_app$ docker-compose build
Execute the following command in the terminal to start the Docker container
.
Terminal
MacBook-Pro:sample_app$ docker-compose up -d
Execute the following command in the terminal to create the database in the container.
Terminal
MacBook-Pro:sample_app$ docker-compose run web rails db:create
When you execute the above command, a database will be created based on the database.yml
edited earlier.
If the database is created successfully, the following will be displayed.
Terminal
reating sample_app_web_run ... done
Created database 'sample_app_development'
Created database 'sample_app_test'
Execute the following command in the terminal to execute the database migration.
Terminal
MacBook-Pro:sample_app$ docker-compose run web rails db:migrate
If the database migration is successful, the following will be displayed.
Terminal
Creating sample_app_web_run ... done
== 20209165960112 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-> 0.0253s
#Omission
-- add_index(:notifications, :comment_id)
-> 0.0251s
== 20209165960112 CreateNotifications: migrated (0.1280s) =====================
Well, this is the last of the long introduction of Docker.
Execute the following command in the terminal to install Gem
.
Terminal
MacBook-Pro:sample_app$ docker-compose exec web bundle install
that's all! !!
You can see the application by connecting to localhost: 3000
.
This time, I was able to introduce Docker as a result of referring to the following articles and video teaching materials. Thank you to the people who wrote the article and Mr. Kojima, the creator of the video teaching materials.