As the title suggests, Docker will be installed in the existing Rails and MySQL application development environment. It also allows you to run rspec tests within Docker. There are already a lot of similar articles, but I've encountered quite a few errors, so I'll summarize how I worked. I would appreciate it if you could let me know if there are any mistakes.
If Rails version is 6 and MySQL version is 8, this method will not work.
Create a new Dockerfile in the existing project directory.
file organization
test_app#Existing project
├── Abbreviation
├── Dockerfile #Create
├── docker-compose.yml #Create
├── Gemfile
├── Gemfile.lock
├── .env #use
├── config
└──database.yml #Change
I will describe the contents.
Please match test_app
below to each app name.
Dockerfile
FROM ruby:2.6.5
RUN apt-get update \
&& apt-get install -y --no-install-recommends nodejs mariadb-client build-essential \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /test_app
COPY Gemfile /test_app/Gemfile
COPY Gemfile.lock /test_app/Gemfile.lock
RUN gem install bundler
RUN bundle install
COPY . /test_app
--First, get the image of ruby with FROM. The version suits your environment.
--Next, you are installing the required packages. There is an article that mariadb-client
is mysql-client
, but note that it resulted in an error. mysql-client seems to be integrated with mariadb-client.
--Next, specify the working directory. Even if there is no RUN mdir
, it will be created with WORKDIR
.
--The following is copying gemfile etc. from local to the container. There is an article that COPY
is ʻADD, but it seems that
COPY` is recommended.
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: today_code_development
MYSQL_ROOT_PASSWORD: ${DATABASE_DEV_PASSWORD}
MYSQL_USER: ${DATABASE_DEV_USER}
MYSQL_PASSWORD: ${DATABASE_DEV_PASSWORD}
volumes:
- mysql-data:/var/lib/mysql
test-db:
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
environment:
MYSQL_DATABASE: today_code_test
MYSQL_ROOT_PASSWORD: ${DATABASE_DEV_PASSWORD}
MYSQL_USER: ${DATABASE_DEV_USER}
MYSQL_PASSWORD: ${DATABASE_DEV_PASSWORD}
ports:
- '3307:3306'
web:
build:
context: .
dockerfile: Dockerfile
command: bundle exec rails s -p 3000 -b '0.0.0.0'
tty: true
stdin_open: true
depends_on:
- db
- test-db
ports:
- "3000:3000"
volumes:
- .:/today_code
- bundle:/usr/local/bundle
volumes:
mysql-data:
bundle:
Write only the points.
--The command
line sets the character code of database to utf8. Without this, an error occurred at db: create
.
--In ʻenviroment, the user name and password are set. Of course, it works even if you write it directly, but here we are expanding the environment variables. The docker-compose file seems to read the .env file by default, so define it in the .env file as shown below. The defined environment variables can be expanded with
$ {}`. Add the .env file to the .gitignore file so that it doesn't commit.
--The data is persisted by creating named volumes with volumes. There seems to be a way to mount the directory, but the data volume seems to have more advantages.
.env
DATABASE_DEV_USER = 'hoge'
DATABASE_DEV_PASSWORD = 'password'
database.yml
default: &default
adapter: mysql2
encoding: utf8
charset: utf8
pool: 5
port: 3306
development:
<<: *default
database: test_app_development
username: <%= ENV['DATABASE_DEV_USER'] %>
password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
host : db
test:
<<: *default
database: test_app_test
username: <%= ENV['DATABASE_DEV_USER'] %>
password: <%= ENV['DATABASE_DEV_PASSWORD'] %>
host : test-db
production:
<<: *default
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOSTNAME'] %>
--username and password are the same as docker-compose. --It is set to connect to the db container created by host. ――Please adjust the production to each environment --The dotenv-rails gem is used to manage environment variables.
If you can start docker at the end, it is successful.
docker-compose build
docker-compose up -d
. -d is an option to start in the background.docker-compose run web rails db: create
If it is displayed at http: // localhost: 3000 /, it is successful. All you have to do is db: migrate or db: seed and you're done.
I tried to build an existing Rails project development environment using Docker (Rails + Mysql) How to run an existing Rails application with a Docker container + DB container visualization with sequence pro Build a test DB in Rails x Docker environment Persist Docker data! Introduction to environment construction starting from understanding Data Volume
Recommended Posts