Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explanation for docker compose up and change version top-level element to name #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 28 additions & 9 deletions docs/part-2/section-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Even with a simple image, we've already been dealing with plenty of command line

Next we will switch to a tool called [Docker Compose](https://docs.docker.com/compose/) to manage these. Docker Compose used to be a separate tool but now it is integrated into Docker and can be used like the rest of the Docker commands.

Docker Compose is designed to simplify running multi-container applications using a single command.
Docker Compose is designed to simplify running multi-container applications using a single command `docker compose [-f <arg>...] [options] [COMMAND] [ARGS...]`.

Assume that we are in the folder where we have our Dockerfile with the following content:

Expand All @@ -25,16 +25,14 @@ ENTRYPOINT ["/usr/local/bin/yt-dlp"]
Let us now create a file called `docker-compose.yml`:

```yaml
version: '3.8'
name: yt-dlp-app

services:
yt-dlp-ubuntu:
image: <username>/<repositoryname>
build: .
```

The version setting is not very strict, it just needs to be above 2 because otherwise the syntax is significantly different. See <https://docs.docker.com/compose/compose-file/> for more info.

The value of the key `build` can be a file system path (in the example it is the current directory `.`) or an object with keys `context` and `dockerfile`, see the [documentation](https://docs.docker.com/compose/compose-file/build/) for more

Now we can build and push with just these commands:
Expand All @@ -49,7 +47,7 @@ $ docker compose push
To run the image as we did previously, we will need to add the volume bind mounts. Volumes in Docker Compose are defined with the following syntax `location-in-host:location-in-container`. Compose can work without an absolute path:

```yaml
version: '3.8'
name: yt-dlp-app

services:

Expand All @@ -67,6 +65,27 @@ We can also give the container a name it will use when running with container_na
$ docker compose run yt-dlp-ubuntu https://imgur.com/JY5tHqr
```

## Key Commands in Docker Compose

To start all the services defined in the `docker-compose.yaml` file:
```console
$ docker compose up
```
To stop and remove the running services:
```console
$ docker compose down
```
If you want to monitor the output of the running containers and debug issues, we can view the logs with:
```console
$ docker compose logs
```
To lists all the services along with their current status:
```console
$ docker compose ps
```

Full list of Compose CLI Commands can be found [here](https://docs.docker.com/reference/cli/docker/compose/).

## Exercise 2.1

:::info Exercise 2.1
Expand All @@ -78,7 +97,7 @@ $ docker compose run yt-dlp-ubuntu https://imgur.com/JY5tHqr
Create a docker-compose.yml file that starts `devopsdockeruh/simple-web-service` and saves the logs into your
filesystem.

Submit the docker-compose.yml, and make sure that it works simply by running `docker compose up` if the log file exists.
Submit the docker-compose.yml, and make sure that it works simply by running `docker compose up -d` if the log file exists.


:::
Expand Down Expand Up @@ -106,7 +125,7 @@ $ docker container rm 736ab83847bb
Let's create a new folder and a Docker Compose file `whoami/docker-compose.yml` from the command line options.

```yaml
version: '3.8'
name: whoami-app

services:
whoami:
Expand All @@ -125,7 +144,7 @@ $ curl localhost:8000
Environment variables can also be given to the containers in Docker Compose as follows:

```yaml
version: '3.8'
name: myapp

services:
backend:
Expand All @@ -147,7 +166,7 @@ Note that there are also [other](https://docs.docker.com/compose/environment-var

Create a docker-compose.yml, and use it to start the service so that you can use it with your browser.

Submit the docker-compose.yml, and make sure that it works simply by running `docker compose up`
Submit the docker-compose.yml, and make sure that it works simply by running `docker compose up -d`

:::

Expand Down
10 changes: 5 additions & 5 deletions docs/part-2/section-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ It is also possible to define the network manually in a Docker Compose file. A m
Let us now have a look how a network is defined in docker-compose.yml:

```yaml
version: "3.8"
name: database-network-app

services:
db:
Expand All @@ -68,7 +68,7 @@ As can be seen, services are configured to use a network by adding `networks` in
Establishing a connection to an external network (that is, a network defined in another docker-compose.yml, or by some other means) is done as follows:

```yaml
version: "3.8"
name: database-network-app

services:
db:
Expand All @@ -85,7 +85,7 @@ networks:
By default all services are added to a network called `default`. The default network can be configured and this makes it possible to connect to an external network by default as well:

```yaml
version: "3.8"
name: database-network-app

services:
db:
Expand Down Expand Up @@ -159,7 +159,7 @@ In a server environment you'd often have a [load balancer](https://en.wikipedia.
Let's add the nginx-proxy to our compose file and remove the port bindings from the whoami service. We'll mount our [docker.sock](https://stackoverflow.com/questions/35110146/can-anyone-explain-docker-sock) (the socket that is used to communicate with the [Docker Daemon](https://docs.docker.com/engine/reference/commandline/dockerd/)) inside of the container in `:ro` read-only mode:

```yaml
version: "3.8"
name: whoami-app

services:
whoami:
Expand Down Expand Up @@ -193,7 +193,7 @@ It's "working", but the Nginx just doesn't know which service we want. The `ngin
The domain `colasloth.com` is configured so that all subdomains point to `127.0.0.1`. More information about how this works can be found at [colasloth.github.io](https://colasloth.github.io), but in brief it's a simple DNS "hack". Several other domains serving the same purpose exist, such as `localtest.me`, `lvh.me`, and `vcap.me`, to name a few. In any case, let's use `colasloth.com` here:

```yaml
version: "3.8"
name: whoami-app

services:
whoami:
Expand Down
6 changes: 3 additions & 3 deletions docs/part-2/section-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ From the section _Environment Variables_ we can see that all versions can use `R
In <https://hub.docker.com/_/postgres> there's a sample compose file under the section "via docker-compose or docker stack deploy". Let's strip that down as follows

```yaml
version: "3.8"
name: postgres-app

services:
db:
Expand Down Expand Up @@ -85,7 +85,7 @@ Instead of the randomly named volume we better define one explicitly.
Let us change the definition as follows:

```yaml
version: "3.8"
name: postgres-app

services:
db:
Expand Down Expand Up @@ -150,7 +150,7 @@ As the [documentation](https://hub.docker.com/_/redmine) mentions, the image cre
With that in mind, our configuration changes to this:

```yaml
version: "3.8"
name: postgres-app

services:
db:
Expand Down
2 changes: 1 addition & 1 deletion docs/part-2/section-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RUN npm install

**docker-compose.yml**
```yaml
version: '3.8'
name: node-app

services:
node-dev-env:
Expand Down
2 changes: 1 addition & 1 deletion docs/part-3/section-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Note that now anyone with access to your Docker Hub also has access to your PC t
Watchtower can be run eg. using the following Docker Compose file:

```yaml
version: "3.8"
name: watchtower-app

services:
watchtower:
Expand Down