Since it was necessary to learn Dockerfile first in order to learn docker-compose, I would like to create a Dockerfile using an application of Django, which is a Python framework.
As a preliminary preparation, the Docker image and container will start empty.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
$docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
This time, I will use the app created with Django created in this article.
To explain only what you should keep in mind so that you do not have to read the article, the app will be launched by executing the following command.
$ python manage.py runserver
If you access http: // localhost: 8000 / hello
while the app is running, the following Hello World will be displayed.
The structure is as shown below.
It is also available on GitHub.
Dockerfile
$ tree
.
├── Dockerfile
├── requirements.txt
├── hello
│ └── (omitted)
├── helloWorldProject
│ └── (omitted)
├── README.md
├── db.sqlite3
└── manage.py
Dockerfile
and requirements.txt
are the files that should be created this time, so I will explain them.
This time, I created it as follows.
Dockerfile
# python:3.Set 8 official images as the base image
FROM python:3.8
#Creating a working directory
RUN mkdir /code
#Working directory settings
WORKDIR /code
#Copy assets in the current directory to the specified directory on the container
ADD . /code
#requirements with pip.Add the package specified in txt
RUN pip install -r requirements.txt
#Start (start to accept on port 8002 of the container)
CMD python3 manage.py runserver 0.0.0.0:8002
The contents of this file are as follows.
--Create a working directory (/ code
) based on the official image of python: 3.8
--Transfer all current sources to this container
--Install the required modules with pip
--It is written that the application is started when the container is started.
I will not explain how to write a Dockerfile.
The difference between RUN
and CMD
was easy to understand in this article.
(Simply put, RUN
is the command executed during build and CMD
is the command executed when the container starts.)
Modules installed with the pip command, as described in the Dockerfile, are now described in requirements.txt
.
This time we'll need Django, so we'll write that.
requirements.txt
Django==3.1
If there is only one module like this time, it is not necessary to make it an external file, but if there are multiple modules to be installed, it is easier to manage if it is described like this.
After preparing the Dockerfile, the image is created by executing the docker build -t [image name to be created] [relative path to the Dockerfile]
command.
This time we will create an image named hello-world
.
$ docker build -t hello-world .
(abridgement)
Successfully tagged hello-world:latest
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 6e2fc6616ab1 About a minute ago 922MB
python 3.8 6feb119dd186 5 days ago 882MB
You can see that the image hello-world
was created like this.
By the way, the image of python: 3.8
is also pulled and displayed.
Looking at the item of SIZE
, hello-world
is larger than python
, so you can see that various additions have been made to the original image.
Now that we have created the image, let's start the container.
$ docker container run -d -p 8001:8002 hello-world
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff92b40204f3 hello-world "/bin/sh -c 'python3…" About an hour ago Up About an hour 0.0.0.0:8001->8002/tcp funny_antonelli
The supplementary information about the options of the run
command is as follows.
---d
: Start in the background
---p
: Expose the container port
Since we have specified 8001: 8002
this time, it means that we are associating "host port 8001" with "container port 8002".
(Originally, ports 8001 and 8002 are not used, but we use different numbers to be aware of the port numbers of the host and container.)
If you visit http: // localhost: 8001 / hello
, you should see something like this:
By writing a Dockerfile file
"Every time you pull this Docker Image and then run the docker container run command, be sure to log in to that container and run this command and this command ~ (omitted)"
Procedures such as
"Run the docker build -t [image name] .command in the directory where the Dockerfile is located. 』\
You will only need one line of.
For example, in this case, everything below FROM python: 3.8
in the Dockerfile must be shown in the procedure manual.
--Created a Dockerfile --Created an image from Dockerfile --Started the container from the created image and confirmed the behavior