I've heard that building an environment is more difficult than writing code.
And Why,,, ** Just copy and paste ** However, it may take some time to understand ... Let's start with a brief overview
Docker Tools for building virtual environments
Virtual environment itself (Docker runs a container on top of the Docker engine)
What you need to run a Docker container.
① .``` Create a project (directory) and move it `` `
$ mkdir app name
$ cd app name
② . Create Dockerfile, docker-compose.yml, Gemfile, Gemfile.lock
$ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock
③. `Open the editor and copy and paste Dockerfile, docker-compose.yml, Gemfile from the following`
Dockerfile
A file that automatically generates a Docker image
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
FROM: Image and command to use RUN: Command execution WORKDIR: Working directory settings COPY: Specify the file or directory of the copy source (host side) and copy destination (virtual environment side)
Gemfile source 'https://rubygems.org' gem 'rails', '~> 5.2.3'
docker-compose.yml version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
volumes:
- ./db/mysql/volumes:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
- gem_data:/usr/local/bundle
ports:
- 3000:3000
depends_on:
- db
tty: true
stdin_open: true
volumes:
gem_data:
version: The version of docker-compose. depends_on: Shows dependencies and allows you to control the boot order. Here, start to "db → web".
Docker compose is a function that automates the procedure for building and executing a service consisting of multiple containers and facilitates management. With Docker compose, you can prepare a compose file and execute the command once to read the settings from that file and start all containers.
In Docker compose, each element to run the application is called service. Usually named web (rails) and db (mysql).
The pid is the process ID. The pid is written to tmp / pids / server.pid when you start the development web server and is deleted when you exit. If pid is written in server.pid, the server will be judged as ** starting **.
ports:
- 3000:3000
The above means that port 3000 in the container is mapped to 3000 on the host. This will allow you to access the web server inside the container at `` `http: // localhost: 3000```.
volumes:
- ./db/mysql/volumes:/var/lib/mysql
Means to mount ./db/mysql/volumes on the host to / var / lib / mysql inside the container. Simply put, the Docker container is ** synchronized ** locally.
volumes :
- .:/myapp
Means all directories on the host with `.``` and mounts it on
`myapp``` in the container.
④ .``` Execute the following command `` `
$ docker-compose run web rails new . --force --database=mysql
The run command will do everything from building an image to building and starting a container. Service must be specified in the argument. Since the volumes:-.:/Myapp part of docker-compose.yml is set to synchronize docker with the local directory, a similar file will be generated locally at the same time after executing this command. ..
⑤. ``` Change to (password: password, host: db) in config / database.yml`
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password Added #password host: db Changed from #localhost to db
⑥. `Execute the following command`
$ docker-compose build
Run `` `build``` when the Gemfile is updated
⑦ .``` Execute the following command `` `
$ docker-compose up -d
The container starts according to docker-compose.yml. Also executed when reflecting changes in docker-compose.yml. The `` `-d``` option starts in the background.
⑧. ``` Execute the following command
$ docker-compose run web rails db:create
Create a database. You can execute commands locally with
docker-compose run web
. (No need to enter the container)
⑨. ``` Execute the following command and check if the two containers are up. `` `(Completed!)
$ docker ps
A command to display the currently running container. When you access
localhost: 3000
, you will see the usual Yay! You're on Rails !.
`If you want to delete the container, do the following`
$ docker-compose down
Check if the container has been deleted with
`docker ps```. To create and start a container, execute
docker-compose up -d
``.
I think it was pretty easy! Let's develop the app with this! : thumbsup:
Recommended Posts