Skip to content

Commit ffc99ea

Browse files
updated/improved Compose samples (docker#3438)
* updated/improved Compose samples Signed-off-by: Victoria Bialas <[email protected]> * linked some more basic docker-compose commands Signed-off-by: Victoria Bialas <[email protected]> * coypedit Signed-off-by: Victoria Bialas <[email protected]>
1 parent 5b6345c commit ffc99ea

File tree

4 files changed

+121
-77
lines changed

4 files changed

+121
-77
lines changed

compose/django.md

Lines changed: 109 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,24 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi
6161
expose. See the [`docker-compose.yml` reference](compose-file.md) for more
6262
information on how this file works.
6363

64-
9. Add the following configuration to the file.
65-
66-
version: '2'
67-
services:
68-
db:
69-
image: postgres
70-
web:
71-
build: .
72-
command: python3 manage.py runserver 0.0.0.0:8000
73-
volumes:
74-
- .:/code
75-
ports:
76-
- "8000:8000"
77-
depends_on:
78-
- db
64+
9. Add the following configuration to the file.
65+
66+
```none
67+
version: '3'
68+
69+
services:
70+
db:
71+
image: postgres
72+
web:
73+
build: .
74+
command: python3 manage.py runserver 0.0.0.0:8000
75+
volumes:
76+
- .:/code
77+
ports:
78+
- "8000:8000"
79+
depends_on:
80+
- db
81+
```
7982
8083
This file defines two services: The `db` service and the `web` service.
8184
@@ -87,7 +90,8 @@ In this step, you create a Django starter project by building the image from the
8790
8891
1. Change to the root of your project directory.
8992
90-
2. Create the Django project using the `docker-compose` command.
93+
2. Create the Django project by running
94+
the [docker-compose run](/compose/reference/run/) command as follows.
9195
9296
docker-compose run web django-admin.py startproject composeexample .
9397
@@ -110,15 +114,15 @@ In this step, you create a Django starter project by building the image from the
110114
-rwxr-xr-x 1 root root manage.py
111115
-rw-rw-r-- 1 user user requirements.txt
112116
113-
If you are running Docker on Linux, the files `django-admin` created are owned
114-
by root. This happens because the container runs as the root user. Change the
115-
ownership of the new files.
117+
If you are running Docker on Linux, the files `django-admin` created are
118+
owned by root. This happens because the container runs as the root user.
119+
Change the ownership of the new files.
116120
117121
sudo chown -R $USER:$USER .
118122
119-
If you are running Docker on Mac or Windows, you should already have ownership
120-
of all files, including those generated by `django-admin`. List the files just
121-
verify this.
123+
If you are running Docker on Mac or Windows, you should already
124+
have ownership of all files, including those generated by
125+
`django-admin`. List the files just to verify this.
122126
123127
$ ls -l
124128
total 32
@@ -133,9 +137,9 @@ In this step, you create a Django starter project by building the image from the
133137
134138
In this section, you set up the database connection for Django.
135139
136-
1. In your project directory, edit the `composeexample/settings.py` file.
140+
1. In your project directory, edit the `composeexample/settings.py` file.
137141
138-
2. Replace the `DATABASES = ...` with the following:
142+
2. Replace the `DATABASES = ...` with the following:
139143
140144
DATABASES = {
141145
'default': {
@@ -148,53 +152,92 @@ In this section, you set up the database connection for Django.
148152
}
149153
150154
These settings are determined by the
151-
[postgres](https://hub.docker.com/_/postgres/) Docker image
155+
[postgres](https://store.docker.com/images/postgres) Docker image
152156
specified in `docker-compose.yml`.
153157
154-
3. Save and close the file.
155-
156-
4. Run the `docker-compose up` command.
157-
158-
$ docker-compose up
159-
Starting composepractice_db_1...
160-
Starting composepractice_web_1...
161-
Attaching to composepractice_db_1, composepractice_web_1
162-
...
163-
db_1 | PostgreSQL init process complete; ready for start up.
164-
...
165-
db_1 | LOG: database system is ready to accept connections
166-
db_1 | LOG: autovacuum launcher started
167-
..
168-
web_1 | Django version 1.8.4, using settings 'composeexample.settings'
169-
web_1 | Starting development server at http://0.0.0.0:8000/
170-
web_1 | Quit the server with CONTROL-C.
171-
172-
At this point, your Django app should be running at port `8000` on your
173-
Docker host. If you are using a Docker Machine VM, you can use the
174-
`docker-machine ip MACHINE_NAME` to get the IP address.
158+
3. Save and close the file.
159+
160+
4. Run the [docker-compose up](/compose/reference/up/) command from the top level directory for your project.
161+
162+
```none
163+
$ docker-compose up
164+
djangosample_db_1 is up-to-date
165+
Creating djangosample_web_1 ...
166+
Creating djangosample_web_1 ... done
167+
Attaching to djangosample_db_1, djangosample_web_1
168+
db_1 | The files belonging to this database system will be owned by user "postgres".
169+
db_1 | This user must also own the server process.
170+
db_1 |
171+
db_1 | The database cluster will be initialized with locale "en_US.utf8".
172+
db_1 | The default database encoding has accordingly been set to "UTF8".
173+
db_1 | The default text search configuration will be set to "english".
174+
175+
. . .
176+
177+
web_1 | May 30, 2017 - 21:44:49
178+
web_1 | Django version 1.11.1, using settings 'composeexample.settings'
179+
web_1 | Starting development server at http://0.0.0.0:8000/
180+
web_1 | Quit the server with CONTROL-C.
181+
```
182+
183+
At this point, your Django app should be running at port `8000` on
184+
your Docker host. On Docker for Mac and Docker for Windows, go
185+
to `http://localhost:8000` on a web browser to see the Django
186+
welcome page. If you are using [Docker Machine](/machine/overview.md),
187+
then `docker-machine ip MACHINE_VM` returns the Docker host IP
188+
address, to which you can append the port (`<Docker-Host-IP>:8000`).
175189
176190
![Django example](images/django-it-worked.png)
177191
178-
> **Note:**
179-
> On certain platforms (Windows 10), you may additionally need to edit `ALLOWED_HOSTS`
180-
> inside settings.py and add your Docker hostname or IP to the list. For demo
181-
> purposes, you may set the value to:
182-
>
183-
> ALLOWED_HOSTS = ['*']
184-
>
185-
> Please note this value is **not** safe for production usage. Refer to the
186-
> [Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts)
187-
> for more information.
188-
189-
5. Clean up: Shut down containers with CONTROL-C.
190-
191-
```
192-
Gracefully stopping... (press Ctrl+C again to force)
193-
Killing test_web_1 ... done
194-
Killing test_db_1 ... done
195-
```
196-
197-
It's safe to `rm -rf` your project directory.
192+
> Note:
193+
>
194+
> On certain platforms (Windows 10), you might need to
195+
edit `ALLOWED_HOSTS` inside `settings.py` and add your Docker host name
196+
or IP address to the list. For demo purposes, you can set the value to:
197+
>
198+
> ALLOWED_HOSTS = ['*']
199+
>
200+
> Please note this value is **not** safe for production usage. Refer to the
201+
[Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.
202+
{: .note-vanilla}
203+
204+
5. List running containers.
205+
206+
In another terminal window, list the running Docker processes with the `docker ps` command.
207+
208+
```none
209+
$ docker ps
210+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
211+
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
212+
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
213+
214+
```
215+
216+
6. Shut down services and clean up by using either of these methods:
217+
218+
* Stop the application by typing `Ctrl-C`
219+
in the same shell in where you started it:
220+
221+
```none
222+
Gracefully stopping... (press Ctrl+C again to force)
223+
Killing test_web_1 ... done
224+
Killing test_db_1 ... done
225+
```
226+
227+
* Or, for a more elegant shutdown, switch to a different shell, and run [docker-compose down](/compose/reference/down/) from the top level of your Django sample project directory.
228+
229+
```none
230+
vmb at mymachine in ~/sandbox/django
231+
$ docker-compose down
232+
Stopping django_web_1 ... done
233+
Stopping django_db_1 ... done
234+
Removing django_web_1 ... done
235+
Removing django_web_run_1 ... done
236+
Removing django_db_1 ... done
237+
Removing network django_default
238+
```
239+
240+
Once you've shut down the app, you can safely remove the Django project directory (for example, `rm -rf django`).
198241
199242
## More Compose documentation
200243
-6.74 KB
Loading

compose/rails.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ to link them together and expose the web app's port.
6666
### Build the project
6767

6868
With those four files in place, you can now generate the Rails skeleton app
69-
using `docker-compose run`:
69+
using [docker-compose run](/compose/reference/run/):
7070

7171
docker-compose run web rails new . --force --database=postgresql
7272

@@ -139,7 +139,7 @@ test:
139139

140140
You can now boot the app with:
141141

142-
docker-compose up
142+
[docker-compose up](/compose/reference/up/)
143143

144144
If all's well, you should see some PostgreSQL output, and then—after a few
145145
seconds—the familiar refrain:
@@ -178,10 +178,10 @@ Docker host IP address, to which you can append the port
178178

179179
### Stop the application
180180

181-
To stop the application, run `docker-compose down` in your project directory.
182-
You can use the same terminal window in which you started the database, or
183-
another one where you have access to a command prompt. This is a clean way to
184-
stop the application.
181+
To stop the application, run [docker-compose down](/compose/reference/down/) in
182+
your project directory. You can use the same terminal window in which you
183+
started the database, or another one where you have access to a command prompt.
184+
This is a clean way to stop the application.
185185

186186
```none
187187
vmb at snapair in ~/sandbox/rails

compose/wordpress.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Compose to set up and run WordPress. Before starting, you'll need to have
3434
mount for data persistence:
3535

3636
```none
37-
version: '2'
37+
version: '3'
3838
3939
services:
4040
db:
@@ -75,8 +75,9 @@ Compose to set up and run WordPress. Before starting, you'll need to have
7575
7676
Now, run `docker-compose up -d` from your project directory.
7777
78-
This pulls the needed images, and starts the wordpress and database
79-
containers, as shown in the example below.
78+
This runs [docker-compose up](/compose/reference/up/) in detached mode, pulls
79+
the needed images, and starts the wordpress and database containers, as shown in
80+
the example below.
8081
8182
$ docker-compose up -d
8283
Creating network "my_wordpress_default" with the default driver
@@ -127,8 +128,8 @@ browser.
127128
128129
### Shutdown and Cleanup
129130
130-
The command `docker-compose down` removes the containers and default network,
131-
but preserves your Wordpress database.
131+
The command [docker-compose down](/compose/reference/down/) removes the
132+
containers and default network, but preserves your Wordpress database.
132133
133134
The command `docker-compose down --volumes` removes the containers, default
134135
network, and the Wordpress database.

0 commit comments

Comments
 (0)