Archive · Docker

Images and Containers: The First Ten Commands

The difference between an image and a container, and the commands you will use every day.

1 min read

  • Docker Engine 19.03.9
  • Docker Hub

An image is a read-only template. A container is a running (or stopped) instance of it.

docker pull nginx:1.18               # download an image
docker run -d --name web -p 8080:80 nginx:1.18
docker ps                            # running containers (-a for all)
docker logs -f web
docker exec -it web sh               # a shell inside the container
docker stop web && docker rm web
docker images
docker rmi nginx:1.18
docker system df                     # disk used by images and containers

-p 8080:80 maps host port 8080 to container port 80. Always pin a tag such as nginx:1.18 rather than relying on latest, which changes over time and makes builds hard to repeat.

Containers are disposable: anything written inside is lost when the container is removed. Persistent data goes in volumes, which a later post covers.