In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.

Introduction

Docker is a "convenient tool that makes it easy to create a development environment."

Even if you want to play baseball in earnest, it's difficult to make a place ... Prepare a base, measure the distance, prepare a bat, a ball, and a seat for the audience. However! With Docker, you can instantly create a PayPay dome! (Not finished.)

Baseball stadiums are not possible, but they are possible in a system development environment.

--LAMP server

It is a convenient application that allows you to instantly create a system development environment and distribute that environment to multiple people.

About this article

This article does not specifically touch on the concept of containers, etc. I am creating an article for the following purposes.

――You can actually touch Docker and experience the feeling that the system environment is ready. ――You can pull it out immediately when you remember it later.

Nezko! !! !! !! !! !! Docker will do its best to use it! !! !!

Execution environment

* If you have not installed it yet, please visit the official website above. </ font>

The site that I used as a reference

In particular, I studied and referred to the top site. The concept is well described, so if you are a reader of this article, please take a look.

-[Series] The most understandable container in the world & Introduction to Docker ~ Part 1: What is a container? ~ -[Difference between RUN instruction and CMD instruction of Dockerfile [Docker]](https://rara-world.com/dockerfile-run-cmd/#:~:text=RUN%E5%91%BD%E4%BB% A4% E3% 83% BBCMD% E5% 91% BD% E4% BB% A4% E3% 81% AE,% E3% 81% A8% E3% 81% 8D% E3% 81% AB% E5% AE% 9F % E8% A1% 8C% E3% 81% 95% E3% 82% 8C% E3% 81% BE% E3% 81% 99% E3% 80% 82) -Docker Document Japaneseization Project -Introduction to Docker (6th) ~ Docker Compose ~ -docker-compose I didn't understand the difference between up, build and start, so I summarized it -I explained how to write docker-compose.yml

Let's do it!

Create a container from Dockerfile

Create a container with PHP and Apache installed on CentOS7.

flow

① Create a Dockerfile in any location (file name: dockerfile) 2 Create an image from the created Dockerfile. ③ Create a container based on the image.

procedure

① First, create a docker file on your PC.

dockerfile


#Based on the centos7 image from Docker Hub.
FROM centos:centos7

#Install Apache and php in centos7 image
RUN yum -y install httpd php

#Execute the command to start Åpach
CMD ["/usr/sbin/httpd","-DFOREGROUND"]

(2) Create an image based on the created Dockerfile.

See Docker Document Japaneseization Project for explanations of options.

#Contents
docker build -t image name:Tag name Dockerfile directory

#Example | The image name is lamptest. Tag is 1.0. Use current directory dockerfile
docker build -t lamptest:1.0 .

The above will create an image. Screen Shot 2020-11-17 at 11.47.03.png

③ Start the container based on the created image

#Contents
docker run -d -p port number:port number--name Container name Image name:Tag name

#Example | Assign port 80 version to 8080 version.
#The container name is testserver.
#Images and tags are lamptest:1.Use 0.
docker run -d -p 8080:80 --name testserver lamptest:1.0

The container is completed successfully. Screen Shot 2020-11-17 at 11.59.47.png

When you access [http: // localhost: 8080](http: // localhost: 8080), the following screen will be displayed.

Screen Shot 2020-11-17 at 15.29.00.png

* Supplement * Container operation


#Start-up
docker start container name

#Stop
docker stop container name

#Go inside the container
docker exec -i -t container name bash

The operation is also possible with GUI. Screen Shot 2020-11-17 at 15.19.03.png

Detailed description of the dockerfile command

The ones that are frequently used are briefly described. Please refer to Docker Document Japaneseization Project for more detailed explanation.

From Create based on Docker image.


#Contents
FROM image name:Tag name

#Example | Based on the image of centos
FROM centos:centos7

Run Execute the command for the image specified in FROM.

#Contents
FROM Command to execute

#Example | Installing httpd with yum
RUN yum -y install httpd

COPY Specify the file you want to copy in the Docker image (in the container).

#Contents
Path of the file you want to copy in the FROM image Path of the copy destination image

#Example | hoge file in the same directory as Dockerfile,/Copy to tmp directory.
COPY hoge.txt /tmp

ADD If you specify a file that can be compressed with a tar archive (gzip, bzip2, xz) that can specify a URL, it will be automatically expanded. Other than that, it is the same as the COPY command.

#Contents
The file path or URL you want to copy into the ADD image The image path of the copy destination.

#Example | tar.Image storage while expanding gz file
ADD hoge.tar.gz /tmp

#Example | Save the URL destination file in the specified directory.
ADD https://zukan.pokemon.co.jp/detail/025 /tmp

RUN Execute the command when creating the image.

#Contents
RUN command option

#Example | ls command-With the option of a.
RUN ls -a

CMD Execute the command when the container starts.

#Contents
CMD [command,option]

#Example | ls command-With the option of a.
CMD [ ls , -a ]

ENV Enter a value in the number of environment variables. (Equal can be omitted.)

#Contents
ENV environment variable name=Environment variable value
ENV environment variable name Environment variable value

#Example | Insert ichinokata into the environment variable MIZUNOKOKYU
ENV MIZUNOKOKYU=ichinokata

USER Switching user execution name

#Contents
USER username

#Example | Switch user name to akiunleash
USER akiunleash

Create and link multiple containers with docker-compose

It is possible to build multiple containers of application and database as one system. Let's create Rails and PostgreSQL containers separately and work together as one system.

flow

① Create necessary files such as docker-compose.yml ② Create images and containers using docker-compose run ③ Gemfile will be updated, so build it again with docker-compose bulde ④ Change the username and password settings so that you can connect to PostgreSQL from the Rails side. ⑤ Create PostgreSQL database from Rails ⑥ Completion!

procedure

Prepare the following four files.

  • docker-compose.yml
  • dockerfile
  • Gemfile
  • Gemfile.lock

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

dockerfile


#Based on the centos7 image from Docker Hub.
FROM ruby:2.3.3

#From the package management software update
# build-essential libpq-install dev nodejs
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

#Create myapp directory in the container
RUN mkdir /myapp

#Set in work directory
WORKDIR /myapp

#Create a file in the same directory as dockerfile in the container
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock

#Install rails written in Gemfile
RUN bundle install

#Create Rails files and directories in a container
ADD . /myapp

Gemfile


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

Gemfile.lock


#Empty file

Execute the command in the directory where the above file is saved

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

The Gemfile will be updated, so execute the command to install the Gem again.

docker-compose build

Edit the information to connect to the database.

config/database.yml



default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Let's start the app!

docker-compose up -d

Since there is no database in the current state, we will create it.

docker-compose run web rake db:create

Confirmation!

If you access [http: // localhost: 3000 /](http: // localhost: 3000 /) and the following page is displayed, it is successful. Screen Shot 2020-11-18 at 11.12.38.png

You can see that the container is also started properly. Screen Shot 2020-11-18 at 11.18.04.png

If it contains unnecessary containers, it will go down once and disappear when you restart.

#Once down
docker-compose down

#Side up
docker-compose up -d

Explanation of docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
item Explanation
version docker-compose version
services Container name (create WEB and DB)
image Use images from Docker Hub
environment Set environment variables
build Use the specified dockerfile.
command Command to execute after creating the container
volumes Mount the file
ports Specifying the port
depends_on Boot order

Finally

I hope it helps you get a feel for what Docker is all about. If you find any mistakes in the content, please let us know.

change history

date Contents
2020/11/18 First edition

Recommended Posts

In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[For those who create portfolios] How to use binding.pry with Docker
How to use Docker in VSCode DevContainer
How to find the total number of pages when paging in Java
Understand in 5 minutes !! How to use Docker
How to display the amount of disk used by Docker container for each container
How to check the logs in the Docker container
Output of how to use the slice method
How to use JQuery in js.erb of Rails6
How to use nginx-ingress-controller with Docker for Mac
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to use git with the power of jgit in an environment without git commands
`bind': Address already in use --bind (2) for 127.0.0.1:3000 (Errno :: EADDRINUSE) How to deal with the error
Summary of how to use the proxy set in IE when connecting with Java
In Time.strptime,% j (total date of the year) is
[For those who create portfolios] How to use font-awesome-rails
How to use CommandLineRunner in Spring Batch of Spring Boot
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
How to use the getter / setter method (in object orientation)
How to create a placeholder part to use in the IN clause
[For those who create portfolios] How to use chart kick
How to derive the last day of the month in Java
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
Use MailHog for checking emails in the development environment (using Docker)
[Sprint Boot] How to use 3 types of SqlParameterSource defined in org.springframework.jdbc.core.namedparam
How to output the value when there is an array in the array
How to get the contents of Map using for statement Memorandum
[Rails] How to change the page title of the browser for each page
How to get the id of PRIMAY KEY auto_incremented in MyBatis
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
I examined the concept of the process to understand how Docker works
How to check for the contents of a java fixed-length string
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to write the view when Vue is introduced in Rails?
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]