――In-house work often uses host-type virtualization, but I felt a sense of crisis that I had little understanding of container-type virtualization in the company, so I decided to try a study session to summarize the advantages and disadvantages. thought. ――I made it with the hope that you will be able to recognize its strengths and weaknesses by touching Docker, which is container-type virtualization, and give you an opportunity to compare it with the legacy methods so far. ――You may have pointed out that your company's way of working is old or bad, and that you are still doing that, but you may not even be aware of it, so you will be left out of the flow of the world. In order to prevent such criticisms and suggestions, we have deliberately prepared materials outside. ――Please point out if there are any mistakes. --Assumption that there are many jobs to create business Web applications with a configuration like Apache + PHP + Postgres (MySQL)
--People who have many opportunities to build a development environment ――People who were thrown to build infrastructure for the time being without information on what kind of application will be built from now on ――When preparing a server or infrastructure, you have to consider various things such as OS, CPU, memory, network bandwidth, etc., but poor people who have to work without getting the answer --People who have been addicted to the difference between the development environment and the production environment --People who are frustrated by the difference between the development environment and the production environment --People who want to focus on application development because infrastructure is troublesome --People who want to create applications but don't want or hate setting up machines --People who want to get their work done quickly --People who want to reuse software assets, etc. --People who are editing or copying files by deploying code ――People who want to be a little cooler without saying CI / CD ――People who are thinking about using Docker from now on
Virtualization is a technology that integrates multiple hardware with software and reproduces the hardware with free specifications, and uses a limited amount of physical resources (CPU, memory, hard disk, network, etc.) in actual quantity. It is to make it appear as if the above resources (logical resources) are running. What is virtualization? https://www.fsi.co.jp/solution/vmware/knowledge/virtualization.html
This can make your job easier, make better use of surplus computer resources, increase system availability, and much more.
--Host type --Hypervisor type --Container type
Taking three major virtualization methods as examples, we will summarize the advantages and disadvantages along with comparison with general servers.
Applications that run on an OS installed directly on the hardware
--Advantages --Simple and clear app works --Low overhead for running applications
A method of running a guest OS in a virtual environment that runs on an OS installed directly on the hardware
--Advantages --Easy to install even on servers that did not use virtualization
--Products and applications
A method of running virtual environment management software called a hypervisor on hardware
--Advantages --Since the host OS is not used, the hardware can be directly attached to the guest OS. --Low overhead when running applications on guest OS compared to host type --Hypervisors often have a function that makes it relatively easy to manage virtual environments.
--Products and applications
A container engine that runs as a process runs on the host OS and runs the container as an application. Virtualize a process instead of a virtual machine
--Advantages --Lightweight because you only need to prepare a virtual environment as a container --Since it does not go through the guest OS, it starts up quickly.
--Products and applications
There are various virtualization methods like this, but this time we will introduce how to use Docker, which uses container-type virtualization, to build a development environment for Web applications.
――The container engine part in the above figure corresponds to the docker service. --Container virtualization is a method of virtualizing as a process --Use a container template called "Docker image" --You can get it from the DockerHub repository and use it, or you can create your own image based on the existing image. --In that case, describe the image creation procedure using the Dockerfile. This makes it possible to make a recipe for the setup procedure.
By aiming for container virtualization using images and Docker files
--No matter who starts it, the result will be (generally) the same. --If you build the development environment and the production environment on the premise of Docker from the beginning, there will be little difference and the time and effort for release will be reduced. --You can start the container in the development environment when you need it. --Rather than having a large instance equipped with a development environment and waiting, if you store it in a repository and start and deploy it each time according to a predetermined procedure, the operating cost of the instance will be reduced.
The advantage will be born. Docker images are prepared at various levels from OS level to application level, and can be built according to the needs of users.
--If you want to use a web server: --Use only Apache images --Description the Apache installation / setup procedure in the Dockerfile using the OS image. --If you want to use PHP in addition to the web server: --Use Apache + PHP image --Description the Apache and PHP installation / setup procedure in the Dockerfile using the OS image.
--It is easy to build the exact same environment if it is assumed that both the development environment and the production environment will be built using Docker from the beginning. --If you make a recipe in Dockerfile, you can prevent the server settings from being missed when building an individual environment.
I will introduce the procedure to install Docker on the server (your server, VPS, etc.)
It is explained in detail below, so I will introduce it by excerpt.
Introduction to Docker (2nd) -Docker setup, container startup- https://knowledge.sakura.ad.jp/13795/
Assuming CentOS, you can install it with the following command.
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce
After starting, the status is confirmed and the service automatic start setting at the time of restart is also performed.
# systemctl start docker
# systemctl status docker
# systemctl enable docker
About the difference between Docker and Docker-CE https://qiita.com/s-suefusa/items/cb3c4044da3b3657dbd0
Get the image of the Web server and start the image.
Get an image of a web server called Nginx and confirm that the image has been added as follows.
# docker pull nginx
# docker images
Start the container using the acquired Nginx image. When starting, the host and the port on the container side are associated, and in the example below, the host side 8181 is associated with the container side 80.
# docker run -d --name nginx-container -p 80:80 nginx
Command: docker stop [CONTAINER ID]
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65ee0bf845dc nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8181->80/tcp nginx-container
# docker stop 65ee0bf845dc
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65ee0bf845dc nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) 7 seconds ago nginx-container
Command: docker rmi [CONTAINER ID]
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65ee0bf845dc nginx "/docker-entrypoint.…" 4 minutes ago Exited (0) About a minute ago nginx-container
# docker rm 65ee0bf845dc
65ee0bf845dc
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
The image used in the deleted container remains in the storage of the server itself, so try deleting it as well.
Command: docker rmi [IMAGE ID]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f35646e83998 12 days ago 133MB
# docker rmi f35646e83998
Untagged: nginx:latest
Untagged: nginx@sha256:ed7f815851b5299f616220a63edac69a4cc200e7f536a56e421988da82e44ed8
Deleted: sha256:f35646e83998b844c3f067e5a2cff84cdf0967627031aeda3042d78996b68d35
Deleted: sha256:9ae13393c37dce86ebd3ea923033503f2cb8f4d6b28fb554827c518a2d171535
Deleted: sha256:423bc419c558f70051d849a661a7a287b61af2037c4ce24f7bbe433e9fb63f39
Deleted: sha256:4cd04e685e3a8e5697bb91e2e6c6b477bc8c4f9a43f05578af3c0a788f011756
Deleted: sha256:611e1562bc2f489d72961d8e2e37f3097d64d9c5212a68c26aab2ad971c98f6d
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Command: docker attach [CONTAINER ID]
It may not be included depending on the -d option or the description method of the Dockerfile.
http://(サーバのIPアドレス)
You can confirm that the web server is running by accessing.
So far, we have described how to acquire and start an image, but in Web development, it is necessary to arrange the files and data to be displayed after building the server, but at that time, how to exchange files between the container and the host It is summarized below.
By specifying the "-v" option when starting the container, it is possible to mount the directory on the host side on the container side.
In the previous nginx example, you can mount it by specifying as follows
docker run -v /host_path:/container_path -d --name nginx-container -p 80:80 nginx
# docker run -v /root/html:/usr/share/nginx/html -d --name nginx-container -p 80:80 nginx
Create html / index.html in advance
index.html
<!DOCTYPE html>
<html>
<body>
<h1>title</h1>
<p>hoge</p>
</body>
</html>
/ usr / share / nginx / html
Now when you edit index.html on the host side, it will be published on the server on the container side.
Since the name can be resolved by the container name from the host OS, you can also log in by SSH.
I feel that there is a way to enable file exchange by setting up samba or an FTP server in the container.
There are various things, but in the case of a Web server, if you prepare the release script without manually arranging the files in the form of bringing the source from the repository and arranging it at the time of container construction (or after construction). I feel beautiful.
When using multiple containers, it is troublesome to start the containers individually, so you can save this trouble by using docker-compose etc.
Docker Compose Overview Compose is a tool for Docker applications that define and run multiple containers. Compose uses YAML files to configure application services. Just execute one command to generate and start the application service based on the settings. https://docs.docker.jp/compose/overview.html
With Laravel + Docker, you can use all of Laradock on board, but I think it's difficult to grasp the functions because you are riding too much.
You can install it with the following command.
※Note
Using the latest Compose release number in the download command The command shown above is just an example, so it may already be an older release number. Check the Compose repository release page on GitHub for the latest version.
Releases ・ docker / compose https://github.com/docker/compose/releases
# sudo curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# sudo chmod +x /usr/local/bin/docker-compose
# docker-compose --version
docker-compose version 1.27.4, build 40524192
That's all. Easy!
If you use docker-compose, you can save the trouble of construction work and start processing when using multiple containers. Create a file called docker-compose.yml and configure the settings when using multiple containers in YAML.
This time, I will describe the settings for one nginx container started by the docker command earlier.
We will proceed on the premise of the following directory structure.
Arbitrary directory
├ docker-compose.yml
├ Dockerfile
└ /html
└ index.html #Nginx document root
docker-compose.yml is written in YAML as below
docker-compose.yml
version: '3' #Magic
services:
nginx: #The name of the container
build: . #The location of the Dockerfile for this container
ports:
- "80:80" #Setting to associate host number 80 with container port 80
volumes:
- ./html:/usr/share/nginx/html #Settings to mount the document root on the host
Add a description based on the nginx image in the Dockerfile
Dockerfile
#base imgae (specify the REPOSITORY displayed by the docker images command)
FROM nginx:latest
Place the html file ʻindex.html to be displayed on the web page in the document root
./html`.
Execute the following command in the directory containing the docker-compose.yml file of the container group you want to start.
# docker-compose up
If you want to start it in the background, add the "-d" option.
# docker-compose up -d
# docker-compose down
The following is a description example of docker-compose.yml for the environment using Apache + PHP + MySQL for Web applications. (With phpmyadmin)
Container type, Dockerfile path, container startup settings and options can be described in docker-compose.yml, but each file must be placed in the location referenced in docker-compose.yml. There is. Please refer to the following repository for the directory structure such as dockerfile.
docker-apache-php-mariadb https://github.com/nc30mtd/docker-apache-php-mariadb
docker-compose.yml
version: "3"
services:
#Web
template-web:
container_name: template-web
build: ./setup
privileged: true
volumes:
- ./html:/var/www/html
ports:
- 80:80
depends_on:
- template-db
tty: true
stdin_open: true
# MySQL
template-db:
image: mariadb:latest
restart: always
container_name: template-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: templatedb(Be careful here)
MYSQL_USER: templatedbuser(Be careful here)
MYSQL_PASSWORD: templatedbpass(Be careful here)
MYSQL_START_TIMEOUT: 1200000
TZ: 'Asia/Tokyo'
volumes:
- ./database/data:/var/lib/mysql
- ./database/sql:/docker-entrypoint-initdb.d
- ./database/log/mysql:/var/log/mysql
ports:
- 3306:3306
# phpmyadmin
template-phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_ARBITRARY: 1
PMA_HOST: template-db(Be careful here)
PMA_USER: templatedbuser(Be careful here)
PMA_PASSWORD: templatedbpass(Be careful here)
PMA_PORT: 3306
links:
- template-db
ports:
- 8080:80
volumes:
- ./phpMyAdmin/sessions:/sessions
depends_on:
- template-db
When configuring a common Web application of Web server + PHP + DB
――Do not pack everything in one container ――Probably the Dockerfile will be complicated when you start packing everything ――I don't understand even if you look at it later. Difficult to reuse.
――I don't make everything myself ――People who are more amazing than you are making better images and containers in the world. --Get on the giants
――At least make a container by dividing it into Web (+ PHP) and DB --When performing verification when upgrading the version of a DB application, you only need to run the new version of the DB application for the DB container and maintain only the application side. ――If the system can be loosely coupled by combining primitive processing well, development will be difficult, but there will be less time and effort for repairs. (There should be less degreasing) --At most, the battlefield is difficult to expand after it has converged (the supply line does not extend completely)
You can create an environment where work progresses relatively quickly just by considering the combination of images and containers.
https://www.fsi.co.jp/solution/vmware/knowledge/virtualization.html https://qiita.com/r-tominaga/items/8ac588d603802572185f https://qiita.com/Qiita/items/4ff5873776992f0511d6 https://docs.docker.jp/compose/overview.html https://qiita.com/s-suefusa/items/cb3c4044da3b3657dbd0
Recommended Posts