Understand images and containers, run an interactive one-off container and a background container, check their status, and clean up the resources. Then confirm that removing a container leaves its image behind. If you installed Docker Engine on Linux, use the access method set up in the installation guide. Prefix the examples with sudo if your user needs it to reach Docker.
Common commands at a glance#
| Command | What it does |
|---|---|
docker version | Check that the client can reach Docker Engine |
docker run --name hello-docker hello-world | Create and run the first container |
docker run --rm -it alpine:3.24 sh | Start an interactive foreground shell and remove it on exit |
docker run -d --name background-demo alpine:3.24 sleep 300 | Start a container in the background |
docker run --help | Show the options for docker run |
docker ps -a | List all containers, including exited ones |
docker logs hello-docker | Read the example container’s output, including logs from before it exited |
docker start --attach hello-docker | Start the existing container again and show its output |
docker stop background-demo | Stop the background exercise container |
docker rm hello-docker | Remove the exited test container |
docker images hello-world | List local hello-world images |
What are images and containers#
An image is a read-only template that packages the code, dependencies, configuration, and filesystem needed to run a program. Think of it as a “blueprint” or “installer package,” but it doesn’t run by itself.
A container is an instance created from an image. You can use the same image to create multiple independent containers. The diagram shows the lifecycle of hello-docker, created from the hello-world image.
flowchart TD
accTitle: How images and containers relate
accDescr: An image is a template for creating containers. docker run creates and starts one container; after its program exits, the container can still be inspected and started again. docker rm deletes it.
image["hello-world image\nread-only template"] -->|docker run: create and start| container["Container hello-docker"]
container -->|Program exits| stopped["Stopped container\nExited (0)"]
stopped -->|docker start --attach| running["Same container runs again"]
stopped -->|docker rm| removed["Container removed\nImage remains"]docker start starts the existing container again. Running docker run again creates another container; it does not resume this one.
Running your first container#
First, verify that Docker CLI and Engine can communicate:
docker versionIf you can see version information for both Client and Server, the environment is working. Now run an official hello-world container:
docker run --name hello-docker hello-worldThe first time you run this, Docker will pull the hello-world image from a remote repository (if it’s not already local), then create and start a container based on that image. The container will print a welcome message and then automatically exit.
--name hello-docker gives this container a name for easier management later. If you don’t specify a name, Docker will generate a random one.
If Docker reports that the name is already in use, do not remove a container you do not recognize. Check it with docker ps -a --filter name=hello-docker. If it belongs to an earlier exercise, choose a unique name and use it consistently in the remaining commands.
To see how to use docker run and which options it supports, run the following command.
docker run --helpNote that -h in docker run is short for --hostname, not a help option. Use --help to look up command options, for example docker ps --help.
Foreground Interaction and Background Runs#
hello-world exits on its own. To type commands inside a container, use -i to keep standard input open and -t to allocate a terminal:
docker run --rm -it alpine:3.24 shAt the container shell prompt, type exit to return to the host. --rm removes this one-off container when the shell exits.
Services and long-running tasks usually run in the background. This command starts an exercise container running sleep:
docker run -d --name background-demo alpine:3.24 sleep 300
docker ps --filter name=background-demo
docker stop background-demo-d runs the container in the background. Confirm that the listed name is the background-demo container created for this exercise before stopping it. If a container with that name already exists, choose a unique exercise name and replace it consistently in the remaining commands.
A stopped container can be started again. Remove it when the exercise is complete:
docker start background-demo
docker stop background-demo
docker rm background-demoChecking container status#
List the example container after it exits.
docker ps -a --filter name=hello-dockerdocker ps shows only running containers by default. Use -a to include all container states, including exited containers. --filter name=hello-docker matches a substring, so confirm that the NAMES column is exactly hello-docker.
Look for Exited (0) in the status column. Exit code 0 means the program completed successfully. You can start the same container again with docker start.
Reading container logs#
docker ps -a tells you whether the container exited. docker logs shows what the program printed before it exited. The hello-docker container has stopped, and its captured logs are still available while the container exists.
docker logs hello-dockerLook for Hello from Docker! in the output. In this example, Docker captures what the program writes to standard output and standard error. If an application writes exclusively to a log file, check that file or the system that collects it.
When troubleshooting a running service, use --tail 50 -f to show the last 50 lines and then stream new output. For instance, while the container in the Flask Dockerfile exercise is running, use docker logs --tail 50 -f flask-hello. Press Ctrl+C to stop following the logs. The container keeps running. Use docker logs --help to look up options such as --since, which filters logs by time.
Restarting and cleaning up the container#
A stopped container can be restarted:
docker start --attach hello-docker--attach displays the output in the current terminal, and you’ll see that welcome message again.
If you no longer need this container, delete it:
docker rm hello-dockerAfter deletion, running docker ps -a again should show that this container is gone.
Confirming the image is still there#
We deleted the container—what about the image?
docker images hello-worldThe image is still there. Deleting a container doesn’t delete the image; images are managed independently. See the image guide for an example of removing a local image.
Common misconceptions#
Container stopped ≠ container deleted
After docker run completes, the container enters an Exited state, but it’s still in the system. docker ps -a can see it, and docker start can restart it. Use docker rm to clean up this example manually.
Deleting container ≠ deleting image
A container is an instance created from an image. Deleting a container only removes that instance; the image itself is unaffected. The image can be used to create new containers and needs to be deleted separately with docker rmi.