Challenge - Build and run
- Docker & Kubernetes course
- Beginner
- Lesson 79/82
- 5 min
Build an image and run the container on a specific port.
Challenge: build and run
Building an image, versioning it and running it with the right options is a series of commands. In this challenge you'll do a version-tagged build, inspect the image and start it in a production-like way:
docker build --pull -t api:1.0.0 -t api:latest .
docker history api:1.0.0
docker run -d --name api --restart unless-stopped -p 127.0.0.1:3000:3000 \
--env-file api.env --memory 256m api:1.0.0
docker logs -f apiPractice Build and inspect
Write two commands: build from the current folder updating the base image, tagged both api:1.0.0 and api:latest; then show the layer history of api:1.0.0.
Starter code
# buraya yazSample solution
docker build --pull -t api:1.0.0 -t api:latest .
docker history api:1.0.0Expected output
[+] Building 7.1s (12/12) FINISHED => naming to docker.io/library/api:1.0.0 done IMAGE CREATED CREATED BY SIZE 4f1e2d3c4b5a 3 seconds ago CMD ["node" "server.js"] 0B
Practice Run like production
Start a container named api from api:1.0.0 with one command: in the background; restarting unless stopped by hand; port 3000 published only on 127.0.0.1; settings from api.env; memory limited to 256m.
Starter code
# buraya yazExpected output
7e8f90a1b2c3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In this run, can the API be reached from another computer on the same network?
- Only if --memory is removed
- No, the port is bound only to 127.0.0.1 Correct answer
- Yes, through port 3000
- Only after a restart
Explanation: To expose it, a reverse proxy is usually put in front.
2 Limit the container's memory.
docker run -d ____ 256m api:1.0.0
docker run -d --memory 256m api:1.0.0
Explanation: --memory (or -m) is a cgroup limit.
3 Match.
| Term | Meaning |
|---|---|
--pull |
Fresh base image |
--restart unless-stopped |
Resilience |
--env-file |
Settings |
127.0.0.1:3000:3000 |
Local access only |