Build a simple Docker + Django development environment

Overview

This article aims to use Dcoker to build a Django development environment without having to install Python and Django on your local machine. There are two ways to build an environment using a container, one is to use only Dockerfile and the other is to use Docker Compose. This article describes the former.

Please check the following article for the latter. ⇒ Simple Docker Compose + Django development environment construction

As the title suggests, we're using the Django standard SQLite3 as is for the database, as we aim to build a simple, easy, problem-free, and fast-moving environment. If you want to use MariaDB or PostgreSQL, please refer to the official Dcoker documentation.

Premise

We assume Windows and Mac with Docker Desktop installed, or Linux with Docker Engine installed.

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.

About the official image

There is an official Django repository on Docker Hub, but it's deprecated as mentioned at the top of the TOP, and it's been abandoned for over 3 years. https://hub.docker.com/_/django

I tried to use this Docker image as it is, but I couldn't even use it for learning purposes because I stumbled at the project creation stage. I think it's best to give up honestly as something that doesn't exist.

Create a single container with Dockerfile only

1. Create a working directory

Use Dcokerfile to create a Django development environment with a single Docker container.

Create an arbitrary working directory on the local machine, and create a file named Dockerfile, requirements.txt and a directory with an arbitrary name in it (here, src). Masu).

Dockerfile
requirements.txt
src/

2. Edit Dockerfile and requirements.txt

Edit the contents of the Dockerfile as follows.

Dockerfile


FROM python: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

To briefly explain the above contents, based on the Python Docker image (based on Linux called Debian), first create a directory named code directly under the root, and then describe it in requirements.txt. The contents are to install the package with the pip command.

Python3 does not come with a yaml module, so it's a good idea to install pyyaml as well. Without the yaml module, it can cause Django dumpdata / loaddata errors.

The directory name can be any name other than code.

3. Create a Docker image

Execute the docker build command under the directory where Dockerfile and requirements.txt exist.

$ docker build -t django_s .
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
django_s            latest              0d5d29c3eef9        4 seconds ago       922MB

The -t option is an option for specifying the image name and tag. The image name is arbitrary and does not matter. Here it is django_s. Don't forget the last . (dot).

After executing the docker build command, confirm that the image was created normally with the docker images command.

4. Create and start a container

Execute the docker run command to create and start the container from the Docker image created in the previous section.

$ docker run -itd -p 8000:8000 -v /Users/dev_user/django_test/src:/code --name django_s_test django_s
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
1ea546d25f3e        django_s            "python3"           11 seconds ago      Up 10 seconds       0.0.0.0:8000->8000/tcp   django_s_test

The -it option is an option that mediates standard I / O. Without it, the container will end immediately after starting.

The -d option is a background run option. Without it, the prompt will not be returned during command execution ... but instead, it will be waiting for input in Python's interactive mode.

The -p option is a port forwarding option. Connect port 8000 on the local machine to port 8000 on the container.

The -v option is an option to mount the directory on your local machine inside the container. Here, the src directory created in Section 1-1. Is mounted on the _ / code_ directory of the container. If there is an update in _ / code_ on the container side, it will be reflected in the src directory, and if there is an update in src on the local machine side, it will be reflected in the _ / code_ directory. Rewrite the directory path appropriately according to the path of your own local machine.

The --name option specifies the name of the container. Any name is fine. Here it is django_s_test.

After executing the docker run command, use the docker ps command to confirm that the container has started normally and the process is running.

You can also check the running container from the dashboard window of Docker Desktop. django_s_test_dashboard.png

5. Create a Django project

From here on, it's mostly Django operations, not Docker.

First, go to the src directory and call the django-admin startproject command in the container through the docker exec command to create a Django project. Here, the project name is django_prj, but of course you can change the project name to anything you like.

$ cd /Users/dev_user/django_test/src
$ docker exec django_s_test django-admin startproject django_prj .

You can run any command in the Docker container with docker exec [container name]. The django-admin command is available if you have Django installed, so if you can't find this command, it's likely that docker build has failed. In that case, make sure that the pip install -r requirements.txt part in the Dockerfile and the django in requirements.txt are misspelled.

If your Django project is created successfully, a project template file will be created under the src directory.

db.sqlite3
django_prj/
manage.py

6. Start Django's development server

Django has a development server function that allows you to check the operation independently without using a web server such as Apache or Nginx.

Run the python manage.py runserver command in the container through the docker exec command, just as you did when creating the project.

To interrupt, press control + c.

$ docker exec django_s_test python manage.py runserver 0.0.0.0:8000
...(abridgement)...
Django version 3.1, using settings 'django_test.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

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 the official Django documentation and commercially available reference books, and if a command is specified, add docker exec [container name] in front of the command to use Django on the Docker container. Various operations will be executed.

Impressions

There was a lot of information on building Django's development environment with Docker, but there was no entry explaining why I did not use the official image posted on Docker Hub, and using Docker and Docker Compose 2 I didn't find an entry in one article mentioning that there was a way to do it, so I created this article for myself to look back on.

The article about building a development environment using Docker Compose is as follows. ⇒ Simple Docker Compose + Django development environment construction

reference

Recommended Posts

Build a simple Docker + Django development environment
Build a simple Docker Compose + Django development environment
Build a PureScript 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 Django + MySQL + nginx with Docker Compose
Build a development environment for Docker, java, vscode
Try to build a Java development environment using Docker
Build a Node.js environment with Docker
Build Unity development environment on docker
Build a WAS execution environment from Docker
Build a Java development environment on Mac
Build a browser test environment using Capybara in the Docker development environment
Build a Laravel / Docker environment with VSCode devcontainer
[Win10] Build a JSF development environment with NetBeans
[First team development ②] Build an environment with Docker
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
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)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
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)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
WSL2 + VSCode + Docker development environment
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 docker environment with WSL
Build Java development environment with WSL2 Docker VS Code
Build a development environment to create Ruby on Jets + React apps with Docker
Ruby ① Build a Windows environment
[Environment construction] Build a Java development environment with VS Code!
[2021] Build a Docker + Vagrant environment for using React / TypeScript
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
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
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!
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 Rails environment with Docker Compose
Build Java development environment (for Mac)
Build a XAMPP environment on Ubuntu
Build jooby development environment with Eclipse
Build docker + laravel environment with laradock
[Node.js] Docker-compose up makes it easy to build a development environment
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Prepare a transcendentally simple PHP & Apache environment on Mac with Docker
Build a development environment where Ruby on Rails breakpoints work on Windows