An open API service indexing awesome lists of open source software.

https://github.com/devwhodevs/django-postgresql-docker-boilerplate

Boilerplate for quick warm up of Python django & postgres projects
https://github.com/devwhodevs/django-postgresql-docker-boilerplate

Last synced: 2 months ago
JSON representation

Boilerplate for quick warm up of Python django & postgres projects

Awesome Lists containing this project

README

          

# Create a Django project

In this step, you create a Django starter project by building the image from the build context defined in the previous procedure.

Change to the root of your project directory.

Create the Django project by running the docker-compose run command as follows.

```sh
$ sudo docker-compose run web django-admin startproject projectname .
```

This instructs Compose to run django-admin startproject composeexample in a container, using the web service’s image and configuration. Because the web image doesn’t exist yet, Compose builds it from the current directory, as specified by the build: . line in docker-compose.yml.

Once the web service image is built, Compose runs it and executes the django-admin startproject command in the container. This command instructs Django to create a set of files and directories representing a Django project.

After the docker-compose command completes, list the contents of your project.

```sh
$ ls -l

drwxr-xr-x 2 root root composeexample
-rw-rw-r-- 1 user user docker-compose.yml
-rw-rw-r-- 1 user user Dockerfile
-rwxr-xr-x 1 root root manage.py
-rw-rw-r-- 1 user user requirements.txt
```

If you are running Docker on Linux, the files django-admin created are owned by root. This happens because the container runs as the root user. Change the ownership of the new files.

```sh
$ sudo chown -R $USER:$USER .
```

If you are running Docker on Mac or Windows, you should already have ownership of all files, including those generated by django-admin. List the files just to verify this.

```sh
$ ls -l

total 32
-rw-r--r-- 1 user staff 145 Feb 13 23:00 Dockerfile
drwxr-xr-x 6 user staff 204 Feb 13 23:07 composeexample
-rw-r--r-- 1 user staff 159 Feb 13 23:02 docker-compose.yml
-rwxr-xr-x 1 user staff 257 Feb 13 23:07 manage.py
-rw-r--r-- 1 user staff 16 Feb 13 23:01 requirements.txt
```

# Connect the database
In this section, you set up the database connection for Django.

In your project directory, edit the composeexample/settings.py file.

Replace the DATABASES = ... with the following:

## settings.py
```py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
```

These settings are determined by the postgres Docker image specified in docker-compose.yml.

Full guide can be found at DockerHub quickstart.

https://docs.docker.com/compose/django/