First part: Docker conversion
Second part: docker-compose introduction`
It will be the description about. (The second part is here)The command notation is based on the assumption that MacOS
will be used as the host PC.
In this article, we will build a local PC as a Docker host PC.
In this case, you need to prepare to run Docker.
There are several methods, but for MacOS
, installing Docker for Mac
with brew is the quickest way.
Terminal
brew cask install docker
This will install the application, so launch Docker.app
locally. If an icon like a whale appears in the upper right and it is Running as shown below, it is complete.
See Github repository.
There are two main changes from Step 1 in making Docker containerization.
--Create a new Dockerfile for each web server and app server --Create a new Makefile containing the startup command
Of these, the second Makefile only simplifies the input of docker commands, and the essential addition is the first Dockerfile. I would like to take a look at the contents of each Dockerfile. (The Dockerfile itself will be described later)
Dockerfile
#Specify the base image. In this case, a specific tag is specified up to the version.
FROM nginx:1.17.6-alpine
#Host PC configuration file(config/nginx.conf)Copy to the directory created above and send
RUN mkdir -p /etc/nginx
COPY config/nginx.conf /etc/nginx/nginx.conf
If you just want to use nginx, just pull the nginx Official Image published on Docker Hub
. This time, I want to read the nginx configuration file created at hand and start it, so I am doing the following work.
Dockerfile
#This time it is based on a debian-based python image
FROM python:3.8-slim-buster
#Transfer of configuration files(I can't think of a suitable place/Directly under the app)
RUN mkdir -p /app/config
COPY requirements.txt /app/requirements.txt
COPY config/gunicorn_settings.py /app/config/gunicorn_settings.py
COPY flask_app.py /app/flask_app.py
#Package installation
RUN pip install -r /app/requirements.txt
EXPOSE 9876
WORKDIR /app
#Command at run time
# ENTRYPOINT->In CMD, CMD can be overwritten with the argument at the time of run
ENTRYPOINT [ "gunicorn", "flask_app:app" ]
CMD [ "-c", "/app/config/gunicorn_settings.py" ]
You can start with the base image of os only and install python etc., but since the official image of python has been released, this time we will start based on this.
Up to package installation
is almost the same as the web server, but below that it is a description that the web server did not have. I am doing the following.
/ app
gunicorn flask_app: app -c /app/config/gunicorn_settings.py
command when starting the container.About Docker
I think there are some things about this, but here are two of the benefits I feel.
In the past, when developing a team, it was necessary for the developer to prepare a development environment on each machine and then start development. There may be a document that shows the procedure, but in reality, it may cost more because the environment cannot be built properly due to reasons such as incomplete documents or tool updates. There is also the risk that each individual will upgrade their language and cause subtle differences. On the other hand, by using the Docker container prepared in advance,
--Environment can be built with a small number of commands (docker run
)
--Easy to unify development environment
You can benefit from that.
Now that operation on the cloud has become commonplace, many container orchestration tools using GKE etc. are on the market. Since it is just a container orchestration, it means that the application is actually running on the container even in the production environment. Of course, it is necessary to change environment variables and endpoints between development, staging, and production, but it can also be absorbed by the orchestration side, and in any case ** The container that worked in the development environment can be used almost as it is in the production environment. It will be operational **.
First, create an image (= build), then create and start a container based on that image. It's not a good example, but if you replace it with a PC game --Docker image = disk --Docker container = window while the game is running It's like that.
A service that allows you to upload, publish, and share container images that you have created. In short, it's like * Github's Docker Limited Edition *. The published images can be freely downloaded and used. When creating your own customized image, select the base image from the ones registered in Docker Hub.
Many images by volunteers are also registered in Docker Hub. Among them, the one that should be selected as the base image is the one with the tag Docker Official Images
.
In a nutshell, it's ** because it's reliable **, but here are a few bullet points.
--Timely security updates --There is a clear document --Provide Dockerfile best practices --Providing an indispensable base image
It is a file that describes instructions that can perform a series of operations such as the image that is the base of the container to be created, application installation, and preparation for application startup.
Please refer to here for details on the description of the Dockerfile.
Recommended Posts