Django development in Docker containers

To keep things tidy, let's create an empty directory named Django to host all the files. Inside the Django directory, we need to define the content of a container by creating a Dockerfile using our favorite text editor. A Dockerfile defines the base image of a container as well as the commands that are necessary to compile an image.

For more information about Dockerfile, please visit https://docs.docker.com/engine/reference/builder/.

We will use Python 3.6.5 as our base image. Please copy the following code to your Dockerfile. A series of additional commands define the working directory and the initiation process:

# The official Python 3.6.5 runtime is used as the base image
FROM python:3.6.5-slim
# Disable buffering of output streams
ENV PYTHONUNBUFFERED 1
# Create a working directory within the container
RUN mkdir /app
WORKDIR /app
# Copy files and directories in the current directory to the container
ADD . /app/
# Install Django and other dependencies
RUN pip install -r requirements.txt

As you may notice, we also need a text file, requirements.txt, to define any package dependencies in our project. Please add the following content to the requirements.txt file in the folder where the project is present:

Django==2.0.4
Matplotlib==2.2.2
stockstats==0.2.0
seaborn==0.8.1

Now, we can run docker build -t django in the terminal to build the image. It takes up to several minutes before the process is complete:

Please make sure you are currently located in the same project folder before running the command.

The following message will be shown if the building process is complete. The exact hash code at the end of the Successfully built ... message could be different:

Successfully built 018e75992e59
Successfully tagged django:latest
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.212