Launch Django on a Docker container with docker-compose up

Introduction

Start Django on the Docker container using docker-compose. I use a Mac.

Reasons to use docker-compose

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).

Creating a Dockerfile

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

Launch Django on a Docker container with docker-compose up
Deploy a Django application with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Set up a Samba server with Docker
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
A note on enabling PostgreSQL with Django
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Launch Flask application with Docker on Heroku
Run matplotlib on a Windows Docker container
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
[Python] Build a Django development environment with Docker
Carry a Docker container
How to run a Django application on a Docker container (development and production environment)
Looking back on creating a web service with Django 1
Launch a Python web application with Nginx + Gunicorn with Docker
How to build a Django (python) environment on docker
Set up Docker on Oracle Linux (7.x) with Vagrant
Looking back on creating a web service with Django 2
A note on speeding up Python code with Numba
A memo about building a Django (Python) application with Docker
Deploy a Django app made with PTVS on Azure
Build a development environment with Poetry Django Docker Pycharm
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Create a homepage with django
[Memo] Build a development environment for Django + Nuxt.js with Docker
Create a Todo app with Django ① Build an environment with Docker
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Creating a Flask server with Docker
Build a deb file with Docker
Django Tips-Create a ranking site with Django-
Implement a Django app on Hy
docker / docker-compose versioned installation (up | down)
code-server online environment (5) Launch code-server on Docker
Make a filter with a django template
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
How to delete a Docker container
Launched a web application on AWS with django and changed jobs
Create a file uploader with Django
Run a Python file inside a Docker container on a remote Raspbian via PyCharm
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
A series of amateur infrastructure engineers touching Django with Docker (2): Creating a model
A memo when setting up a Docker container for using JUMAN ++, KNP, python
Django + Apache with mod_wsgi on Windows Server 2016
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
Throw a request with a certificate on httpie
Let's scrape a dynamic site with Docker
Creating a login screen with Django allauth
[Linux] Build a jenkins environment with Docker
Launch environment with LineBot + Heroku + Docker + Python
Run a Python web application with Docker
Create a web service with Docker + Flask
I made a WEB application with Django
[Linux] Build a Docker environment with Amazon Linux 2
A series of amateur infrastructure engineers touching Django with Docker ⑤: View, Template, CSS
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Django + Docker
Build a LAMP environment on your local Docker
Build a C language development environment with a container