Build a simple Docker Compose + Django development environment

Overview

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.

Premise

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.

Create multiple containers with Docker Compose

1. Create a working directory

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/

2. Edit the configuration file

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.

3. Create a container and Django project

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. django_s_test_dashboard.png If your Django project is created successfully, a project template file will be created under the src directory.

db.sqlite3
django_prj
manage.py

4. Start the container

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. django_s_test_dashboard.png For confirmation, go to http: // localhost: 8000 / in your browser.

django_s_test_rocket.png If you see the home page with the rocket image, your Django project is working properly. Thank you for your hard work: D┼─┤

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.

Impressions

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.

reference

Recommended Posts

Build a simple Docker Compose + Django development environment
Build a simple Docker + Django development environment
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
Build a development environment for Docker, java, vscode
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Try to build a Java development environment using Docker
Build a Node.js environment with Docker
Build Rails environment with Docker Compose
Build Unity development environment on docker
Docker × Java Building a development environment that is too simple
Build a WAS execution environment from Docker
Build a Java development environment on Mac
Build an Ultra96v2 development environment on Docker 1
Build a browser test environment using Capybara in the Docker development environment
Build a Laravel / Docker environment with VSCode devcontainer
I tried to build a Firebase application development environment with Docker in 2020
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Win10] Build a JSF development environment with NetBeans
A reminder of Docker and development environment construction
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
[First team development ②] Build an environment with Docker
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
Build a Doker-based development environment on Windows 10 Home 2020 ver. Part 1 Until WSL2-based Docker build
Build a development environment to create Ruby on Jets + React apps with Docker
Build a docker container for a python simple web server
Build a Ruby on Rails development environment on AWS Cloud9
Easily build a Vue.js environment with Docker + Vue CLI
Build Docker + Laravel PHP + Vue.js development environment in 5 minutes
[Note] Build a Python3 environment with Docker in EC2
Build Java development environment with WSL2 Docker VS Code
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
[Environment construction] Build a Java development environment with VS Code!
[2021] Build a Docker + Vagrant environment for using React / TypeScript
WSL2 + VSCode + Docker development environment
Build docker environment with WSL
Ruby ① Build a Windows environment
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
I tried to create a padrino development environment with Docker
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Java development environment (for Mac)
Build a XAMPP environment on Ubuntu