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.
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! !! !!
* If you have not installed it yet, please visit the official website above. </ font>
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
Create a container with PHP and Apache installed on CentOS7.
① Create a Dockerfile in any location (file name: dockerfile) 2 Create an image from the created Dockerfile. ③ Create a container based on the image.
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"]
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.
#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.
When you access [http: // localhost: 8080](http: // localhost: 8080), the following screen will be displayed.
#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.
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
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.
① 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!
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
docker-compose run web rails new . --force --database=postgresql
docker-compose build
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'] %>
docker-compose up -d
docker-compose run web rake db:create
If you access [http: // localhost: 3000 /](http: // localhost: 3000 /) and the following page is displayed, it is successful.
You can see that the container is also started properly.
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
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 |
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.
date | Contents |
---|---|
2020/11/18 | First edition |
Recommended Posts