docker-compose.yml and other files to launch develop environment for django with Nginx and PostgreSQL
The instructions below are for Linux. For Mac or Windows, read accordingly.
$ 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.
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
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.
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.
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
Thank you.