Docker, start to ship · Chapter 17 of 26
Containers talking to each other
By the end of this chapter you can let one container reach another through the lab machine's published ports, or put a container on the lab machine's own network. 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
Tapri now has two services: the price list, chai-17-prices (nginx, published on port 8067), and Bunty's orders API, chai-17-api (port 8017), which asks the price list what an order costs. On Bunty's laptop, with both running as plain programs, it worked. In containers:
$ curl -s localhost:8017
No prices: could not reach http://localhost:8067/prices.json
$ docker logs chai-17-api
orders API on port 3000, prices from http://localhost:8067/prices.json
could not reach the price list at http://localhost:8067/prices.json: ECONNREFUSED
Ravi also wants a redis cache beside the API, and a second copy of the API that skips Docker's port publishing. So far every request came from you, on the lab machine, and -p was enough. Now a container is the one asking, and the address that worked on the laptop leads nowhere.
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-17-cache and ping it from another container
Run a redis cache, then reach it from a second container.
1. Run a container named chai-17-cache from redis, in the background.
2. Publish its port 6379 on the lab's port 8077.
3. With docker run, start a short-lived redis container (use --rm). Make it run redis-cli ping instead of a server.
4. Give redis-cli the lab machine's name inside a container, host.containers.internal, as its host, and 8077 as its port. It answers PONG.
To pass: chai-17-cache is running from redis, with 6379 published on 8077. You ran that docker run with redis-cli, port 8077 and host.containers.internal (or the host network) in the chapter folder, and it worked. A command with a typing mistake does not count, so type it again.
- Task 2
Fix chai-17-api so it reaches the price list
Bunty's API cannot reach the price list. Fix its address.
1. Read the API's logs. They show the address it tries for the price list.
2. Delete chai-17-api. Its settings are fixed when it is made.
3. Run a new chai-17-api from chai-17-api:1.0, in the background, with its port 3000 published on the lab's port 8017.
4. Set PRICES_URL so it reaches the price list on the lab's port 8067. Use the lab machine's name from task 1.
To pass: chai-17-api is running, its port 3000 is published on 8017, and localhost:8017 answers with the order priced at Rs 70.
- Task 3
Run chai-17-api-host on the host network, on port 8087
Run a second copy of the API on the lab machine's own network.
1. Run a container named chai-17-api-host from chai-17-api:1.0, in the background.
2. Put it on the host network. On the host network there is nothing to publish.
3. Set PORT to 8087, so it does not use the API's default port.
4. Set PRICES_URL to the price list's address, as seen from the lab machine.
To pass: chai-17-api-host is running from chai-17-api:1.0 on the host network, and localhost:8087 answers with the order priced at Rs 70.
- Task 4 · interview question
Interview: why localhost missed the price list
Interview question: curl localhost:8067 works on the lab machine. Why did the API's address localhost:8067 not reach the price list from inside its container?
1. Think about what localhost means inside a container.
2. Write one or two sentences in your own words.
3. Save them in answer.txt, in the chapter folder.
To pass: answer.txt says what localhost is inside a container.
What you will learn
- Where localhost leads, asked from the lab machine and from inside a container.
- host.containers.internal: the lab machine's name inside a container, where every published port is.
- --network host: a container on the lab machine's own network, with nothing to publish.
More in Ports and config
Build "Containers talking to each other" 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.