Table of Contents


Docker volumes

By default, data written inside a container lives in its writable layer and is lost when the container is removed. A named volume persists data on the host and survives container removal — the standard way to keep stateful data such as database files.

Start a MariaDB container with a named volume attached, using --rm so the container is auto-removed on stop:

docker run --rm --detach --name my-mariadb \
  --env MARIADB_ROOT_PASSWORD=123456 \
  --volume mariadb-data:/var/lib/mysql \
  mariadb:latest

List volumes and inspect the one just created:

$ docker volume ls
DRIVER    VOLUME NAME
local     mariadb-data
 
$ docker inspect mariadb-data
[
    {
        "CreatedAt": "2026-04-29T23:35:25+01:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/mariadb-data/_data",
        "Name": "mariadb-data",
        "Options": null,
        "Scope": "local"
    }
]

With root access, you can see the actual database files on the host at the volume’s mountpoint:

$ sudo ls "/var/lib/docker/volumes/mariadb-data/_data"
aria_log.00000001  ddl_recovery.log  ibdata1   ibtmp1    multi-master.info  performance_schema  tc.log    undo002
aria_log_control   ib_buffer_pool    ib_logfile0  mariadb_upgrade_info  mysql   sys  undo001  undo003

Stop the container — the container is removed, but the volume mariadb-data remains on disk:

docker stop my-mariadb

Volume vs. bind mount

A named volume (--volume mariadb-data:/var/lib/mysql) is managed by Docker and works on any host. A bind mount (--volume /host/path:/container/path) maps an arbitrary host directory and is more common in development. Prefer named volumes for database data.

Docker networking

The classic (now deprecated) way of connecting containers is the --link flag, which injects the target container’s entry into the source container’s /etc/hosts.

Start a Redis container and store a key in it:

# start a Redis (NoSQL) container
docker run -d --name redis-master --rm redis:alpine
$ docker exec -it redis-master redis-cli
 
[IP_ADDRESS]:6379> keys *
(empty array)
[IP_ADDRESS]:6379> set temperature 22.6
OK
[IP_ADDRESS]:6379> get temperature
"22.6"
[IP_ADDRESS]:6379> exit

Start a second Redis container linked to the first:

docker run -d --name redis-test --link redis-master --rm redis:alpine
$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS      NAMES
268466808cef   redis:alpine   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   6379/tcp   redis-test
976f538fe887   redis:alpine   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   6379/tcp   redis-master

Inspect the /etc/hosts file inside the test container — note the redis-master entry added by --link:

$ docker exec -it redis-test sh
 
$ cat /etc/hosts
[IP_ADDRESS]	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::	ip6-localnet
ff00::	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
[IP_ADDRESS]	redis-master 976f538fe887
[IP_ADDRESS]	268466808cef

Connect to the master by hostname and read the key stored earlier:

$ redis-cli -h redis-master
redis-master:6379> keys *
1) "temperature"
redis-master:6379> get temperature
"22.6"
redis-master:6379> exit
docker stop redis-master redis-test

Deprecated feature

--link is a legacy feature and should not be used in new deployments. Docker’s embedded DNS on user-defined networks (see next section) provides the same name-based resolution without a fixed startup order or shared network namespace. It is shown here only because you may encounter it in older environments.

User-defined networks

On a user-defined bridge network, containers reach each other by container name through Docker’s embedded DNS server. This is the recommended approach.

# list existing networks
$ docker network ls
 
# create an isolated bridge network
$ docker network create redis-network
 
# start two containers on the same network
$ docker run -d --name redis-1 --rm --net redis-network redis:alpine
$ docker run -d --name redis-2 --rm --net redis-network redis:alpine

Test name resolution and cross-container connectivity:

$ docker exec -it redis-1 sh
 
$ ping redis-2
$ redis-cli -h redis-2
redis-2:6379> ping
PONG

Default bridge vs. user-defined networks

Containers on the default bridge network cannot resolve each other by name — only by legacy --link. Name resolution works automatically only on user-defined networks like redis-network above.

Exercise: webcounter

In this exercise, you will deploy a multi-container application using Docker networking. The application consists of a web counter service that tracks page visits using a Redis cache as the backend. Containers communicate through an isolated Docker network.

The GitLab project: https://gitlab.com/form-cfreire/webcounter (Python web counter with a Redis backend).

Exercise Objectives

  • Create a Docker network named webcounter-net
  • Use the image cfreire70/webcounter for the web counter container
  • The web counter container exposes port 5000 internally, mapped to 8080 on the host
  • Set the environment variable REDIS_URL=redis-cache to point the app to Redis
  • Use the official redis:alpine image for the cache container
  • Name the Redis container redis-cache to match the REDIS_URL value
  • Attach both containers to the same network (webcounter-net) so DNS resolution works
  • Access the web counter at http://localhost:8080 once both containers are running

Exercise Architecture

img/webcounter-architecture.png
Two containers on the webcounter-net bridge network: the webcounter app (port 5000) connects to redis-cache by DNS name, and port 8080 is published on the host.

Exercise Solution

Step 1 — Create a Docker network

Create an isolated bridge network so both containers can communicate by name.

docker network create webcounter-net

Expected output:

a3f2c1d4e5b6... (a unique network ID)

Step 2 — Run the web counter container

Start the web counter application, connecting it to the network and exposing port 8080 on the host.

docker run -d \
  --name webcounter \
  -p 8080:5000 \
  -e REDIS_URL=redis-cache \
  --net webcounter-net \
  cfreire70/webcounter

Flag breakdown:

FlagValuePurpose
-d—Run in detached (background) mode
--namewebcounterAssign a name to the container
-p8080:5000Map host port 8080 → container port 5000
-eREDIS_URL=redis-cacheSet env var pointing to the Redis container
--netwebcounter-netAttach to the custom network

Expected output:

Unable to find image 'cfreire70/webcounter:latest' locally
latest: Pulling from cfreire70/webcounter
...
Status: Downloaded newer image for cfreire70/webcounter:latest
7f3a1b2c4d5e... (container ID)

Step 3 — Run the Redis container

Start the Redis cache container on the same network.

docker run -d \
  --name redis-cache \
  --net webcounter-net \
  redis:alpine

Flag breakdown:

FlagValuePurpose
-d—Run in detached mode
--nameredis-cacheMust match the REDIS_URL env var set above
--netwebcounter-netSame network as the web container

Expected output:

Unable to find image 'redis:alpine' locally
alpine: Pulling from library/redis
...
Status: Downloaded newer image for redis:alpine
9e4b7c8f1a2d... (container ID)

Step 4 — Verification

docker ps

Expected output:

CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                    NAMES
9e4b7c8f1a2d   redis:alpine           "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds                             redis-cache
7f3a1b2c4d5e   cfreire70/webcounter   "python app.py"          30 seconds ago   Up 29 seconds   [IP_ADDRESS]:8080->5000/tcp   webcounter

Step 5 — Test the application

Open your browser or use curl:

curl http://localhost:8080

Expected output (example):

This page has been visited 1 times.

Each refresh increments the counter — proof that Redis is storing the visit count.

Step 6 — Inspect the network

docker network inspect webcounter-net

You should see both webcounter and redis-cache listed under Containers.

Troubleshooting

SymptomPossible causeFix
curl returns connection refusedwebcounter container not runningCheck docker ps and docker logs webcounter
Counter not incrementingRedis not reachableVerify both containers are on the same network
Port already in useAnother process uses 8080Change -p 9090:5000 and use port 9090
Image pull failsNo internet / wrong image nameCheck connectivity and image name

View container logs:

docker logs webcounter
docker logs redis-cache

Cleanup

Destructive commands

The following commands remove containers, networks and images. Any data not persisted in a volume is lost.

Remove all resources created in this exercise:

# stop and remove containers
docker rm -f webcounter redis-cache
 
# remove the network
docker network rm webcounter-net
 
# (optional) remove pulled images
docker rmi cfreire70/webcounter redis:alpine

Build and publish an image

Build the webcounter image

Public Gitlab repository
https://gitlab.com/form-cfreire/webcounter.git

Simple Python web counter backed by Redis.

git clone https://gitlab.com/form-cfreire/webcounter.git
 
cd webcounter
 
# build the image from the Dockerfile in the current directory
docker build -t webcounter .

Run locally

# start the Redis dependency
docker run -d --name redis --rm redis
 
# run the web counter, linking to Redis and publishing port 80
docker run -d --rm -p 8080:5000 --name webcounter --link redis -e REDIS_URL=redis webcounter

Deprecated feature

--link is deprecated; use a user-defined network instead (see 02-docker-builds):

docker network create webcounter-net
docker run -d --rm --name redis --net webcounter-net redis
docker run -d --rm -p 8080:5000 --name webcounter --net webcounter-net -e REDIS_URL=redis webcounter

Test the application:

curl localhost:8080

Destroy the project:

Destructive commands

docker rm -f forcibly stops and removes the containers.

docker rm -f webcounter redis

Publish to Docker Hub

# tag the image with your Docker Hub username
docker build <docker_id>/webcounter .
 
# authenticate
docker login -u <docker_id>
 
# push the image to the registry
docker push <docker_id>/webcounter

You can use TTL.sh as temporary storage https://ttl.sh/