Chapter 6: Docker Images and Containers
6.4 build, run, ps, logs, exec, stop
Why. These six commands are daily Docker muscle.
# build an image and name it
docker build -t badge-api-hello:0.1 .
# run in the background, publish port (host:container)
docker run -d --name badge-api -p 8080:8080 badge-api-hello:0.1
# list running containers
docker ps
# logs (follow)
docker logs -f badge-api
# shell inside (if the image has a shell)
docker exec -it badge-api bash
# alpine images may need: docker exec -it badge-api sh
# stop and remove
docker stop badge-api
docker rm badge-api
| Command | Use when |
|---|---|
docker build -t name:tag . |
Dockerfile ready; . is build context |
docker run … |
Start a container from an image |
docker ps |
See running containers (-a includes stopped) |
docker logs |
Read stdout/stderr of the app |
docker exec |
Run a command inside a running container |
docker stop / docker rm |
Stop and delete the container instance |
What you see after a good run: docker ps shows badge-api, ports column like 0.0.0.0:8080->8080/tcp, and curl http://localhost:8080/health (or your path) returns OK.
Steps — build and curl
- From the folder with the Dockerfile:
docker build -t badge-api-hello:0.1 . - Run:
docker run -d --name badge-api -p 8080:8080 badge-api-hello:0.1 docker ps— confirm status Up.curl -i http://localhost:8080/health(adjust path to your app).docker logs badge-api— read startup lines.docker stop badge-api && docker rm badge-apiwhen finished.
Ravindra Bagale's Tip
Build context . motha asel tar slow aani risky — .dockerignore ne .git / node_modules / .env baher theva. Secrets image layer madhe jau nayet. Bilkul visru naka!