Skip to content

This is an sample of building a Django environment on Docker using Nginx, Gunicorn, and PostgreSQL.

License

Notifications You must be signed in to change notification settings

dsonoda/django-on-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-on-docker

This is an sample of building a Django environment on Docker using Nginx, Gunicorn, and PostgreSQL.

Usage

Git clone

$ git clone git@github.com:dsonoda/django-on-docker.git

Building a development environment

Goal

Use docker-compose to set up a service-specific container on a single host to build a Django web application development environment. The host is assumed to be in a local environment.

1. Edit .env.development and set the environment variables

SECRET_KEY=[django secret key value]
DB_PASSWORD=[db password]
POSTGRES_PASSWORD=[db user password]

2. Launch the containers

$ cd django-on-docker
$ docker-compose -f docker-compose.development.yml up -d --build
...
Creating django-on-docker_db_1 ... done
Creating django-on-docker_app_1 ... done
Creating django-on-docker_nginx_1 ... done

$ docker-compose -f docker-compose.development.yml ps -a
        Name                        Command               State          Ports
----------------------------------------------------------------------------------------
django-on-docker_app_1     /usr/src/app/entrypoint.de ...   Up      8000/tcp
django-on-docker_db_1      docker-entrypoint.sh postgres    Up      5432/tcp
django-on-docker_nginx_1   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:1337->80/tcp

3. Initialization

All data deleted.
Remove data from all tables except for some, such as migration management.

$ docker-compose -f docker-compose.development.yml exec app python manage.py flush --no-input

Generating a migration files.
If a model modification occurs in subsequent development, it must be run again.

$ docker-compose -f docker-compose.development.yml exec app python manage.py makemigrations

Create DB tables from the generated migration files.
If a model modification occurs in subsequent development, it must be run again.

$ docker-compose -f docker-compose.development.yml exec app python manage.py migrate

Move the static files into the static directory.

$ docker-compose -f docker-compose.development.yml exec app python manage.py collectstatic --no-input --clear

Registering an administrative user.

$ docker-compose -f docker-compose.development.yml exec app python manage.py createsuperuser
Username (leave blank to use 'app'): [admin user name]
Email address: [admin user email]
Password: [admin user password]
Password (again):
Superuser created successfully.

4. Access pages

page url note
Frontend file upload page http://localhost:1337/uploads/ Try file upload.
Admin login http://localhost:1337/admin/login/ Enter the registered administrator's information.

5. Other commands

Stop the container and suspend development.

$ docker-compose -f docker-compose.development.yml stop

Start the container and resume development.

$ docker-compose -f docker-compose.development.yml start

Restart the container.

$ docker-compose -f docker-compose.development.yml restart

Destroy images, containers, volumes, and networks.

$ docker-compose -f docker-compose.development.yml down

If you want to modify only the app code and check the result, restart the app container only.

$ docker container restart django-on-docker_app_1

Building a production environment

Recently Announced.

Author

License

This repository is under MIT license.

About

This is an sample of building a Django environment on Docker using Nginx, Gunicorn, and PostgreSQL.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published