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.
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.
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.
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
requirements.txt and a directory with an arbitrary name in it (here, src). Masu).
Dockerfile requirements.txt src/
Edit the contents of the Dockerfile as follows.
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.
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.
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
-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
After executing the
docker build command, confirm that the image was created normally with the
docker images command.
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
-it option is an option that mediates standard I / O.
Without it, the container will end immediately after starting.
-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.
-p option is a port forwarding option.
Connect port 8000 on the local machine to port 8000 on the container.
-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.
--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.
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].
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
Django has a development server function that allows you to check the operation independently without using a web server such as Apache or Nginx.
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.
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.
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