Course page
Log in

You are watching a free preview. Enroll to open all 26 lessons, track progress and ask the course tutor.

Enroll for $34.99

Section 1: Containers from first principles

Running containers: the commands you will use daily

Article 11 min Lesson 2 of 26 Preview

Let us get comfortable with the core commands by running a real service.

docker run -d --name web -p 8080:80 nginx:1.27
  • -d runs the container in the background (detached).
  • --name web gives it a name you can use instead of an id.
  • -p 8080:80 publishes port 80 inside the container on port 8080 of your machine.

Open http://localhost:8080 and you see the nginx welcome page.

Looking at what is running

docker ps              # running containers
docker ps -a           # including stopped ones
docker logs web        # output from the container's main process
docker logs -f web     # follow the logs live

Getting inside

docker exec -it web sh

exec runs an extra process in a running container. Here it opens a shell, so you can look around: ls /usr/share/nginx/html, cat /etc/os-release. Exit with exit. The container keeps running because you only stopped the shell, not the main process.

Stopping and removing

docker stop web
docker rm web

A stopped container still exists, with its writable layer. Removing it deletes that layer. Anything you changed inside, without a volume, is gone, which is exactly why containers are treated as disposable.

Environment variables

Most images are configured through environment variables:

docker run -d --name db -e POSTGRES_PASSWORD=devpass -p 5432:5432 postgres:17

Tags and versions

The part after the colon is a tag. Avoid latest in anything you share: it changes over time and different machines may get different versions. Pin a specific version such as postgres:17 or, for full reproducibility, an image digest.

Cleaning up

Over time, stopped containers, unused images and build cache fill your disk.

docker system df       # see what is using space
docker system prune    # remove stopped containers, unused networks and dangling images

Read the prompt before confirming. Add --volumes only if you are sure you do not need the data in unused volumes.

← → switch lessons