GithubHelp home page GithubHelp logo

ox1d0 / django_docker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from archatas/django_docker

0.0 1.0 0.0 76 KB

Boilerplate for Docker with Django, Gunicorn, Nginx, and PostgreSQL

License: MIT License

Dockerfile 7.14% Shell 0.49% Python 75.72% HTML 16.65%

django_docker's Introduction

Django Docker

1. Create executable build_dev.sh

Copy build_dev_example.sh to build_dev.sh.

Edit the build_dev.sh file and add sensible values there.

Add execution permissions:

$ chmod +x build_dev.sh

2. Build the Docker containers

Run build_dev.sh:

$ ./build_dev.sh

3. Check if the build was successful

If you now go to http://0.0.0.0/ you should see a "Hello, World!" page there.

If you now go to http://0.0.0.0/admin/, you should see

OperationalError at /admin/
FATAL:  role "myproject" does not exist

This means that you have to create the database user and the database in the Docker container.

4. Create database user and project database

SSH into the database container and create user and database there with the same values as in the .build_dev.sh script:

$ docker exec -it django_docker_db_1 bash
/# su - postgres
/$ createuser --createdb --password myproject
/$ createdb --username myproject myproject

When asked, enter the same password for the database as in the build_dev.sh script.

Press [Ctrl + D] twice to logout of the postgres user and Docker container.

If you now go to http://0.0.0.0/admin/, you should see

ProgrammingError at /admin/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM "django_se...

This means that you have to run migrations to create database schema.

5. Run migration and collectstatic commands

SSH into the gunicorn container and run the necessary Django management commands:

$ docker exec -it django_docker_gunicorn_1 bash
$ source env/bin/activate
(env)$ python manage.py migrate
(env)$ python manage.py collectstatic
(env)$ python manage.py createsuperuser

Answer all the questions asked by the management commands.

Press [Ctrl + D] twice to logout of the Docker container.

If you now go to http://0.0.0.0/admin/, you should see the Django administration where you can login with the super user's credentials that you have just created.

6. Overview of useful commands

Rebuild docker containers

$ docker-compose down
$ ./build_dev.sh

SSH to the Docker containers

$ docker exec -it django_docker_gunicorn_1 bash
$ docker exec -it django_docker_nginx_1 bash
$ docker exec -it django_docker_db_1 bash

View logs

$ docker-compose logs nginx
$ docker-compose logs gunicorn
$ docker-compose logs db

Copy files and directories to and from Docker container

$ docker cp ~/avatar.png django_docker_gunicorn_1:/home/myproject/media/
$ docker cp django_docker_gunicorn_1:/home/myproject/media ~/Desktop/

7. Create analogous scripts for staging, production, and test environments

Copy build_dev.sh to build_staging.sh, build_production.sh, and build_test.sh and change the environment variables analogously.

8. Feedback

If you have any feedback about the boilerplate code or this README file, please open new issues.

django_docker's People

Contributors

archatas avatar ox1d0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.