Run a Docker container with storage and networking
Run Nginx, compare the lifetime of files in a writable layer, volume, and bind mount, then connect through a published port and a bridge alias.
A file you write inside an Nginx container survives a stop and start. If you replace the container, the file’s storage location determines whether it survives, and the source of each request determines which address to use.
Run Nginx from the official image
Run these commands in order in the same terminal on a machine running Docker locally with curl installed. Use a POSIX shell such as bash or zsh (WSL on Windows). The lab used Docker Desktop with Docker Engine and CLI 29.9.3 (Linux arm64) and Nginx 1.30.8 from nginx:1.30-alpine. The first commands create a lab directory under the current directory and use its name to prefix the Docker resources.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
docker version
lab_dir=$(mktemp -d "$PWD/docker-lab.XXXXXX")
cd "$lab_dir"
lab=$(basename "$lab_dir" | tr '[:upper:]' '[:lower:]')
base_image=nginx:1.30-alpine
doc_root=/usr/share/nginx/html
docker pull "$base_image"
docker image ls "$base_image"
docker run -d --name "$lab-base" -p 127.0.0.1::80 "$base_image"
docker ps --filter "name=$lab-base"
docker port "$lab-base" 80/tcp
http() {
curl -sS --retry 5 --retry-connrefused \
"http://$(docker port "$1" 80/tcp)$2"
}
http "$lab-base" /
docker logs --tail 5 "$lab-base"
docker stop "$lab-base"
docker rm "$lab-base"
pull downloads an image. run creates a container from that image and starts it. The -d flag leaves the container running in the background. With -p 127.0.0.1::80, Docker picks an available port on the host’s IPv4 loopback interface and forwards the traffic to TCP port 80 in the container. The http function looks up the address with docker port before making each request. In the lab, the official image served Welcome to nginx! and docker logs recorded a 200 response for GET / HTTP/1.1. Pull an image · Run a container
Build a custom image and replace a container
Save these as Dockerfile and index.html in the lab directory. The dot at the end of docker build makes that directory the build context. The COPY instruction takes the HTML file from that context and puts it in the Nginx document root in the new image. Build context
1
2
3
ARG BASE_IMAGE=nginx:1.30-alpine
FROM ${BASE_IMAGE}
COPY index.html /usr/share/nginx/html/index.html
1
2
3
4
5
6
7
8
9
10
11
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello Docker</title>
</head>
<body>
<h1>Hello from my Docker image</h1>
<p>version: v1</p>
</body>
</html>
1
2
3
4
image="$lab-hello:dev"
docker build -t "$image" .
docker run -d --name "$lab-web" -p 127.0.0.1::80 "$image"
http "$lab-web" /
Look for Hello from my Docker image and version: v1 in the response body. Next, create note.txt inside the running container so you can track a file that was written after the image was built.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
docker exec "$lab-web" sh -c \
'printf "%s\n" written-in-container > /usr/share/nginx/html/note.txt'
docker inspect --format '{{.Id}}' "$lab-web"
docker stop "$lab-web"
docker ps -a --filter "name=$lab-web"
docker start "$lab-web"
docker inspect --format '{{.Id}}' "$lab-web"
http "$lab-web" /note.txt
docker run -d --name "$lab-fresh" -p 127.0.0.1::80 "$image"
http "$lab-fresh" /
curl -sS -o /dev/null -w '%{http_code}\n' \
"http://$(docker port "$lab-fresh" 80/tcp)/note.txt"
docker stop "$lab-fresh"
docker rm "$lab-fresh"
After stop, docker ps -a shows the container as Exited. The lab saw the same container ID after start and got written-in-container from /note.txt. A fresh container from the same image returned HTTP 404 for that path. Each container has its own writable layer above the image. With no mount at that path, the file belongs to that layer and disappears when you remove the container. Use start to keep the files and settings of a stopped container, and run when you need a clean container from the image. Docker storage drivers
Change version: v1 to version: v2 in the lab’s index.html, save the file, and rebuild with the same tag. Compare the image ID and response body for the original container and a new one.
1
2
3
4
5
6
7
8
9
10
docker build -t "$image" .
docker image inspect --format '{{.Id}}' "$image"
docker run -d --name "$lab-v2" -p 127.0.0.1::80 "$image"
docker inspect --format '{{.Name}} {{.Image}}' "$lab-web" "$lab-v2"
http "$lab-web" /
http "$lab-v2" /
docker stop "$lab-web" "$lab-v2"
docker rm "$lab-web" "$lab-v2"
docker image inspect --format '{{.Id}}' "$image"
The tag moved to the new image ID. The original container still served v1 and the new one served v2. The final inspect command still found the v2 image after both containers were removed. The tag is a reference to an image. Docker resolves that reference when it creates a container, and the container keeps using the selected image. Tag an image · Remove a container
Keep files after removing a container
| Storage location | After container removal | Use it for |
|---|---|---|
| Container writable layer | Docker removes the files with the container. | Temporary files you can recreate |
| Named volume | The files stay in a Docker-managed volume. | Application data a replacement container needs |
| Bind mount | The files stay at the selected path on the host. | Source and configuration you edit on the host |
Give a volume a name so another container can mount it later. The first container writes msg.txt and Docker removes the container when it exits. The second container mounts the same volume and prints saved-before-rm. Docker volumes
1
2
3
4
5
docker volume create "$lab-data"
docker run --rm --mount "type=volume,src=$lab-data,dst=/data" "$base_image" \
sh -c 'printf "%s\n" saved-before-rm > /data/msg.txt'
docker run --rm --mount "type=volume,src=$lab-data,dst=/data" "$base_image" \
cat /data/msg.txt
When you mount an empty volume over the document root, Docker copies the image files at that path into the volume by default. The lab got the v2 page and found index.html in the mounted directory. Add volume-nocopy to the --mount options if you want to skip this initial copy. Mount a volume over existing data
1
2
3
4
5
6
7
docker volume create "$lab-html"
docker run -d --name "$lab-seeded" -p 127.0.0.1::80 \
--mount "type=volume,src=$lab-html,dst=$doc_root" "$image"
http "$lab-seeded" /
docker exec "$lab-seeded" ls "$doc_root"
docker stop "$lab-seeded"
docker rm "$lab-seeded"
A bind mount makes an existing host path visible at a path in the container. Use it when you want Nginx to serve files as you edit them on the host. The readonly option blocks writes through that mount from inside the container. In the lab, changing the host’s index.html changed the same container’s response from bind edit 1 to bind edit 2. Docker bind mounts
1
2
3
4
5
6
7
8
9
10
11
mkdir live empty
printf '<h1>bind edit 1</h1>\n' > live/index.html
docker run -d --name "$lab-bind" -p 127.0.0.1::80 \
--mount "type=bind,src=$PWD/live,dst=$doc_root,readonly" "$image"
http "$lab-bind" /
printf '<h1>bind edit 2</h1>\n' > live/index.html
http "$lab-bind" /
docker exec "$lab-bind" sh -c \
'printf x > /usr/share/nginx/html/x.txt'
docker stop "$lab-bind"
docker rm "$lab-bind"
The last write command produced a Read-only file system error in the lab. Mount an empty host directory at the same path and the files from the image are hidden under the mount. With this Nginx configuration, the document root had zero entries and GET / returned HTTP 403. For permission errors on a mounted path, check the file ownership and the UID and GID of the process. UID and GID permissions for volumes and bind mounts
1
2
3
4
5
6
7
8
docker run -d --name "$lab-empty" -p 127.0.0.1::80 \
--mount "type=bind,src=$PWD/empty,dst=$doc_root,readonly" "$image"
http "$lab-empty" /
curl -sS -o /dev/null -w '%{http_code}\n' \
"http://$(docker port "$lab-empty" 80/tcp)/"
docker exec "$lab-empty" ls -A "$doc_root"
docker stop "$lab-empty"
docker rm "$lab-empty"
Connect through a published port or a bridge alias
From the host, use the 127.0.0.1:<port> address shown by docker port. Docker assigns the host port and can pick a different one when the container restarts, so look up the current mapping again after a restart. In the lab, the container got a new port after stop and start. A container on the same user-defined bridge can ask Docker DNS for the alias web and connect to the server’s container port 80. Here, one Nginx container accepts requests over both paths. Docker bridge networks
1
2
3
4
5
6
7
8
9
10
11
docker network create "$lab-net"
docker network create "$lab-isolated"
docker run -d --name "$lab-netweb" \
--network "$lab-net" --network-alias web \
-p 127.0.0.1::80 "$image"
docker network inspect "$lab-net"
docker port "$lab-netweb" 80/tcp
http "$lab-netweb" /
docker run --rm --network "$lab-net" "$base_image" wget -qO- http://web:80/
docker run --rm --network "$lab-isolated" "$base_image" wget -qO- http://web:80/
In the docker network inspect output, the Containers section lists $lab-netweb as attached to the network. The host’s curl and the first wget both got the v2 page. The second wget, running on a different bridge, failed with bad address 'web:80'. Inside a container, 127.0.0.1 refers to that container’s own loopback interface. Pick the destination address based on whether your caller runs on the host or inside another container.
Dockerfile EXPOSE documents which port the image uses. Pass -p at runtime to publish a port on the host. With Docker Engine older than 28.0.0, other hosts on the same layer 2 network could reach ports published to localhost. Use Engine 28.0.0 or later if you rely on loopback binding to limit access. Dockerfile EXPOSE · Docker port publishing · EXPOSE and host port mapping
Clean up the lab resources
These commands remove the last container and the networks, volumes, and image tag created with the lab’s prefix. Removing the volumes deletes the files stored there. The official image and build cache are available for subsequent runs. If you stopped partway through, run docker ps -a --filter "name=$lab" to find the containers you created and remove those first.
1
2
3
4
5
6
7
8
9
10
11
docker stop "$lab-netweb"
docker rm "$lab-netweb"
docker network rm "$lab-net" "$lab-isolated"
docker volume rm "$lab-data" "$lab-html"
docker image rm "$image"
: "${lab_dir:?}"
rm -- "$lab_dir/Dockerfile" "$lab_dir/index.html" "$lab_dir/live/index.html"
rmdir -- "$lab_dir/live" "$lab_dir/empty"
cd "$lab_dir/.."
rmdir -- "$lab_dir"