AI Hints
Home›Docker Tutorial›Docker Basics
Docker · Lesson 3 of 8

Images & Containers

Pull, run, list and clean up.

⏱ 2 min read▶ Video at 3:13🎓 Free
Watch this lesson in the Docker courseWatch on YouTube ↗Full Docker course

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
CommandWhat it does
docker pslist running containers (add -a for stopped ones)
docker logs NAMEshow what the container printed
docker exec -it NAME shopen a shell inside a running container
docker stop NAMEstop it gracefully
docker rm NAMEdelete a stopped container
docker rmi IMAGEdelete 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 cache

Summary

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