Getting started with Docker - Part 2
Docker Part 2 explains volumes, user-defined networks and Docker Compose through manual commands, multi-container communication and repeatable local environments for practical guidance.
Getting started with Docker - Part 2
A real application is rarely one container. Our fund API needs a cache, the cache needs to keep its data when the container is restarted, and the API must find the cache without a hard-coded IP address. When we start each container by hand with long docker run commands, it works on the first day. From the second day, someone forgets a port, a volume or an environment variable, and we are back to the same “it works on my machine” problem which made us look at Docker in the first place.
Last year, in Getting started with Docker - Part 1, we discussed images, containers and the basic commands. In the previous article, we discussed how to set up Cloudflare in front of an Azure Web App, and before that, how to build a Docker image for an ASP.NET Core application with a Dockerfile. In this article, we are going to see the three things which make Docker useful for multi container applications, volumes, networks and Docker Compose.
TL;DR Summary
- A container’s writable layer is deleted with the container, durable data belongs in a volume.
- Named volumes are managed by Docker, bind mounts map a host folder into the container.
- User-defined networks give containers a private network and DNS, containers find each other by name.
- Don’t use
--linkfor new work, use user-defined networks. - Docker Compose describes all containers, networks and volumes of an application in one
docker-compose.ymlfile. docker-compose upstarts the whole application with one command, the same way on every machine.- Compose is great for development and testing, for production orchestration look at Swarm mode, DC/OS or Kubernetes.
Challenges
Data lost with the container
When a database or cache container is removed and created again (for example to update the image), all the data inside its writable layer is gone. It is fine for stateless containers, but not for the database.
Containers can’t find each other
Containers get dynamic IP addresses. Hard-coding an IP in the connection string breaks as soon as the container is restarted. The old --link option works but it is limited and now considered as legacy.
Long and error-prone commands
Starting three containers with ports, volumes, networks and environment variables means three long commands, executed in the right order. It is easy to make a mistake and hard to share with the team.
What is a Volume?
A Docker volume is a storage location managed outside the container’s union file system, which persists independently of the container lifecycle and can be shared between containers.
In simple terms, the container is a hotel room and the volume is the locker in the hotel lobby. When we check out of the room (remove the container), the room is cleaned, but the locker still has our things for the next stay.
There are two common types,
- Named volume - created and managed by Docker (
docker volume create redis-data). Best for database and cache data. - Bind mount - maps a folder from the host into the container (
-v /home/user/src:/app). Useful for development, to see code changes without rebuilding the image.
What is a Docker network?
A Docker network connects containers to each other and to the outside world. Docker creates a default bridge network, but a user-defined bridge network gives us an isolated network with an embedded DNS server, so containers can reach each other by container name.
In simple terms, it is like an office intranet. Everyone on the same intranet can call each other by name, and people outside can only reach the reception desk (the published port).
The following diagram depicts the volume and network setup for our example.

The web container (our fund API) and the cache container (Redis) are on the same user-defined network app-net. Only the web container publishes a port to the host. The Redis data is stored in the named volume redis-data, so it survives when the container is recreated.
Why it is important?
In order to keep the data safe across container updates, to connect containers without hard-coded addresses and to start the whole application the same way on every developer machine and test server, volumes, networks and Compose are essential. Without them, Docker is only a better way to run one process, with them, it becomes a way to describe the full application environment.
Steps to Implement: Volumes and networks by hand
Follow the following instructions to understand the building blocks before using Compose.
-
Create a named volume and a user-defined network.
docker volume create --name redis-data docker network create app-net -
Just to pulse check, list them.
docker volume ls docker network ls -
Run Redis on the network with the volume, with persistence enabled.
docker run -d --name cache --network app-net -v redis-data:/data redis:3.2 redis-server --appendonly yes -
Run the fund API on the same network. The API uses
cache:6379as the Redis address,cacheis the container name.docker run -d --name web --network app-net -p 8080:5000 -e "Redis__Host=cache:6379" <registry>/fund-reporting-api:<version> -
Check that a container on the same network can reach Redis by name. It should reply
PONG.docker run --rm --network app-net redis:3.2 redis-cli -h cache ping -
Remove and recreate the Redis container, the data in
redis-datais still there.docker rm -f cache docker run -d --name cache --network app-net -v redis-data:/data redis:3.2 redis-server --appendonly yes -
Inspect the network to see the connected containers and their IP addresses.
docker network inspect app-net
What is Docker Compose?
Docker Compose is a tool for defining and running multi container Docker applications, where the services, networks and volumes are described in a YAML file and managed with a single command.
Simply put, the long docker run commands become a file in source control. The following diagram explains what docker-compose up creates from the file.

Steps to Implement: Docker Compose
Follow the following instructions to describe the same application with Compose.
-
Docker Compose is included in Docker for Windows and Docker Toolbox. Just to pulse check, check the version.
docker-compose version -
Create a
docker-compose.ymlfile in the solution folder.version: '2' services: web: build: ./src/FundReporting.Api image: <registry>/fund-reporting-api:dev ports: - "8080:5000" environment: - ASPNETCORE_ENVIRONMENT=Development - Redis__Host=cache:6379 depends_on: - cache networks: - app-net cache: image: redis:3.2 command: redis-server --appendonly yes volumes: - redis-data:/data networks: - app-net volumes: redis-data: networks: app-net: -
Start the application. Compose builds the
webimage, creates the network and volume, and starts the containers in order.docker-compose up -d -
List the services and follow the logs.
docker-compose ps docker-compose logs -f web -
Call the API to pulse check.
curl http://localhost:8080/api/funds -
Stop and remove the containers and the network. The named volume is kept unless we add
-v.docker-compose down
Note:
depends_ononly controls the start order, it doesn’t wait until Redis is ready to accept connections. The application must retry the connection at start up, which is a good practice anyway in the cloud.
Must Do and Don’ts
- Must Do: Store database and cache data in named volumes.
- Must Do: Use user-defined networks and connect by service name.
- Must Do: Keep
docker-compose.ymlin source control along with the code. - Should: Use a
docker-compose.override.ymlfor developer specific settings like bind mounts. - Don’t: publish the database or cache ports to the host unless you really need them.
- Don’t: put secrets in
docker-compose.yml, use an.envfile which is not committed, or environment variables from the build server. - Be mindful: with Docker Toolbox, bind mounts work only for folders under
C:\Usersby default, because of the VirtualBox shared folder.
Wrap up
In this article, we have looked at volumes, user-defined networks and Docker Compose, why they are needed for multi container applications and the step by step instructions to use them by hand and with Compose. I appreciate your time and efforts to read this article and I hope this article provided you some insights into running multi container applications with Docker. This closes the Cloud How-to series for 2016. Refer the hosting model article from earlier this year for choosing where to run these containers in Azure.
Related Posts
How to build a Docker image for an ASP.NET Core application?
Building a Docker image for ASP.NET Core explained through Dockerfiles, SDK and runtime images, build commands, containers, configuration and deployment practices.
Getting started with Docker - Part 1
Getting started with Docker through images, containers, Docker Toolbox, Docker Machine, host addresses, and basic Docker 1.x commands for practical first steps.
How to configure deployment slots in Azure App Service?
Azure App Service deployment slots explained through staging, slot settings, validation, and swapping an ASP.NET MVC release into production safely.