Learn how to pull images, run containers with essential flags, manage container lifecycle, and clean up your local environment.
Images and Containers
With Docker installed, you are ready to start pulling images and running containers. This chapter walks through the core workflow you will repeat hundreds of times: fetch an image, start a container with the right options, inspect what is running, and clean up when you are done.
Pulling Images
Images live in registries. The docker pull command downloads an image to your local machine so you can start containers from it.
Image names follow the pattern [registry/][namespace/]name[:tag]. When you omit the registry, Docker defaults to Docker Hub. When you omit the tag, Docker uses latest.
Browsing available tags
Docker Hub shows available tags at https://hub.docker.com/_/nginx. For images you use regularly (Node.js, Postgres, Redis), it is worth checking the tags page to understand what variants exist - alpine variants are smaller, numbered tags are stable, slim variants have fewer tools pre-installed.
Running Your First Real Container
docker run is the most important command in your toolkit. It pulls the image if not cached locally, creates a container from it, and starts it.
Open http://localhost:8080 in your browser - you should see the nginx welcome page.
Let us unpack the flags:
Essential Flags
Detached mode (-d)
Without -d, the container runs in the foreground and its output streams to your terminal. This is fine for quick tests but impractical for servers. Use -d for long-running services.
Port mapping (-p)
Containers have their own network stack. A service inside a container is not reachable from your host unless you explicitly map a port.
Volume mounts (-v)
Containers have an ephemeral writable layer - data written inside a container is lost when the container is removed. Use -v to persist data. Volumes are covered in depth in Chapter 4; here is the syntax:
The :ro suffix makes the mount read-only inside the container.
Environment variables (-e)
Most official images are configured through environment variables, following the 12-factor app principle.
Naming containers (--name)
Without --name, Docker generates a random name like cranky_einstein. Always name containers you will reference later - it is much easier to type docker stop my-nginx than docker stop 3f9b8c21a0e2.
Interactive Mode (-it)
Some tasks require an interactive shell inside a container - exploring the filesystem, running ad-hoc commands, debugging.
The flags:
-i(--interactive) - Keep stdin open so you can type commands-t(--tty) - Allocate a pseudo-TTY (terminal), giving you a proper shell prompt
Once inside, you are in an isolated environment. Try:
When you type exit, the process exits and the container stops.
Exec into a running container
To open a shell in an already running container (without stopping it):
docker exec runs an additional process inside the container's namespace. The container keeps running when you exit this shell.
Listing Containers
Sample output of docker ps:
Stopping and Removing Containers
A stopped container still exists (you can docker start it again) and still occupies disk space in the writable layer. If you will never need the container again, remove it.
Managing Images
Removing an image only removes the local copy. The image still exists in the registry and can be pulled again any time.
Inspecting an image
Viewing Container Logs
A Practical Workflow Example
Here is the full lifecycle for running a local Postgres database for development:
Cleaning Up
Over time, stopped containers, unused images, and dangling build cache accumulate. The system prune command clears all of it:
Summary
You now have the vocabulary and the commands to work with containers day-to-day. The next step is building your own images with a Dockerfile.