Docker · Lesson 3 of 8
Images & Containers
Pull, run, list and clean up.
Now let's work with real images. You will pull an image from Docker Hub, run it as a container, look at what is running, read its logs, and clean up afterwards. These few commands are the ones you will use every single day.
Pull and list images
terminal
docker pull nginx:1.27 1.27: Pulling from library/nginx Status: Downloaded newer image for nginx:1.27 docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.27 3b25b682ea82 2 weeks ago 192MB hello-world latest d2c94e258dcb 1 year ago 13.3kB
The part after the colon is the tag, usually a version. Always pin a tag instead of relying on latest.
Run a web server in a container
This starts nginx in the background (-d), gives the container a name, and maps port 8080 on your computer to port 80 inside the container (-p). Open http://localhost:8080 and you will see the nginx welcome page.
terminal
docker run -d --name web -p 8080:80 nginx:1.27 7f1e2c9a4b8d3e6f... docker ps CONTAINER ID IMAGE STATUS PORTS NAMES 7f1e2c9a4b8d nginx:1.27 Up 5 seconds 0.0.0.0:8080->80/tcp web curl -s localhost:8080 | head -4 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>
Look inside
terminal
docker logs web 172.17.0.1 - - "GET / HTTP/1.1" 200 615 "-" "curl/8.7.1" docker exec -it web sh -c "nginx -v" nginx version: nginx/1.27.2
| Command | What it does |
|---|---|
| docker ps | list running containers (add -a for stopped ones) |
| docker logs NAME | show what the container printed |
| docker exec -it NAME sh | open a shell inside a running container |
| docker stop NAME | stop it gracefully |
| docker rm NAME | delete a stopped container |
| docker rmi IMAGE | delete an image |
Clean up
terminal
docker stop web web docker rm web web docker system df TYPE TOTAL ACTIVE SIZE Images 2 0 192MB
docker system prune removes stopped containers, unused networks and dangling images in one go.
Try it yourself
✍️ Exercise: Run your own Redis
Start the official redis:7 image in the background with the name cache, check it is running, then stop and remove it.
Show hint
docker run -d --name ..., then docker ps
Show solution
terminalbash
docker run -d --name cache redis:7
docker ps
docker stop cache
docker rm cacheSummary
Key takeaways
- docker pull downloads, docker run starts a container
- -d runs in the background, -p maps ports
- ps, logs, exec, stop and rm manage containers
Next up: Your First Dockerfile — package your own app