Skip to content

Commit f6adc80

Browse files
committed
remove examples for ENV key value without '='
The `ENV key value` form can be ambiguous, for example, the following defines a single env-variable (`ONE`) with value `"TWO= THREE=world"`: ENV ONE TWO= THREE=world While we cannot deprecate/remove that syntax (as it would break existing Dockerfiles), we should reduce exposure of the format in our examples. Also updating some code-blocks that were missing language-hints Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 463d98b commit f6adc80

6 files changed

Lines changed: 335 additions & 298 deletions

File tree

compose/django.md

Lines changed: 148 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi
2525

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

28-
FROM python:3
29-
ENV PYTHONUNBUFFERED 1
30-
RUN mkdir /code
31-
WORKDIR /code
32-
COPY requirements.txt /code/
33-
RUN pip install -r requirements.txt
34-
COPY . /code/
35-
36-
This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
37-
The parent image is modified by adding a new `code` directory. The parent image is further modified
38-
by installing the Python requirements defined in the `requirements.txt` file.
28+
```dockerfile
29+
FROM python:3
30+
ENV PYTHONUNBUFFERED=1
31+
RUN mkdir /code
32+
WORKDIR /code
33+
COPY requirements.txt /code/
34+
RUN pip install -r requirements.txt
35+
COPY . /code/
36+
```
37+
38+
This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
39+
The parent image is modified by adding a new `code` directory. The parent image is further modified
40+
by installing the Python requirements defined in the `requirements.txt` file.
3941

4042
4. Save and close the `Dockerfile`.
4143

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

6365
9. Add the following configuration to the file.
6466

65-
```none
66-
version: '3'
67-
68-
services:
69-
db:
70-
image: postgres
71-
environment:
72-
- POSTGRES_DB=postgres
73-
- POSTGRES_USER=postgres
74-
- POSTGRES_PASSWORD=postgres
75-
web:
76-
build: .
77-
command: python manage.py runserver 0.0.0.0:8000
78-
volumes:
79-
- .:/code
80-
ports:
81-
- "8000:8000"
82-
depends_on:
83-
- db
84-
```
85-
86-
This file defines two services: The `db` service and the `web` service.
87-
88-
> Note:
89-
>
90-
> This uses the build in development server to run your application
91-
> on port 8000. Do not use this in a production environment. For more
92-
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.
67+
```yaml
68+
version: "{{ site.compose_file_v3 }}"
69+
70+
services:
71+
db:
72+
image: postgres
73+
environment:
74+
- POSTGRES_DB=postgres
75+
- POSTGRES_USER=postgres
76+
- POSTGRES_PASSWORD=postgres
77+
web:
78+
build: .
79+
command: python manage.py runserver 0.0.0.0:8000
80+
volumes:
81+
- .:/code
82+
ports:
83+
- "8000:8000"
84+
depends_on:
85+
- db
86+
```
87+
88+
This file defines two services: The `db` service and the `web` service.
89+
90+
> Note:
91+
>
92+
> This uses the build in development server to run your application
93+
> on port 8000. Do not use this in a production environment. For more
94+
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.
9395

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

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

105-
sudo docker-compose run web django-admin startproject composeexample .
107+
```console
108+
$ sudo docker-compose run web django-admin startproject composeexample .
109+
```
106110

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

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

119-
$ ls -l
120-
drwxr-xr-x 2 root root composeexample
121-
-rw-rw-r-- 1 user user docker-compose.yml
122-
-rw-rw-r-- 1 user user Dockerfile
123-
-rwxr-xr-x 1 root root manage.py
124-
-rw-rw-r-- 1 user user requirements.txt
123+
```console
124+
$ ls -l
125125
126-
If you are running Docker on Linux, the files `django-admin` created are
127-
owned by root. This happens because the container runs as the root user.
128-
Change the ownership of the new files.
126+
drwxr-xr-x 2 root root composeexample
127+
-rw-rw-r-- 1 user user docker-compose.yml
128+
-rw-rw-r-- 1 user user Dockerfile
129+
-rwxr-xr-x 1 root root manage.py
130+
-rw-rw-r-- 1 user user requirements.txt
131+
```
129132

130-
sudo chown -R $USER:$USER .
133+
If you are running Docker on Linux, the files `django-admin` created are
134+
owned by root. This happens because the container runs as the root user.
135+
Change the ownership of the new files.
131136

132-
If you are running Docker on Mac or Windows, you should already
133-
have ownership of all files, including those generated by
134-
`django-admin`. List the files just to verify this.
137+
```console
138+
$ sudo chown -R $USER:$USER .
139+
```
135140

136-
$ ls -l
137-
total 32
138-
-rw-r--r-- 1 user staff 145 Feb 13 23:00 Dockerfile
139-
drwxr-xr-x 6 user staff 204 Feb 13 23:07 composeexample
140-
-rw-r--r-- 1 user staff 159 Feb 13 23:02 docker-compose.yml
141-
-rwxr-xr-x 1 user staff 257 Feb 13 23:07 manage.py
142-
-rw-r--r-- 1 user staff 16 Feb 13 23:01 requirements.txt
141+
If you are running Docker on Mac or Windows, you should already
142+
have ownership of all files, including those generated by
143+
`django-admin`. List the files just to verify this.
144+
145+
```console
146+
$ ls -l
147+
148+
total 32
149+
-rw-r--r-- 1 user staff 145 Feb 13 23:00 Dockerfile
150+
drwxr-xr-x 6 user staff 204 Feb 13 23:07 composeexample
151+
-rw-r--r-- 1 user staff 159 Feb 13 23:02 docker-compose.yml
152+
-rwxr-xr-x 1 user staff 257 Feb 13 23:07 manage.py
153+
-rw-r--r-- 1 user staff 16 Feb 13 23:01 requirements.txt
154+
```
143155

144156

145157
### Connect the database
146158

147159
In this section, you set up the database connection for Django.
148160

149-
1. In your project directory, edit the `composeexample/settings.py` file.
161+
1. In your project directory, edit the `composeexample/settings.py` file.
150162

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

153-
# settings.py
154-
155-
DATABASES = {
156-
'default': {
157-
'ENGINE': 'django.db.backends.postgresql',
158-
'NAME': 'postgres',
159-
'USER': 'postgres',
160-
'PASSWORD': 'postgres',
161-
'HOST': 'db',
162-
'PORT': 5432,
163-
}
165+
```python
166+
# settings.py
167+
168+
DATABASES = {
169+
'default': {
170+
'ENGINE': 'django.db.backends.postgresql',
171+
'NAME': 'postgres',
172+
'USER': 'postgres',
173+
'PASSWORD': 'postgres',
174+
'HOST': 'db',
175+
'PORT': 5432,
164176
}
177+
}
178+
```
165179

166180
These settings are determined by the
167181
[postgres](https://hub.docker.com/_/postgres) Docker image
168182
specified in `docker-compose.yml`.
169183

170-
3. Save and close the file.
171-
172-
4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.
173-
174-
```none
175-
$ docker-compose up
176-
djangosample_db_1 is up-to-date
177-
Creating djangosample_web_1 ...
178-
Creating djangosample_web_1 ... done
179-
Attaching to djangosample_db_1, djangosample_web_1
180-
db_1 | The files belonging to this database system will be owned by user "postgres".
181-
db_1 | This user must also own the server process.
182-
db_1 |
183-
db_1 | The database cluster will be initialized with locale "en_US.utf8".
184-
db_1 | The default database encoding has accordingly been set to "UTF8".
185-
db_1 | The default text search configuration will be set to "english".
186-
187-
. . .
188-
189-
web_1 | July 30, 2020 - 18:35:38
190-
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
191-
web_1 | Starting development server at http://0.0.0.0:8000/
192-
web_1 | Quit the server with CONTROL-C.
193-
```
194-
195-
At this point, your Django app should be running at port `8000` on
196-
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
197-
to `http://localhost:8000` on a web browser to see the Django
198-
welcome page. If you are using [Docker Machine](../machine/overview.md),
199-
then `docker-machine ip MACHINE_VM` returns the Docker host IP
200-
address, to which you can append the port (`<Docker-Host-IP>:8000`).
201-
202-
![Django example](images/django-it-worked.png)
203-
204-
> Note:
205-
>
206-
> On certain platforms (Windows 10), you might need to
207-
edit `ALLOWED_HOSTS` inside `settings.py` and add your Docker host name
208-
or IP address to the list. For demo purposes, you can set the value to:
209-
>
210-
> ALLOWED_HOSTS = ['*']
211-
>
212-
> This value is **not** safe for production usage. Refer to the
213-
[Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.
214-
215-
5. List running containers.
216-
217-
In another terminal window, list the running Docker processes with the `docker container ls` command.
218-
219-
```none
220-
$ docker ps
221-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
222-
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
223-
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
224-
225-
```
226-
227-
6. Shut down services and clean up by using either of these methods:
184+
3. Save and close the file.
185+
186+
4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.
187+
188+
```console
189+
$ docker-compose up
190+
191+
djangosample_db_1 is up-to-date
192+
Creating djangosample_web_1 ...
193+
Creating djangosample_web_1 ... done
194+
Attaching to djangosample_db_1, djangosample_web_1
195+
db_1 | The files belonging to this database system will be owned by user "postgres".
196+
db_1 | This user must also own the server process.
197+
db_1 |
198+
db_1 | The database cluster will be initialized with locale "en_US.utf8".
199+
db_1 | The default database encoding has accordingly been set to "UTF8".
200+
db_1 | The default text search configuration will be set to "english".
201+
202+
. . .
203+
204+
web_1 | July 30, 2020 - 18:35:38
205+
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
206+
web_1 | Starting development server at http://0.0.0.0:8000/
207+
web_1 | Quit the server with CONTROL-C.
208+
```
209+
210+
At this point, your Django app should be running at port `8000` on
211+
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
212+
to `http://localhost:8000` on a web browser to see the Django
213+
welcome page. If you are using [Docker Machine](../machine/overview.md),
214+
then `docker-machine ip MACHINE_VM` returns the Docker host IP
215+
address, to which you can append the port (`<Docker-Host-IP>:8000`).
216+
217+
![Django example](images/django-it-worked.png)
218+
219+
> Note:
220+
>
221+
> On certain platforms (Windows 10), you might need to edit `ALLOWED_HOSTS`
222+
> inside `settings.py` and add your Docker host name or IP address to the list.
223+
> For demo purposes, you can set the value to:
224+
>
225+
> ALLOWED_HOSTS = ['*']
226+
>
227+
> This value is **not** safe for production usage. Refer to the
228+
> [Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.
229+
230+
5. List running containers.
231+
232+
In another terminal window, list the running Docker processes with the `docker container ls` command.
233+
234+
```console
235+
$ docker ps
236+
237+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238+
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
239+
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
240+
```
241+
242+
6. Shut down services and clean up by using either of these methods:
228243

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

232-
```none
247+
```console
233248
Gracefully stopping... (press Ctrl+C again to force)
234249
Killing test_web_1 ... done
235250
Killing test_db_1 ... done
236251
```
237252

238253
* 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.
239254

240-
```none
255+
```console
241256
vmb at mymachine in ~/sandbox/django
242257
$ docker-compose down
258+
243259
Stopping django_web_1 ... done
244260
Stopping django_db_1 ... done
245261
Removing django_web_1 ... done

0 commit comments

Comments
 (0)