Best Docker document for Docker
Best Docker document for Docker
Best Docker document for Docker
jim stafford
1.1. Goals
You will learn:
• how to implement a network of services for development and testing using Docker Compose
• how to operate a Docker Compose network lifecycle and how to interact with the running
instances
1.2. Objectives
At the conclusion of this lecture and related exercises, you will be able to:
1. identify the purpose of Docker Compose for implementing a network of virtualized services
2. create a Docker Compose file that defines a network of services and their dependencies
4. perform Docker Compose lifecycle commands to build, start, and stop a network of services
6. instantiate back-end services for use with the follow-on database lectures
1
Chapter 2. Development and Integration
Testing with Real Resources
To date, we have primarily worked with a single Web application. In the follow-on lectures we will
soon need to add back-end database resources.
• MongoDB
• Postgres
2
Manually Starting Images
③ using our example Spring Boot Web application; it does not yet use the databases
• we start integrating the API image with the individual resources through networking
• we want multiple instances of the test running concurrently on the same machine without
interference with one another
Lets not mess with manual Docker commands for too long! There are better ways to do this with
Docker Compose.
3
Chapter 3. Docker Compose
DockerCompose is a tool for defining and running multi-container Docker applications. With
Docker Compose, we can:
4
Chapter 4. Docker Compose Configuration
File
The Docker Compose (configuration) file is based on YAML — which uses a concise way to express
information based on indentation and firm symbol rules. Assuming we have a simple network of
three (3) services, we can limit our definition to a file version and individual services.
docker-compose.yml Shell
version: '3.8'
services:
mongo:
...
postgres:
...
api:
...
• version - informs the docker-compose binary what features could be present within the file. I
have shown a recent version of 3.8 but our use of the file will be very basic and could likely be
set to 3 or as low as 2.
• services - lists the individual nodes and their details. Each node is represented by a Docker
image and we will look at a few examples next.
mongo:
image: mongo:4.4.0-bionic
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: secret
# ports: ①
# - "27017" ②
# - "27017:27017" ③
# - "37001:27017" ④
# - "127.0.0.1:37001:27017" ⑤
5
④ 37001 external and 27017` internal
• image - identifies the name and tag of the Docker image. This will be automatically downloaded
if not already available locally
• environment - defines specific environment variables to be made available when running the
image.
◦ VAR by itself passes in variable VAR with whatever the value of VAR has been assigned to be in
the environment (i.e., environment variable or from environment file).
• ports - maps a container port to a host port with the syntax "host interface:host
port#:container port#"
◦ no ports defined means the container port# that do exist are only accessible within the
network of services defined within the file
postgres:
image: postgres:12.3-alpine
# ports: ①
# - "5432:5432"
environment:
POSTGRES_PASSWORD: secret
6
become a client of the other three services.
api:
build:
context: .
dockerfile: Dockerfile.layered
image: docker-hello-example:layered
ports:
- "${API_PORT:-8080}:8080"
depends_on:
- mongo
- postgres
environment:
- spring.profiles.active=integration
- MONGODB_URI=mongodb://admin:secret@mongo:27017/votes_db?authSource=admin
- DATABASE_URL=postgres://postgres:secret@postgres:5432/postgres
• build - identifies a source Dockerfile that can build the image for this service
◦ dockerfile - defines the specific name of the Dockerfile (optional in this case)
• image - identifies the name and tag used for the built image
• ports - using a ${variable:-default} reference so that we have option to expose the container
port# 8080 to a dynamically assigned host port# during testing. If API_PORT is not resolved to a
value, the default 8080 value will be used.
◦ API is not yet using the databases, but these URLs are consistent with what will be
encountered when deployed to Heroku.
◦ if only the environment variable name is supplied, it’s value will not be defined here and
the value from external sources will be passed at runtime
$ docker-compose build
7
postgres uses an image, skipping
mongo uses an image, skipping
Building api
[+] Building 0.2s (13/13) FINISHED
=> => naming to docker.io/library/docker-hello-example:layered
..
After the first start, a re-build is only performed using the build command or when the --build
option.
You will notice that no ports were assigned to the unassigned mongo and postgres services. However,
the given shown port# in the output is available to the other hosts within that Docker network. If
we don’t need mongo or postgres accessible to the host’s network — we are good. The api service was
assigned a variable (value 1234) port# — which is accessible to the host’s network.
$ docker-compose ps
Name State Ports
----------------------------------------------------------------------------------
docker-hello-example_api_1 Up 0.0.0.0:1234->8080/tcp,:::1234->8080/tcp
docker-hello-example_mongo_1 Up 27017/tcp
docker-hello-example_postgres_1 Up 5432/tcp
$ curl http://localhost:1234/api/hello?name=jim
hello, jim
8
Example Compose Override File
version: '3.8'
services:
mongo:
ports:
- "27017:27017"
postgres:
ports:
- "5432:5432"
Notice how the container port# is now mapped according to how the override file has specified.
$ unset API_PORT
$ docker-compose down
$ docker-compose up -d
$ docker-compose ps
Name State Ports
--------------------------------------------------------------------------------------
docker-hello-example_api_1 Up 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp
docker-hello-example_mongo_1 Up 0.0.0.0:27017->27017/tcp,:::27017->27017/tcp
docker-hello-example_postgres_1 Up 0.0.0.0:5432->5432/tcp,:::5432->5432/tcp
$ ls docker-compose.*
docker-compose.override.yml docker-compose.yml
$ docker-compose up ①
9
4.8. Multiple Compose Files
Docker Compose will accept a series of explicit -f file specifications that are processed from left to
right. This allows you to name your own override files.
① starting network in foreground with two configuration files, with the left-most file being
specialized by the right-most file
1. as an environment variable
2. in an environment file
3. when the variable is named and set to a value in the Compose file
Docker Compose will use .env as its default environment file. A file like this would normally not be
checked into CM since it might have real credentials, etc.
$ cat .gitignore
...
.env
API_PORT=9090
You can also explicitly name an environment file to use. The following is explicitly applying the
alt-env environment file — thus bypassing the .env file.
$ cat alt-env
API_PORT=9999
10
dockercompose-votes-api:latest 0.0.0.0:9999->8080/tcp
...
① starting network in background with an alternate environment file mapping API port to 9999
11
Chapter 5. Docker Compose Commands
5.1. Build Source Images
With the docker-compose.yml file defined — we can use that to control the build of our source
images. Notice in the example below that it is building the same image we built in the previous
lecture.
$ docker-compose build
postgres uses an image, skipping
mongo uses an image, skipping
Building api
[+] Building 0.2s (13/13) FINISHED
=> => naming to docker.io/library/docker-hello-example:layered
$ docker-compose up
docker-hello-example_mongo_1 is up-to-date
docker-hello-example_postgres_1 is up-to-date
Recreating docker-hello-example_api_1 ... done
Attaching to docker-hello-example_mongo_1, docker-hello-example_postgres_1, docker-
hello-example_api_1
We can trigger a new build with the --build option. If there is no image present, a build will be
triggered automatically but will not be automatically reissued on subsequent commands without
supplying the --build option.
pwd
.../svc-container/docker-hello-example
$ docker-compose up
12
docker-hello-example_mongo_1 is up-to-date
docker-hello-example_postgres_1 is up-to-date
Recreating docker-hello-example_api_1 ... done
We can explicitly set the project name using the -p option. This can be helpful if the parent
directory happens to be something generic — like target or src/test/resources.
$ docker-compose -p foo up ①
Creating network "foo_default" with the default driver
Creating foo_postgres_1 ... done ②
Creating foo_mongo_1 ... done
Creating foo_api_1 ... done
Attaching to foo_postgres_1, foo_mongo_1, foo_api_1
$ docker-compose up -d
Creating network "docker-hello-example_default" with the default driver
Creating docker-hello-example_postgres_1 ... done
Creating docker-hello-example_mongo_1 ... done
Creating docker-hello-example_api_1 ... done
$ ①
① -d option starts all services in the background and returns us to our shell prompt
② tails the current logs for the api and mongo services.
13
5.6. Stop Running Services
If the services were started in the foreground, we can simply stop them with the <ctl>+C command.
If they were started in the background or in a separate shell, we can stop them by executing the
down command in the docker-compose.yml directory.
$ docker-compose down
Stopping docker-hello-example_api_1 ... done
Stopping docker-hello-example_mongo_1 ... done
Stopping docker-hello-example_postgres_1 ... done
Removing docker-hello-example_api_1 ... done
Removing docker-hello-example_mongo_1 ... done
Removing docker-hello-example_postgres_1 ... done
Removing network docker-hello-example_default
14
Chapter 6. Docker Cleanup
Docker Compose will mostly cleanup after itself. The only exceptions are the older versions of the
API image and the builder image that went into creating the final API images. Using my example
settings, these are all end up being named and tagged as none in the images repository.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-hello-example layered 9c45ff5ac1cf 17 hours ago
316MB
registry.heroku.com/ejava-docker/web latest 9c45ff5ac1cf 17 hours ago
316MB
docker-hello-example execjar 669de355e620 46 hours ago
315MB
dockercompose-votes-api latest da94f637c3f4 5 days ago
340MB
<none> <none> d64b4b57e27d 5 days ago
397MB
<none> <none> c5aa926e7423 7 days ago
340MB
<none> <none> 87e7aabb6049 7 days ago
397MB
<none> <none> 478ea5b821b5 10 days ago
340MB
<none> <none> e1a5add0b963 10 days ago
397MB
<none> <none> 4e68464bb63b 11 days ago
340MB
<none> <none> b09b4a95a686 11 days ago
397MB
...
<none> <none> ee27d8f79886 4 months ago
396MB
adoptopenjdk 14-jre-hotspot 157bb71cd724 5 months ago
283MB
mongo 4.4.0-bionic 409c3f937574 12 months ago
493MB
postgres 12.3-alpine 17150f4321a3 14 months ago
157MB
<none> <none> b08caee4cd1b 41 years ago
279MB
docker-hello-example 6.0.1-SNAPSHOT a855dabfe552 41 years ago
279MB
Even though Docker displays each of these images as >300MB, they may share
some base layers and — by themselves — much smaller. The value presented is the
15
space taken up if all other images are removed or if this image was exported to its
own TAR file.
Deleted Images:
deleted: sha256:e035b45628fe431901b2b84e2b80ae06f5603d5f531a03ae6abd044768eec6cf
...
deleted: sha256:c7560d6b795df126ac2ea532a0cc2bad92045e73d1a151c2369345f9cd0a285f
16
6.3. Image Repository State After Pruning
After pruning the images — we have just the named/tagged image(s).
$ docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
docker-hello-example layered 9c45ff5ac1cf 17 hours ago
316MB
registry.heroku.com/ejava-docker/web latest 9c45ff5ac1cf 17 hours ago
316MB
docker-hello-example execjar 669de355e620 46 hours ago
315MB
mongo 4.4.0-bionic 409c3f937574 12 months ago
493MB
postgres 12.3-alpine 17150f4321a3 14 months ago
157MB
docker-hello-example 6.0.1-SNAPSHOT a855dabfe552 41 years ago
279MB
17
Chapter 7. Summary
In this module we learned:
• the purpose of Docker Compose and how it is used to define a network of services operating
within a virtualized Docker environment
• to create a Docker Compose file that defines a network of services and their dependencies
18