Start Django on the Docker container using docker-compose. I use a Mac.
In a Docknized application, the database and middleware containers may be separated and the application may be composed of multiple containers. Running Docker run in each container is tedious. At that time, if you use docker-compose, you can control multiple containers with one command. docker-compose describes and controls the behavior of the container in docker-compose.yml.
The configuration that separates containers for each function is also introduced in the AWS tutorial and is called a microservice. The advantage of microservices-aware configurations is that application development and testing can be done in small units for each function. In addition, if each container is designed as loosely coupled, each function can be developed independently, so it is possible to develop, deploy, and scale without being aware of the work of other teams. Even in the startup development that I experienced, by designing with microservices in mind, it was convenient to be able to perform parallel development, verification and testing in small functional units.
This article is part of the Build Django app on Docker and deploy it to AWS Fargate (https://qiita.com/keita_gawahara/items/66e58a6eed5c0c5f31d2).
As a preparation, use Pipenv to install Django in your virtual environment. Please refer to the link because I have written a separate article for the detailed procedure. https://qiita.com/keita_gawahara/items/487a635d226fb0705b13
Create a Dockerfile in the root folder of your project.
$ touch Dockerfile
Add the following contents to the Dockerfile.
FROM python:3.7-slim
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /
COPY Pipfile Pipfile.lock /
RUN pip install pipenv && pipenv install --system
COPY . /
Build the Dockerfile with the docker build command.
$ docker build .
Sending build context to Docker daemon 155.1kB
...
Step 7/7 : COPY . /
---> d239897dhfii9u”
Successfully built d26473003af
$touch docker-compose.yml
Add the following contents to docker-compose.yml.
version: '3.7'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
Start the container using the docker-compose up command.
$ docker-compose up
Creating network...
...
web_1 | System check identified no issues (0 silenced).
web_1 | July 13, 2019 - 16:47:03
web_1 | Django version 2.2.3, using settings 'hello_project.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
When I browsed http://127.0.0.1:8000 in my browser, the Django welcome page was displayed. You've now launched Django on Docker.
After quitting with CONTROL-C, use docker-compose down to stop the container completely.
$ docker-compose down
Recommended Posts