This article is ** Part.2 ** of a group of articles related to "Learn how to use Docker through building a Django + MySQL environment".
In this article, we'll consider writing a Dockerfile
to create a ** Docker image ** for Django
.
For ** Docker image **, it's like (just as an "image in my head") ** a small dedicated machine for running projects that runs on an OS independent of the host machine ** I'm catching it.
Design the officially provided Python
image for your Django
project through the description in the Dockerfile
. The description itself is almost the same as what is written in the Official Document. Here, we will consider the meaning of the description so that we can design and customize it as needed in the future.
In addition, we will proceed assuming that Docker has already been installed.
** Dockerfile
is created directly under the directory of the created project previous. ** **
First, the description of the entire file is as follows.
Dockerfile
FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/
EXPOSE 8000
From top to bottom,
FROM python3.7
On the first line, ** specify which image to use from the already created images **. Python3.7
is specified here according to the virtual environment created in Previous article.
ENV PYTHONUNBUFFERED 1
On the second line, ** ʻENVspecifies the environment variable **. This means that the value of the environment variable
PYTHONUNBUFFERED is set to
1`. Set to disable Python buffering for standard I / O.
RUN mkdir /code
On the 4th line, you can use RUN
to ** specify the command to be executed at build time **. Here, we are creating the code
directory.
WORKDIR /code
On the 5th line, ** specify the working directory **. It specifies from which directory the subsequent RUN
and CMD
will be executed.
COPY requirements.txt /code/
After a while, on the 7th line, Last time Copy the created ** requirements.txt
under the code
directory **. You can do the same with ʻADD, but it seems that ** COPY
is recommended for copying pure files ** due to its versatility. (See official documentation (https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy))
RUN pip install --upgrade pip
On line 8, upgrade the pip
itself,
RUN pip install -r requirements.txt
On the 9th line, install the packages described in the requirements.txt
that you copied earlier.
COPY . /code/
Line 11, this will copy all ** all directories / files ** under the ** host PC (Mac) project root ** (the hierarchy where the Dockerfile is located) to the ** container that will be generated later. ** Will be.
EXPOSE 8000
Finally, ** declare the port number to use **. ** It's customary to use the standard numbers used in libraries / frameworks **, so I'm using Django
to specify the default number 8000
. (I also tried declaring another number, but it seemed to work fine if it was consistent with the command line arguments at server runtime and the description in docker-compose.yml
that I created later.)
django_starter
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── Dockerfile <- New!
├── manage.py
├── .venv
│ └── (Abbreviation)
└── requirements.txt
Now, the description of the Dockerfile
to create the Django
environment is a break.
Next, let's think about the description of docker-compose.yml
based on the Dockerfile
created this time.
Click here for the next article ↓
"3. Consider the description of docker-compose.yml"
Thank you for visiting.
Recommended Posts