What volumes and ports are for

A container's own files are deleted with the container, and nothing outside can reach the container until you open a gate. A volume keeps the data. A published port opens the gate, and its address decides who can come in.

Part 5 of 9 · Containers5 min read
its own top sheet:gone on docker rmpgdatavolume: a trailerstays when the box goesbind mount:a door to your foldersdev only-p 8080:80open to everyone127.0.0.1:8080:80this machine onlyports: a gate

You remove a Postgres container and start a fresh one from the same image. The tables are gone. A minute later you start a web server. It answers inside the container, but your browser gets no answer. Both problems come from how a container is built. What a container writes stays inside it, and nothing outside can reach it until you open a way.

The container’s own sheet

In part 2 we saw that an image is a stack of read-only sheets. A running container adds one more sheet on top. This top sheet is the only one it can write on: its writable layer. Every file the container creates or changes is stored there.

alpine (read-only, shared)/note.txtcontainer scratchnothing writtenanother containerdocker rm throws this sheet away
The image's sheets are read-only and shared. The top sheet belongs to one container, and docker rm deletes it.

Write a file in one container, then start another from the same image:

Shell
docker run --name scratch alpine sh -c 'echo hello > /note.txt' 
docker diff scratch 
docker rm scratch 
docker run --rm alpine cat /note.txt 
A /note.txt
scratch
cat: can't open '/note.txt': No such file or directory

Here is what each marked part does:

  1. 1sh -c '...' runs the text in quotes as a shell command inside the container. echo hello > /note.txt writes the word hello into a file, and > sends the output to that file.
  2. 2docker diff scratch lists the files that the container added, changed or deleted.
  3. 3docker rm scratch deletes the stopped container together with its writable layer.
  4. 4--rm removes this container automatically when it ends.

docker diff lists what is on the top sheet: one added file. After docker rm the sheet is gone, and the new container starts from the image’s clean sheets. Stopping is not removing. docker stop and docker start keep the sheet. But containers are made to be deleted and replaced, and every replacement starts empty.

A trailer left at the dock

So the data must be stored outside the box. Bub, our dock hand, parks a trailer at the dock and hooks it to the box. When the crane removes the box, the trailer stays. The next box connects to the same trailer. That trailer is a named volume.

The container that wrote the file is gone. What does the new one print?

Shell
docker volume create notes 
docker run --rm -v notes:/data alpine sh -c 'echo hello > /data/note.txt' 
docker run --rm -v notes:/data alpine cat /data/note.txt
notes
hello

Here is what each marked part does:

  1. 1docker volume create notes makes an empty named volume called notes.
  2. 2-v notes:/data mounts the volume notes at the path /data inside the container.

-v notes:/data connects the trailer. It mounts the volume named notes at /data inside the container. Everything the container writes under /data goes into the trailer. The first container wrote there and was removed (--rm). The second container mounted the same trailer and found the file. Two containers that mount one volume at the same time read and write the same files. They do not get copies.

notes writenotesdocker rmthe trailer staysnotes same trailer

A box hooked to the trailer writes its data into the trailer, not onto its own sheet.

Docker keeps the trailer in its own yard. docker volume inspect notes shows a path under /var/lib/docker/volumes/. On Docker Desktop that path is inside its Linux VM, not on your Mac. You reach the data through a container or with docker volume commands. Only docker volume rm notes deletes the data.

A door in the wall

Sometimes you want the opposite. You want a folder from your own machine inside the box, so that the code you edit appears in the container at once. That is a bind mount, a door cut into the warehouse wall. It uses the same -v flag. When the part before the colon is a path instead of a name, Docker mounts that host folder. Everything the container writes goes directly onto your disk:

Shell
mkdir -p door 
docker run --rm -v "$PWD/door":/door alpine sh -c 'echo from the box > /door/hello.txt' 
cat door/hello.txt
from the box

Here is what each marked part does:

  1. 1mkdir -p door creates the folder door. The option -p stops it from failing if the folder already exists.
  2. 2"$PWD/door":/door starts with a path, so this is a bind mount. $PWD is the folder you are in now, and /door is the place inside the container.

The file is on the host, and it stays there whatever happens to the container. A bind mount is useful in development and dangerous elsewhere. In part 4, -v /:/host:ro gave the whole host to a container. The mount was read-only only because we asked for that.

named volumebind mountnotes ~/door Docker looks after itstays after docker rma door into the host's folderthe host decides what is behind it
A volume is a trailer that Docker manages. A bind mount is a door directly into the host's warehouse, and the host decides what is behind it.

Use a volume for data the container owns, like a database. Use a bind mount when the host owns the files, like source code in development or a config file you edit by hand.

A gate onto the street

Now the network. In part 1 the container got its own network namespace, the blindfold for the network. nginx listens on port 80 inside that namespace. That port 80 is not the host’s port 80:

Shell
docker run -d --name quiet nginx:alpine 
docker exec quiet wget -qO- localhost | grep -o '<title>.*</title>' 
curl -sS localhost:80 
b64f96ca66f28147ce8c5232f37bc8b0984b0c2a032ffed3868965d39b4cf879
<title>Welcome to nginx!</title>
curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server

Here is what each marked part does:

  1. 1-d starts the container in the background and prints its ID.
  2. 2docker exec quiet ... runs a command inside the running container quiet. wget -qO- downloads a page quietly and prints it, and grep -o prints only the part that matches.
  3. 3curl -sS requests a page from the host. -s hides the progress bar, and -S still shows an error if the request fails.

The first line is the new container’s ID. Inside the box, nginx answers. From the host, nothing is listening on port 80 (unless some other program on your machine already uses that port). To let people in, open a gate. -p 8080:80 publishes the container’s port 80 on the host’s port 8080. If something on your machine already uses 8080, choose another host port.

Shell
docker run -d --name web -p 8080:80 nginx:alpine 
curl -s localhost:8080 | grep -o '<title>.*</title>' 
docker port web
8022a3c68d05c09ab39a1996c9072a862473e18bf1288ea2a8926b8d3b93533f
<title>Welcome to nginx!</title>
80/tcp -> 0.0.0.0:8080
80/tcp -> [::]:8080

Here is what each marked part does:

  1. 1-p 8080:80 connects port 8080 on the host to port 80 inside the container.
  2. 2curl -s requests the page from the host port 8080 and hides the progress bar. grep -o keeps only the title.

Which way the gate opens

Look at the last two lines. 0.0.0.0 and [::] mean every address the host has. The gate opens onto the street. Anyone who can reach the machine, on any of its networks, can reach port 8080. With an address in front, the gate opens onto the back yard instead:

Shell
docker run -d --name local -p 127.0.0.1:8081:80 nginx:alpine 
docker port local 
98befd5f1022b1d65900d38442e2fef4552a815b43bd16aa8efed5c78d928e6f
80/tcp -> 127.0.0.1:8081

Here is what each marked part does:

  1. 1127.0.0.1:8081:80 adds the host address in front. Only this machine can connect to port 8081.
  2. 2docker port local lists the published ports of the container local and the host addresses they use.

127.0.0.1 is loopback. Only programs on this machine can connect to it. A database that no other machine needs belongs here.

-p 8080:80-p 127.0.0.1:8081:80the streethost nginx :800.0.0.0:8080the streethost nginx :80back yardcurl127.0.0.1:8081no way in
-p 8080:80 opens the gate onto the street. -p 127.0.0.1:8081:80 opens it onto the back yard, for this machine only.

Clean up with docker rm -f quiet web local, docker volume rm notes and rm -r door.

Key points

  • A container writes on its own top sheet, the writable layer, and docker rm deletes it. When you replace a container, its files are gone.
  • A named volume is a trailer that stays at the dock. Docker keeps it outside the box, and the next container mounts the same data.
  • A bind mount is a door into the host’s own folders. It is right for source code in development and dangerous for anything else.
  • A container’s ports are closed to the host until you publish one with -p. -p 8080:80 opens the port to everyone who can reach the host. -p 127.0.0.1:8080:80 opens it to this machine only.

Next, in part 6: several boxes and one plan. Compose describes the boxes, their trailers and their gates in one file.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS