Skip to content

docker-compose.yml and other files to launch develop environment for django with Nginx and PostgreSQL

License

Notifications You must be signed in to change notification settings

hiro2620/django-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-docker

docker-compose.yml and other files to launch develop environment for django with Nginx and PostgreSQL

Usage

The instructions below are for Linux. For Mac or Windows, read accordingly.

STEP 1 Clone repo

$ cd dir-to-work
$ git clone https://github.com/hiro2620/django-docker.git
$ cd django-docker

*alias.sh is just for running long comannd after django setup has finished, and never used while setting up environment.

STEP 2 Build Docker containers

It takes a few minuits, depending on your internet speed.

$ docker-compose up

note: '-d' is not necessary.

Wait until building is finished with messages like below.

db_1      | 2020-05-20 05:39:41.098 UTC [1] LOG:  database system is ready to accept connections

Them, run following to stop container.

$ docker-compose down

STEP 3 Start Django project

Run

$ docker-compose run --rm python django-admin startproject app .

Be careful not to forget '.' at the end.

After that, django files are in ./django-root

Then, access https://localhost:8010 on yout browser to check if Django project works fine.

screenshot

STEP 4 Allow django to connect PostgreSQL

Generated files in ./django-root/ is unwritable, so run

$ sudo chmod -R 777 django-root

to edit them.

Then, open ./django-root/app/settings.py and replace

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

with

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': '5432',
    }
}

Run following commands to migrate database.

docker-compose run --rm python python3 manage.py makemigrations
docker-compose run --rm python python3 manage.py migrate

After migration, run collectstatic(below) to apply css.

docker-compose run --rm python python3 manage.py collectstatic

And run following command to create superuser.

docker-compose run --rm python python3 manage.py createsuperuser

Then, access `https://localhost:8010/admin. If it works fine, Django connected Postgre SQL successfully, and all step has been done.

NOTE

How to change port to access Django page?

  • Edit line 7 in docker-compose.yml
ports:
- "8010:8000"

this 8010 is the port that is used to access, so please edit it.

How to start app or collect static in django?

  • Run following comannd on console on host PC.
docker-compose run --rm python python3 manage.py startapp # To run startapp
docker-compose run --rm python python3 manage.py collectstatic #To run collectstatic

This is also noted on alias.sh on the same directory to docker-compose.yml

How to stop containers started by $ docker-compose up -d ?

  • Run
$ docker-compose down

Licence

MIT

Author

hiro2620

Thank you.

About

docker-compose.yml and other files to launch develop environment for django with Nginx and PostgreSQL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published