Images and Containers

Understand how images and containers relate, and learn common Docker commands for listing, starting, stopping, and removing them.

🎯 Introduction

By the end of this article, you will understand images, containers, and their relationship. You will also learn the Docker commands you will use most often: listing, starting, stopping, accessing, and removing containers. These commands appear throughout the remaining courses.


🧱 What Is an Image?

An image is a packaged template for software, containing the software itself and everything it needs to run.

Think of an npm package: npm install gives you code packaged by someone else. An image is similar, but packages more than code: it also includes the runtime environment. For example, the nginx image contains Nginx and its dependencies, while mysql:8 contains MySQL 8.

An image name can include a tag, separated by a colon, which usually identifies a version:

  • nginx: Without an explicit tag, Docker uses latest, conventionally used for the latest release.
  • mysql:8: Use the image tagged for MySQL 8.

🧱 What Is a Container?

A container is an instance created from an image. An image is a static template stored on disk; docker run uses it to create a running container.

The relationship is similar to classes and instances:

  • An image is like a class: A single definition that can be reused.
  • A container is like an object: An instance created from the class. You can create multiple independent instances.

A single nginx image can therefore run two containers, web1 and web2, mapped to different ports and working independently.


🛠 Common Container Commands

Use an Nginx container to try these commands. Start one first:

docker run -d --name web -p 8080:80 nginx

List running containers:

docker ps

You will see details for web, including its container ID, image, port mappings, and status. Add -a to list all containers, including stopped ones:

docker ps -a

Stop and start a container:

docker stop web
docker start web

Stopping a container does not remove it. docker start runs it again with its files and stored state intact.

View container logs:

docker logs web

This shows logs printed by the program in the container, which is useful for troubleshooting. Add -f to follow new log output as it appears.

Run commands inside a container:

docker exec -it web bash

This opens a command line inside the container. You can inspect files and run commands as you would on a separate Linux machine. Type exit to leave. The MySQL and MongoDB courses also use docker exec to access database clients.

Remove a container:

docker stop web
docker rm web
Removing a container also removes the data and files stored in its writable filesystem, and they cannot be recovered. Make sure you no longer need them before removing it. Use volumes for data you need to keep; a later article covers them.

🛠 Common Image Commands

List local images:

docker images

Download an image ahead of time (without running it):

docker pull nginx

docker run automatically downloads an image if it is not available locally. pull lets you prepare the image in advance.

Remove a local image:

docker rmi nginx
Remove all containers created from an image before removing the image itself. Otherwise, Docker reports that the image is still in use.

❓ Questions and Answers

Q1: What is the difference between docker stop and docker rm?

stop stops a container but keeps it and its data, so you can resume it with start. rm removes the container and its data. Think of stop as shutting down a computer and rm as throwing it away.

Q2: What happens if I omit the image tag?

Docker uses latest. This is fine for learning, but specify a version such as mysql:8 in real projects to avoid unexpected behavior when latest changes.

Q3: What is the container ID shown by docker ps?

It is a unique identifier Docker assigns to each container. Commands accept either a name or an ID: docker stop web and docker stop CONTAINER_ID can refer to the same container.

Q4: What if docker run fails because a port is already in use?

Another program is using that port on your computer. Choose a different host port, such as -p 8081:80, then visit localhost:8081.

Q5: What does -it in docker exec -it mean?

-i keeps standard input open, and -t allocates a terminal. Together, they let you interact with programs inside the container. Remember this combination for interactive sessions.


🧪 Practice

  1. Run two containers from the nginx image: web1 mapped to port 8081 and web2 mapped to port 8082. Use docker ps to confirm both are running, then visit each in your browser to see how one image can run multiple containers:
# Write your commands here
  1. Open a shell in web1, find the Nginx welcome page, and read its contents:
# Hint: enter the container with docker exec -it web1 bash
# The page is /usr/share/nginx/html/index.html; read it with cat
# Write your commands here

🎉 You now understand images, containers, and the commands used to manage them! Next, we will address an important question: how can volumes preserve data after a container is removed?