Let's finally use Docker: Terms, Images, Launch

I was able to install it last time, so I want to move it ... Finally using Docker: Installation

By the way, the flow of how to use it I will write an article to explain the terms that appear and add an image

Operating environment

MacOS Docker version 19.03.12

Flow of using

-Install Docker on the host PC (your local PC) (Docker for Mac) ・ ** Create a Docker image ** (game software) → Get from docker hub -Create a ** container ** (** build, build ) using Docker Image ( image ) -Create a Docker Container ( container ) based on Docker Image and enable the virtual environment ( run, run **)

A container is like a Linux virtual machine

To put it simply ** Image **… Class (game software) ** Container **… Instance (save data)

If you don't have the image you want ...

You can also create it ** Write a Dockerfile ** → A file of to describe the configuration information of the container that runs on Docker. Create ** Docker Image ** based on Dockerfile (** build, build **)

This time, from writing the Dockerfile!

Create a Dockerfile

It seems that it is OK to edit with a touch Dockerfile and an editor Edit with ** vim **

pwd
/Users/username/environment/rails
~/environment/rails

❯ ls
~/environment/rails

❯ touch Dockerfile
~/environment/rails

❯ ls
Dockerfile
~/environment/rails

❯ vi Dockerfile

environment/rails/Dockerfile


//Ruby repository 2.4.Point to the image of the 5 tag
FROM ruby:2.4.5
//Means a command to execute in a container
//Installation of required packages
// build-I have the essential and nodejs packages installed
RUN apt-get update -qq && apt-get install -y build-essential nodejs
//The app directory is created in the root directory. Rails app is created here
//Create and set working directory
RUN mkdir /app
//Point to working directory
WORKDIR /app
//Files on the host PC/Copying into the app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
//All the contents of the app/Copying into the app
COPY . /app

→ The above contents are ** saved as a container **

Create Gmefile and Gmefile.lock

You can also create this with the touch command in the same way.

pwd
/Users/username/environment/rails
~/environment/rails

❯ ls
~/environment/rails

❯ touch Gemfile
~/environment/rails

❯ touch Gemfile.lock
~/environment/rails

❯ ls
Dockerfile Gemfile Gemfile.lock
~/environment/rails

❯ vi Gemfile

Edit Gemfile

environment/rails/Gemfile


source 'https://rubygems.org'
gem 'rails', '5.0.0.1'

Next is Gemfile.lock, but you can leave the contents empty

environment/rails/Gemfile.lock



Usually after running the "bundle install" command instead of editing directly Because Gem is installed and ** automatically ** listed in Gemfile.lock **

Create and edit docker-compose.yml

".Yml" is a format often used in Rails

docker_compose.yml


// docker_compose.Represents the yml version
version: '3'
services:
  //Rails container
  web:
    // docker-compose.Files in the same directory as yml
    //Means to create and use an original image
    build: .
    //Set the command to be executed by default when the container is executed
    // -You have set the IP address to bind with b
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    //The directory on the PC is the container "/Mounted in the "app" directory
    //Docker on pc-compose.The folder where yml is located is on the container
    // 「/Shared in the "app" directory
    //In other words, the code modified on the PC is reflected in the container as well.
    volumes:
      - .:/app
    //Set to expose the port number outside the container
    //If you do not set it, you will not be able to communicate from your PC to Rails running in the container.
    // <Port number to be published outside the container>:<Forwarding port number inside the container>
    ports:
      - 3000:3000
    //Set the MySQL server to start first before Rails starts
    depends_on:
      - db
    //"Tty" and "stdin"_"open" is a setting required when using "pry" with Rails
    tty: true
    stdin_open: true
  //MySQL server container
  db:
    // app 5.Use version 7
    image: mysql:5.7
    // 「db-The area created on the PC with the name "volume"
    //Container ":/var/lib/Mounted on "mysql"
    //If not set, the data will be saved on the container
    //Note that the data will be lost if you delete the container
    volumes:
      - db-volume:/var/lib/mysql
    //Defines environment variables to be set in the container

    //Set the password used by the root user
    environment:
      MYSQL_ROOT_PASSWORD: password
volumes:

→ "web" and "db" are here ** service name **

Create a Rails app and start it

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

" Docker compose run web "... A container for web services, behind ** Command to execute command ** " Rails new. --force --database = mysql "... with" rails new . " ** Create a file ** on "/ app" which is the working directory ** on the container **

" --Force "option… ** Used to overwrite existing fills ** Used to overwrite even if there is a Gem file etc.

** build **

docker-compose build

After build is complete, edit the DB configuration file used by Rails

Edit database.yml

vim config/database.yml

Specify the minimum password and host

config/database.yml


# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  // MYSQL_ROOT_PASSWORD environment variable`password`
  password: password
  //It is a container of MYSQL server`db`
  host: db

Edit to

Start Docker

//"Docker" in the current directory-compose.Based on "yml"
//Command to start the container
docker-compose up -d

Rails server and MySQL server commands start It is running, but the DB for the development environment has not been created yet. Create next

Check if the container is running

docker-compose ps

Create a DB

docker-compose run web bundle exec rake db:create

Almost the same as before The commands after docker-compose run web bundle exec Commands executed inside the container

Rails rake task command, if DB does not exist Will create

Make sure the Rails app is running

Promised Access localhost: 3000 with a browser and confirm that the server has started.

If the screen appears safely, it's OK!

Reference articles, videos

https://www.udemy.com/course/rails-kj https://qiita.com/gold-kou/items/44860fbda1a34a001fc1 https://qiita.com/azul915/items/5b7063cbc80192343fc0

Recommended Posts

Let's finally use Docker: Terms, Images, Launch
Let's finally use Docker: Start, stop, delete development environment
Docker terms and commands
Launch MariaDB with Docker
Why we use Docker
Use Puphpeteer with Docker
Use ngrok with Docker
Use images in Rails
[Swift] Let's use extension
Delete all Docker images