You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two primary ways to run docker containers, with `docker run` and `docker compose up`.
4
4
5
-
Options everyone should know:
5
+

6
+
7
+
Docker run takes a single container image and runs a container based on it, while docker compose takes a specification of 1 or more services and can build container images for them and/or run containers from those images.
8
+
9
+
Generally `docker run` is preferable for one off quick use cases (for example those described in `04-using-3rd-party-containers`) while docker compose is preferable if you are developing a containerized application with more than one service.
10
+
11
+
## individual docker run commands
12
+
13
+
The portion of the Makefile labeled `### DOCKER CLI COMMANDS` shows the commands can would use to build and run all of these services. To build the images and then run them you can execute:
14
+
15
+
```bash
16
+
make docker-build-all
17
+
make docker-run-all
18
+
```
19
+
20
+
***Note:*** Because the Dockerfiles and application source code are located in different directories, the build commands appear more complicated than they actually are. Generally the Dockerfile would live alongside the application and the command would be more like `docker build -t <TAG> .` (and docker defaults to choosing the Dockerfile in the local directory).
21
+
22
+
You will notice that each of the run commands has a bunch of options used to ensure the configuration works properly.
23
+
24
+
- Uses the default docker bridge network
25
+
- Uses `--link` to enable easy host name for network connections
26
+
- Publishing ports (`-p` option) useful to connect to each service individually from host, but only necessary to connect to the frontend
27
+
- Named containers make it easier to reference (e.g. with link), but does require removing them to avoid naming conflict
28
+
- Restart policy allows docker to restart the container (for example if database weren't up yet causing one of the api servers to crash)
29
+
30
+
## docker compose
31
+
32
+
Using docker compose allows encoding all of the logic from the `docker build` and `docker run` commands into a single file. Docker compose also manages naming of the container images and containers, attaching to logs from all the containers at runtime, etc...
33
+
34
+
The `docker-compose.yml` file and the portion of the Makefile labeled `### DOCKER COMPOSE COMMANDS` shows how you can use docker compose to build and run the services. To build and run them you can execute
35
+
36
+
```bash
37
+
make compose-up-build
38
+
```
39
+
40
+
As you can see, this is much simpler than needing to execute all of the individual build/run commands and provides a clear way to specify the entire application stack in a single file!
41
+
42
+
# Important Configuration Options
43
+
44
+
The example shows many configuration options, but does not cover them all.
All of the command line flags/options can also be specified them within a compose file: https://docs.docker.com/compose/compose-file/
49
+
50
+
Here are a set of options everyone should know:
6
51
```
7
52
-d
8
53
--entrypoint
@@ -19,7 +64,7 @@ Options everyone should know:
19
64
--tty, -t
20
65
```
21
66
22
-
Less commonly used options, but worth knowing about:
67
+
Here are a set of less commonly used options, but still worth knowing about:
23
68
24
69
```bash
25
70
--cap-add, --cap-drop
@@ -37,29 +82,3 @@ Less commonly used options, but worth knowing about:
37
82
--security-opt
38
83
--userns
39
84
```
40
-
41
-
## Example web app
42
-
43
-
### individual docker run commands
44
-
45
-
See Makefile:
46
-
```bash
47
-
make docker-build-all
48
-
make docker-run-all
49
-
```
50
-
51
-
- Uses the default docker bridge network
52
-
- Uses `--link` to enable easy host name for network connections
53
-
- Publishing ports useful to connect to each service individually from host, but only necessary to connect to the frontend
54
-
- Named containers make it easier to reference (e.g. with link), but does require removing them to avoid naming conflict
55
-
- Restart policy allows docker to restart the container (for example if database weren't up yet causing one of the api servers to crash)
56
-
57
-
### docker compose
58
-
59
-
See Makefile:
60
-
```bash
61
-
make compose-build
62
-
make compose-up
63
-
```
64
-
65
-
Using docker compose allows encoding all of the logic from the `docker build` and `docker run` commands into a single file. Docker compose also manages naming of the container images and containers, attaching to logs from all the containers at runtime, etc...
0 commit comments