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.
Write a file in one container, then start another from the same image:
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
sh -c '...'runs the text in quotes as a shell command inside the container.echo hello > /note.txtwrites the word hello into a file, and>sends the output to that file. - 2
docker diff scratchlists the files that the container added, changed or deleted. - 3
docker rm scratchdeletes the stopped container together with its writable layer. - 4
--rmremoves 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?
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.txtnotes hello
Here is what each marked part does:
- 1
docker volume create notesmakes an empty named volume callednotes. - 2
-v notes:/datamounts the volumenotesat the path/datainside 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.
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:
mkdir -p door
docker run --rm -v "$PWD/door":/door alpine sh -c 'echo from the box > /door/hello.txt'
cat door/hello.txtfrom the box
Here is what each marked part does:
- 1
mkdir -p doorcreates the folderdoor. The option-pstops it from failing if the folder already exists. - 2
"$PWD/door":/doorstarts with a path, so this is a bind mount.$PWDis the folder you are in now, and/dooris 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.
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:
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
-dstarts the container in the background and prints its ID. - 2
docker exec quiet ...runs a command inside the running containerquiet.wget -qO-downloads a page quietly and prints it, andgrep -oprints only the part that matches. - 3
curl -sSrequests a page from the host.-shides the progress bar, and-Sstill 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.
docker run -d --name web -p 8080:80 nginx:alpine
curl -s localhost:8080 | grep -o '<title>.*</title>'
docker port web8022a3c68d05c09ab39a1996c9072a862473e18bf1288ea2a8926b8d3b93533f <title>Welcome to nginx!</title> 80/tcp -> 0.0.0.0:8080 80/tcp -> [::]:8080
Here is what each marked part does:
- 1
-p 8080:80connects port 8080 on the host to port 80 inside the container. - 2
curl -srequests the page from the host port 8080 and hides the progress bar.grep -okeeps 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:
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
127.0.0.1:8081:80adds the host address in front. Only this machine can connect to port 8081. - 2
docker port locallists the published ports of the containerlocaland 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.
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 rmdeletes 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:80opens the port to everyone who can reach the host.-p 127.0.0.1:8080:80opens 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.