Consider the description of Dockerfile (Django + MySQL②)

About this article

This article is ** Part.2 ** of a group of articles related to "Learn how to use Docker through building a Django + MySQL environment".

  1. Build a Python virtual environment using venv
  2. ** Consider the description of Dockerfile (this article) **
  3. Consider the description of docker-compose.yml
  4. Edit the configuration file and execute docker-compose up
  5. Adjust container startup timing between dependent services

Introduction

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. ** **

Description

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

Think about the meaning of the description

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 variablePYTHONUNBUFFERED 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.)

Current directory structure

django_starter
    ├── config
    │     ├── __init__.py
    │     ├── asgi.py
    │     ├── settings.py
    │     ├── urls.py
    │     └── wsgi.py
    ├── Dockerfile        <- New!
    ├── manage.py
    ├── .venv
    │   └── (Abbreviation)
    └── requirements.txt

At the end

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

Consider the description of Dockerfile (Django + MySQL②)
Consider the description of docker-compose.yml (Django + MySQL ③)
The meaning of ".object" in Django
I tried the asynchronous server of Django 3.0
Understand the benefits of the Django Rest Framework
Script to change the description of fasta
How to check the version of Django
The meaning of {version-number} in the mysql rpm package
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
Exclusive release of the django app using ngrok
I checked the session retention period of django
Consider improving the accuracy of VAE abnormality detection
The story of viewing media files in Django
Django + MySQL settings
General description of the CPUFreq core and CPUFreq notifiers
The wall of changing the Django service from Python 2.7 to Python 3
[Django] Change the Default IP address of the runserver command
[Django 2.2] Sort and get the value of the relation destination
[Python] Let's change the URL of the Django administrator site
The beginning of cif2cell
The meaning of self
Impressions of touching Django
the zen of Python
The story of sys.path.append ()
[Django] Rename the project
Use MySQL with Django
Revenge of the Types: Revenge of types
Edit the config file and run docker-compose up (Django + MySQL ④)
DJango Note: From the beginning (simplification and splitting of URLConf)
I participated in the translation activity of Django official documents
Django returns the contents of the file as an HTTP response
Django + MongoDB development environment maintenance (in the middle of writing)
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
The story of a Django model field disappearing from a class
Summary of stumbling blocks in Django for the first time
Until the start of the django tutorial with pycharm on Windows
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]