Previously, in the following article, I explained how to build a Django development environment with a single container using Dockerfile. Simple Docker + Django development environment construction
This time, I will describe the method using Docker Compose.
As with the last time, we are aiming to build a simple environment, so we will use Django standard SQLite 3 as it is for the database.
We assume Windows and Mac with Docker Desktop installed, or Linux with Docker Engine and Docker Compose installed.
For Windows and Mac, Docker Compose is included in Docker Desktop, so there is no need to install it separately.
The commands in the article are executed on the Mac terminal, but if Docker Desktop is installed, it seems that the same commands can be executed on the Windows command prompt.
Use Dockerfile and docker-compose.yml to create a Django development environment with multiple Docker containers.
Create an arbitrary working directory on the local machine, and create a file named Dockerfile
, requirements.txt
, docker-compose.yml
and a directory with an arbitrary name in it. (Here we call it src).
Dockerfile
docker-compose.yml
requirements.txt
src/
Edit the contents of the Dockerfile as follows.
Dockerfile
FROM python:3.8.3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Edit the contents of requirements.txt as follows.
requirements.txt
django
pyyaml
Edit the contents of docker-compose.yml as follows.
docker-compose.yml
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./src:/code
ports:
- "8000:8000"
Dockerfile and requirements.txt are explained in Previous article, so I will omit them.
In docker-compose.yml
, define services such as web server, database, mail server, etc.
Only the service named web is defined here.
If you start the container with the docker-compose
command, the container will be started for each service defined here.
Execute the docker-compose run
command under the directory where Dockerfile etc. exists.
By specifying the service name web after run
, you will execute the django-admin start project
command on the web service.
$ docker-compose run web django-admin.py startproject django_prj .
According to build: .
described in the web service in docker-compose.yml
, if the container is not created, the image is automatically built and the container is created at this timing.
You can see that the container has been created on the dashboard window of Docker Desktop. If your Django project is created successfully, a project template file will be created under the src directory.
db.sqlite3
django_prj
manage.py
Execute the docker-compose up
command under the directory where Dockerfile etc. exists.
$ docker-compose up
You can see that the container has started on the dashboard window of Docker Desktop.
For confirmation, go to http: // localhost: 8000 /
in your browser.
After that, read Django's official documents and commercial reference books, and if a command is specified, add docker-compose run [web service name]
before the command to Django on the Docker container. Various operations will be executed for.
Like Docker, the information on building the development environment for Docker Compose and Django was also a big hit, but I could only find an entry with over-engineered settings for the production environment, so I will read this article for myself to look back on. created.
It doesn't have a body or a lid, but it's easiest to understand how to create a Django container as explained in the official Docker documentation, so be sure to check it out.
There is a translation of the page on the document Japanese site, but the settings may differ from the original, so if you can't read English, refer to the original for the settings and translate the explanation into Japanese. I think you should check the documentation.
Recommended Posts