Containers · ToolTrap learning guide
Docker: reach your container and keep its data
Separate the container's port from your laptop's port, then follow a file through a container restart and replacement.
Start with a small local experiment
Imagine you are testing a website on your laptop. The web server inside its container listens on port 80, but you want to visit localhost:8085 in your browser. These are two different network endpoints. Publishing a port creates the connection between them; it does not change the port the web server listens on.
Run the following commands in a terminal with Docker installed. This example binds to your laptop's loopback address, so the published service is for local use. Check for a port conflict if another program already uses 8085.
docker run --name tooltrap-web -d -p 127.0.0.1:8085:80 nginx:alpine
docker port tooltrap-web
docker logs tooltrap-webOpen http://localhost:8085. In the mapping, 8085 is the host port and 80 is the container port. If the page fails to load, first check whether the container is running with docker ps, then read its logs. A port mapping cannot repair a server that has stopped.
Restarting and replacing are different
A container has a writable filesystem. Restarting that same container normally keeps files written there. Removing the container removes that writable layer, even if the image is still available. This distinction matters when a service stores uploads or database files.
A named volume has a lifecycle separate from a container. Here, a temporary writer puts a message in a volume, then exits and is removed. A second, independent container reads the same message. Nothing in this experiment relies on retaining the first container.
docker volume create tooltrap-notes
docker run --rm -v tooltrap-notes:/notes alpine sh -c 'echo learned-docker > /notes/progress.txt'
docker run --rm -v tooltrap-notes:/notes alpine cat /notes/progress.txtThe reader prints learned-docker. Both containers mount the volume at /notes, so they see the same file. A volume is persistence, not a backup: accidental deletion or a bad application write can still destroy data. Back up important records separately.
Check your understanding
Question 1: Which address reaches the sample web server: localhost:80 or localhost:8085? Answer: localhost:8085, because the left side of the mapping chooses the host port.
Question 2: Will progress.txt disappear when the temporary writer exits? Answer: no. The named volume remains after the writer container is removed.
Question 3: Does docker restart simulate replacing a container? Answer: no. To test replacement, create a new container that mounts the same volume. Do that only with disposable data when learning.
Finish the lab by removing the sample web container with docker rm -f tooltrap-web. Once you no longer need the practice file, remove this specific lab volume with docker volume rm tooltrap-notes. Check the names before cleanup so you leave other projects alone.
Explore the official documentation
Use these references to check command options and continue your practice.