[PYTHON] I created a Dockerfile for Django's development environment

Introduction

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.

Advance preparation

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

Create a Django project

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.

スクリーンショット 2020-08-10 23.08.30.png

Construction

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.

Creating a Dockerfile

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

Creating requirements.txt

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.

Creating an image from a Dockerfile

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.

Start container

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: スクリーンショット 2020-08-11 2.13.29.png

merit

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.

Summary

--Created a Dockerfile --Created an image from Dockerfile --Started the container from the created image and confirmed the behavior

Recommended Posts

I created a Dockerfile for Django's development environment
Building a Python development environment for AI development
Creating a development environment for machine learning
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Build a Kubernetes environment for development on Ubuntu
Build a mruby development environment for ESP32 (Linux)
I want to create a nice Python development environment for my new Mac
Set up a development environment for natural language processing
I want to easily build a model-based development environment
How to build a development environment for TensorFlow (1.0.0) (Mac)
[For organizing] Python development environment
[Day 1] Prepare Django's development environment
[Memo] Build a development environment for Django + Nuxt.js with Docker
Dockerfile for creating a data science environment based on pip3
I want to create a Dockerfile for the time being.
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
I touched PyAutoIt for a moment
Step by Step for creating a Dockerfile
[For beginners] Django -Development environment construction-
Python development environment options for May 2020
Emacs settings for Python development environment
Created an environment for Anaconda & Jupyter
Rebuild Django's development environment with Docker! !! !! !!
Build a local development environment for Lambda + Python using Serverless Framework
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
[python] I created a follow-up correlation diagram for twitter (Gremlin edition)
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
I made a dash docset for Holoviews
I created a template for a Python project that can be used universally
Python development environment for macOS using venv 2016
Easily build a development environment with Laragon
Create a Python development environment locally at the fastest speed (for beginners)
Let's create a virtual environment for Python
I created an Anaconda environment using Docker!
I want to build a Python environment
[Mac] Building a virtual environment for Python
I created a visualization site for GDP (Gross Domestic Product) using DASH!
I installed Kivy on a Mac environment
Prepare the development environment for keyhac for Mac
Building a conda environment for ROS users
Construction of development environment for Choreonoid class
I built a TensorFlow environment on windows10
I tried to build a Mac Python development environment with pythonz + direnv
I started Node.js in a virtual environment
I created a password tool in Python.
I made a library for actuarial science
Escape from Python's virtual environment ~ A story about being trapped in a virtual environment I created ~
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build a C language development environment with a container
I made a python dictionary file for Neocomplete
Created a Python wrapper for the Qiita API
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
Prepare a programming language environment for data analysis
Created a header-only library management tool for C / C ++
[Python] Build a Django development environment with Docker
I made a downloader for word distributed expression
Build a Python development environment on your Mac
[Python] Web development preparation (building a virtual environment)
Build a Django development environment with Doker Toolbox
Commands for creating a python3 environment with virtualenv