Docker, start to ship · Chapter 16 of 26
Publish or perish
By the end of this chapter you can publish a container's port where you want it: on a port you choose, on one address only, or on a port Docker picks. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Ports and config
- 4 tasks
- Docker
- In the all labs plan
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.
Why you need this
Bunty has the menu site running, and docker ps agrees with him:
$ docker ps --filter name=chai-16
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6e5a5720025 docker.io/library/nginx:latest nginx -g daemon o... Less than a second ago Up Less than a second ago chai-16-menu
$ curl localhost:8016
curl: (7) Failed to connect to localhost port 8016 after 0 ms: Couldn't connect to server
"But the image exposes port 80," he says, and it does (docker image inspect --format '{{json .Config.ExposedPorts}}' nginx prints {"80/tcp":{}}). Meera also wants the admin page, with today's takings on it, running beside the menu, but nobody outside the lab machine may reach it. So far -p has meant one thing to you, lab:container on every address, which would put the takings on the street.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Run chai-16-menu again, on port 8016
Bunty's chai-16-menu runs, but you cannot reach it from the lab machine.
1. Delete Bunty's chai-16-menu. A container's ports are fixed when it is made.
2. Run a new chai-16-menu from nginx, in the background.
3. Publish its port 80 on the lab's port 8016 with -p.
4. Run curl localhost:8016 to see the page.
To pass: chai-16-menu is running, its port 80 is published on port 8016, and localhost:8016 shows the Welcome to nginx page.
- Task 2
Run chai-16-admin on 127.0.0.1:8066 only
The admin page must answer only on the lab machine itself.
1. Run a container named chai-16-admin from nginx, in the background.
2. Mount the admin folder at /usr/share/nginx/html. Read-only is best.
3. Publish its port 80 on port 8066 of the address 127.0.0.1 only.
4. Run curl 127.0.0.1:8066 to see the page.
To pass: chai-16-admin is running, its port 80 is published on 127.0.0.1:8066 and nowhere else, and that address shows the Tapri admin page.
- Task 3
Let Docker pick a port for chai-16-spare
Let Docker choose a free port on the lab machine, then find which one it chose.
1. Run a container named chai-16-spare from nginx, in the background.
2. Publish its port 80, but do not choose the lab's port. Give -p only the container's port, or use -P.
3. Ask Docker which lab port it chose. docker port shows it.
4. Save that number in port.txt, in the chapter folder.
To pass: chai-16-spare is running, you made it with docker run and -p 80 or -P, typed in the chapter folder, and port.txt has the port Docker chose.
- Task 4 · interview question
Interview: what EXPOSE does and what -p does
Interview question: what does EXPOSE in a Dockerfile do, and what does -p on docker run do?
1. Think about Bunty's first chai-16-menu. Its image exposed port 80. Could you reach it?
2. Write one or two sentences in your own words, one about each.
3. Save them in answer.txt, in the chapter folder.
To pass: answer.txt says what EXPOSE does to a port, and it names -p (or publishing) and what it does.
What you will learn
- -p lab:container again, with the mistakes it invites.
- -p address:lab:container: publishing on one address only.
- -p container and -P: letting Docker pick the lab's port, and docker port to find it.
More in Ports and config
Build "Publish or perish" yourself
Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.