Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 148 additions & 132 deletions compose/django.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi

3. Add the following content to the `Dockerfile`.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
The parent image is modified by adding a new `code` directory. The parent image is further modified
by installing the Python requirements defined in the `requirements.txt` file.
```dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
```

This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
The parent image is modified by adding a new `code` directory. The parent image is further modified
by installing the Python requirements defined in the `requirements.txt` file.

4. Save and close the `Dockerfile`.

Expand All @@ -62,34 +64,34 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi

9. Add the following configuration to the file.

```none
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
```

This file defines two services: The `db` service and the `web` service.

> Note:
>
> This uses the build in development server to run your application
> on port 8000. Do not use this in a production environment. For more
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.
```yaml
version: "{{ site.compose_file_v3 }}"

services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
```

This file defines two services: The `db` service and the `web` service.

> Note:
>
> This uses the build in development server to run your application
> on port 8000. Do not use this in a production environment. For more
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.

10. Save and close the `docker-compose.yml` file.

Expand All @@ -102,7 +104,9 @@ In this step, you create a Django starter project by building the image from the
2. Create the Django project by running
the [docker-compose run](reference/run.md) command as follows.

sudo docker-compose run web django-admin startproject composeexample .
```console
$ sudo docker-compose run web django-admin startproject composeexample .
```

This instructs Compose to run `django-admin startproject composeexample`
in a container, using the `web` service's image and configuration. Because
Expand All @@ -116,130 +120,142 @@ the [docker-compose run](reference/run.md) command as follows.

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

$ 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
```console
$ ls -l

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.
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
```

sudo chown -R $USER:$USER .
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.

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.
```console
$ sudo chown -R $USER:$USER .
```

$ 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
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.

```console
$ 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.

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

2. Replace the `DATABASES = ...` with the following:

# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
```python
# settings.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](https://hub.docker.com/_/postgres) Docker image
specified in `docker-compose.yml`.

3. Save and close the file.

4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.

```none
$ docker-compose up
djangosample_db_1 is up-to-date
Creating djangosample_web_1 ...
Creating djangosample_web_1 ... done
Attaching to djangosample_db_1, djangosample_web_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".

. . .

web_1 | July 30, 2020 - 18:35:38
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
```

At this point, your Django app should be running at port `8000` on
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
to `http://localhost:8000` on a web browser to see the Django
welcome page. If you are using [Docker Machine](../machine/overview.md),
then `docker-machine ip MACHINE_VM` returns the Docker host IP
address, to which you can append the port (`<Docker-Host-IP>:8000`).

![Django example](images/django-it-worked.png)

> Note:
>
> On certain platforms (Windows 10), you might need to
edit `ALLOWED_HOSTS` inside `settings.py` and add your Docker host name
or IP address to the list. For demo purposes, you can set the value to:
>
> ALLOWED_HOSTS = ['*']
>
> This value is **not** safe for production usage. Refer to the
[Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.

5. List running containers.

In another terminal window, list the running Docker processes with the `docker container ls` command.

```none
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1

```

6. Shut down services and clean up by using either of these methods:
3. Save and close the file.

4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.

```console
$ docker-compose up

djangosample_db_1 is up-to-date
Creating djangosample_web_1 ...
Creating djangosample_web_1 ... done
Attaching to djangosample_db_1, djangosample_web_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".

. . .

web_1 | July 30, 2020 - 18:35:38
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
```

At this point, your Django app should be running at port `8000` on
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
to `http://localhost:8000` on a web browser to see the Django
welcome page. If you are using [Docker Machine](../machine/overview.md),
then `docker-machine ip MACHINE_VM` returns the Docker host IP
address, to which you can append the port (`<Docker-Host-IP>:8000`).

![Django example](images/django-it-worked.png)

> Note:
>
> On certain platforms (Windows 10), you might need to edit `ALLOWED_HOSTS`
> inside `settings.py` and add your Docker host name or IP address to the list.
> For demo purposes, you can set the value to:
>
> ALLOWED_HOSTS = ['*']
>
> This value is **not** safe for production usage. Refer to the
> [Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.

5. List running containers.

In another terminal window, list the running Docker processes with the `docker container ls` command.

```console
$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
```

6. Shut down services and clean up by using either of these methods:

* Stop the application by typing `Ctrl-C`
in the same shell in where you started it:

```none
```console
Gracefully stopping... (press Ctrl+C again to force)
Killing test_web_1 ... done
Killing test_db_1 ... done
```

* Or, for a more elegant shutdown, switch to a different shell, and run [docker-compose down](reference/down.md) from the top level of your Django sample project directory.

```none
```console
vmb at mymachine in ~/sandbox/django
$ docker-compose down

Stopping django_web_1 ... done
Stopping django_db_1 ... done
Removing django_web_1 ... done
Expand Down
Loading