We will use Docker and Docker-compose to build a development environment for Django and postgreSQL. Hit http: // localhost: 8000
until the screen shown below is displayed.
If you already have Django, you don't have to.
pip install Django
Install Docker from here
** Check version **
$ docker --version
Docker version 18.09.2
Install Docker-compose with the following command.
$ curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
** Check version **
$ docker-compose --version
docker-compose version 1.23.2
$ mkdir docker-demo-with-django && cd docker-demo-with-django
$ mkdir app && cd app
$ vi Pipfile
/app/Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
Django = "==3.1"
[requires]
python_version = "3.7.5"
Use the following command to install and log in.
$ pip install pipenv
$ pipenv install
$ pipenv shell
Use the following command to start the project and start the server.
$ django-admin.py startproject django_demo .
$ python manage.py migrate
$ python manage.py runserver
From here, we will set up the Docker container.
Dockerfile
Create a Dockerfile
$ vi Dockerfile
app/Dockerfile
#Python3 from the official.7 on alpine linux image pull
FROM python:3.7-alpine
#Set working directory
WORKDIR /usr/src/app
#Set environment variables
#Prevent Python from writing to pyc files and discs
ENV PYTHONDONTWRITEBYTECODE 1
#Prevent Python from buffering standard I / O
ENV PYTHONUNBUFFERED 1
#Installation of psycopg2
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk del build-deps
#Install Pipenv
RUN pip install --upgrade pip \
&& pip install pipenv
#Copy the host pipfile to the container's working directory
COPY ./Pipfile /usr/src/app/Pipfile
#Install packages from pipfile and build a Django environment
RUN pipenv install --skip-lock --system --dev
#Copy the host's current directory (currently the app directory) to your working directory
COPY . /usr/src/app/
# entrypoint.run sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
vi ./../docker-compose.yml
docker-demo-with-django/docker-compose.yml
version: '3.7'
services:
#Service name can be set freely
django:
#From within the app directory`Dockerfile`Find
build: ./app
#Set the command to be entered after starting the service
command: python manage.py runserver 0.0.0.0:8000
#Settings for persisting data.`host:container`Describe the path with
volumes:
- ./app/:/usr/src/app/
#Specify the port to open.`host:container`List the port in
ports:
- 8000:8000
#Specify environment variables
environment:
#1 is debug mode
- DEBUG=1
- SECRET_KEY=hoge
- DATABASE_ENGINE=django.db.backends.postgresql
- DATABASE_DB=django_db
- DATABASE_USER=django_db_user
- DATABASE_PASSWORD=password1234
- DATABASE_HOST=postgres
- DATABASE_PORT=5432
#Specify the service to connect to
depends_on:
- postgres
postgres:
#Pull the image from the official
image: postgres:11.4-alpine
#Database persistence
#At the beginning so as not to mount in the host directory`./`Do not attach
volumes:
- postgres_data:/var/lib/postgresql/data
#Create a database with su privileges and the same name as the specified user
#value is the same as the one specified in the django service
environment:
- POSTGRES_USER=django_db_user
- POSTGRES_PASSWORD=password1234
- POSTGRES_DB=django_db
#"Named volumes" written at the top level can be referenced from multiple services
volumes:
postgres_data:
#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $DATABASE_HOST $DATABASE_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
python manage.py flush --no-input
python manage.py migrate
exec "$@"
$ docker-compose up -d --build
Connect to the following URL, and if you can connect, the construction is completed http://localhost:8000