Create Rails 6 + MySQL environment with Docker compose

The official Docker site has a Docker compose tutorial for Rails applications (, but it's a bit old and didn't work with Rails 6, so Rails 6 Here's how to get it working with. Since the basics follow the steps of the official tutorial, I've added supplements focusing on the changes made for Rails 6.

DB describes how to use MySQL instead of Postgres.

Preparing the project directory

mkdir myrailsapp
cd myrailsapp

Preparation of configuration file


FROM ruby:2.5

##nodejs and yarn are required when installing webpack
#Install yarn package management tool
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS | apt-key add - && \
echo "deb stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

RUN apt-get update -qq && apt-get install -y nodejs yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

RUN yarn install --check-files
RUN bundle exec rails webpacker:compile

# Add a script to be executed every time the container starts.
COPY /usr/bin/
RUN chmod +x /usr/bin/

# Start the main process.
CMD ["rails", "server", "-b", ""]

Rails 6 requires yarn to run Webpacker, so add the procedure for installing yarn to the Dockerfile. If you normally install with apt-get yarn with the image of ruby: 2.5, a strange version of 0.32 + git will be installed and an error will occur later, so I added the repository according to the instructions on the yarn official website. Later apt-get install yarn.


Also, in the Production environment, it is necessary to compile Webpacker in advance, so I added yarn package update and webpacker: compile.

Excluded because postgres is not used.


source ''
gem 'rails', '~>6'

Specify Rails 6 series in Gemfile. This Gemfile will be overwritten with the contents of the Rails project after the Rails project is created.

touch Gemfile.lock

set -e

# Remove a potentially pre-existing for Rails.
rm -f /myapp/tmp/pids/

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

This area is still an official sample, so explanation is omitted.


version: '3'
    image: mysql:5.7
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
      - "3306:3306"
      - ./tmp/db:/var/lib/mysql

    build: .
    command: bash -c "rm -f tmp/pids/ && bundle exec rails s -p 3000 -b ''"
      - .:/myapp
      - "3000:3000"
      - db

Change the setting to use MySQL.

Creating a Rails project

docker-compose run web rails new . --force --no-deps --database=mysql

Create a project with the DB setting set to MySQL with --database = mysql.

docker-compose build

DB connection settings


  <<: *default
  database: myapp_development
  host: db
  username: root
  password: password

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  <<: *default
  database: myapp_test
  host: db
  username: root
  password: password

Described the setting so that development and test use the MySQL image db started by docker-compose.

DB creation

docker-compose run web rails db:create

Introducing Webpacker

docker-compose run web rails webpacker:install 

This step is necessary because Rails 6 has replaced Sprockets with Webpacker.

Launch image

docker-compose up


docker-compose run web rails g scaffold article title:string body:text published_at:timestamp
docker-compose run web rails db:migrate

When developing using docker-compose, execute generate commands including scaffold and migration with docker-compose run web.

docker-compose up

スクリーンショット 2020-09-22 0.07.07.png

Recommended Posts

Create Rails 6 + MySQL environment with Docker compose
Rails + MySQL environment construction with Docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Create a MySQL environment with Docker from 0-> 1
Rails6 [API mode] + MySQL5.7 environment construction with Docker
[Docker] Rails 5.2 environment construction with docker
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build environment with vue.js + rails + docker
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Create Rails5 and postgresql environment with Docker and make pgadmin available
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Deploy to heroku with Docker (Rails 6, MySQL)
Edit Mysql with commands in Docker environment
Create Spring Boot-gradle-mysql development environment with Docker
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Docker] Create Node.js + express + webpack environment with Docker
Laravel + MySQL + phpMyadmin environment construction with Docker
How to build Rails 6 environment with Docker
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Rails Docker environment construction
Rails deploy with Docker
[Docker] Connection with MySQL
[Rails] Development with MySQL
[Memo] Create a CentOS 8 environment easily with Docker
Notes on building Rails6 / PostgreSQL with Docker Compose
React + Django + Nginx + MySQL environment construction with Docker
[Rails] How to build an environment with Docker
Create a Spring Boot development environment with docker
I built a rails environment with docker and mysql, but I got stuck
Pytorch execution environment with Docker
[Docker] Create Elasticsearch, Kibana environment!
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Rails6 (MySQL, Ubuntu environment, Cloud9)
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
MySQL 5.7 (Docker) environment construction memo
Create an E2E test environment with Docker x Cypress
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Run Rails whenever with docker
Build docker environment with WSL
[Note] Create a java environment from scratch with docker
[Rails / MySQL] Mac environment construction
React environment construction with Docker
[Docker] Use whenever with Docker + Rails
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Docker command to create Rails project with a single blow in environment without Ruby
I made a development environment with rails6 + docker + postgreSQL + Materialize.
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Easy environment construction of MySQL and Redis with Docker and Alfred
How to build Rails, Postgres, ElasticSearch development environment with Docker
I tried to create a padrino development environment with Docker
Create portfolio with rails + postgres sql