Code Monkey home page Code Monkey logo

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

django_docker's Issues

collectstatic

Hello,
Could you tell me why I get such an error while I run:
python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 21, in
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/myproject/env/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line
utility.execute()
File "/home/myproject/env/lib/python3.8/site-packages/django/core/management/init.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/myproject/env/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/myproject/env/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/myproject/env/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
collected = self.collect()
File "/home/myproject/env/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
handler(path, prefixed_path, storage)
File "/home/myproject/env/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 348, in copy_file
self.storage.save(prefixed_path, source_file)
File "/home/myproject/env/lib/python3.8/site-packages/django/core/files/storage.py", line 52, in save
return self._save(name, content)
File "/home/myproject/env/lib/python3.8/site-packages/django/core/files/storage.py", line 248, in _save
os.makedirs(directory, exist_ok=True)
File "/usr/local/lib/python3.8/os.py", line 213, in makedirs
makedirs(head, exist_ok=exist_ok)
File "/usr/local/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/home/myproject/static/site'

I have root permissions on the volumes

Deploy using TLS?

Hi, how do I set nginx to work with https here? I am planning to use Lets Encrypt.

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.