LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 315 min read

Running Containers: run, ps, logs, exec and stop

Run containers in the foreground, interactively and in the background, then list, inspect, enter, stop, start and remove them.

On this page 12 sections

Introduction

Most of your time with Docker is spent running containers and looking after them. This lesson covers the commands you will use every day — by the end you will run a real web server in a container and manage it from start to finish.

What You Will Learn
  • How to run a one-off command, an interactive shell and a background service.
  • How to publish a port so you can reach a container from your computer.
  • How to read a container’s logs and run commands inside it.
  • The container lifecycle: created, running, exited and removed.

Running a Command in a Container

The general form is docker run [options] IMAGE [command]. Anything after the image name is the command to run inside the container:

docker run alpine echo "Hello, Docker"
Output

Click Run to see what this code prints.

Docker pulled the Alpine image, started a container, ran echo inside it, and the container stopped as soon as echo finished. A container lives only as long as its main process — this is the single most important fact about containers.

Images are named name:tag. alpine alone means alpine:latest; here Priya asks for a specific Ubuntu version and uses --rm to delete the container automatically when it exits:

docker run --rm ubuntu:24.04 grep PRETTY_NAME /etc/os-release
Output

Click Run to see what this code prints.

Listing Containers: docker ps

docker ps lists running containers. Right now there are none — both commands above finished:

docker ps
Output

Click Run to see what this code prints.

Add -a (all) to include stopped containers. The echo container is still there, in the Exited state; the Ubuntu one is gone because of --rm:

docker ps -a
Output

Click Run to see what this code prints.

ColumnMeaning
CONTAINER IDA unique ID; you can use the first few characters to refer to the container
IMAGE / COMMANDThe image it was made from and the command it ran
STATUSUp (running) or Exited, with the exit code in brackets
PORTSPublished ports, e.g. 0.0.0.0:8080->80/tcp
NAMESA name — random (two words) unless you give one with --name

Interactive Containers: -it

To work inside a container, start a shell with -it: -i keeps input open and -t gives it a terminal. Notice the prompt change — Priya is now root inside the container, whose hostname is its container ID:

docker run -it --rm ubuntu:24.04 bash
Output

Click Run to see what this code prints.

Typing exit ends bash, the container’s main process, so the container stops (and --rm removes it). This is a great way to try out a Linux distribution or a tool without installing anything.

Background Containers: -d, -p and --name

Servers should keep running in the background. Here Priya starts the Nginx web server with three important options:

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

Click Run to see what this code prints.

  • -d (detached) runs the container in the background and prints its full ID.
  • --name web gives it a name to use in later commands instead of the ID.
  • -p 8080:80 publishes a port: connections to port 8080 on Priya’s computer go to port 80 inside the container, where Nginx listens. Without it, the server would be unreachable from outside.
docker ps
Output

Click Run to see what this code prints.

The web server answers on port 8080 (you could also open http://localhost:8080 in a browser):

curl -s http://localhost:8080 | grep -i "<h1>"
Output

Click Run to see what this code prints.

Logs and exec

A background container’s output goes to its log. docker logs shows it; --tail 3 shows only the last three lines, and -f would follow new lines live. The last line is Priya’s curl request:

docker logs --tail 3 web
Output

Click Run to see what this code prints.

docker exec runs an extra command inside a running container — perfect for checking versions or looking at files:

docker exec web nginx -v
docker exec web ls /usr/share/nginx/html
Output

Click Run to see what this code prints.

With -it you get a shell inside the running container. Alpine-based images have sh rather than bash:

docker exec -it web sh
Output

Click Run to see what this code prints.

Exiting this shell does not stop the container, because the shell was an extra process — Nginx is still the main process.

Mini Challenge · Predict the Output

Priya’s web container is running. She runs docker exec -it web sh, looks around, then types exit. What is the state of the web container afterwards?

Choose one answer

Stopping, Starting and Removing

docker stop asks the main process to shut down cleanly (like kill in Linux), waiting up to 10 seconds before forcing it. A stopped container keeps its settings and files:

docker stop web
docker ps -a --filter name=web
Output

Click Run to see what this code prints.

docker start runs it again with the same settings (docker restart does stop and start in one go):

docker start web
docker ps --filter name=web --format "{{.Names}}: {{.Status}}"
Output

Click Run to see what this code prints.

(--format picks the columns you want, using Go template syntax.) Docker refuses to remove a running container, to protect you from mistakes:

docker rm web
Output

Click Run to see what this code prints.

Either stop it first, or force removal with -f:

docker rm -f web
Output

Click Run to see what this code prints.

Mini Challenge · Put It in Order

Put this container’s life in order.

  1. docker logs web — check it started correctly
  2. docker run -d --name web -p 8080:80 nginx:alpine
  3. docker stop web
  4. docker start web
  5. docker rm -f web

Exit Codes and Cleaning Up

When a container’s main process ends, its exit code becomes the result of docker run — 0 means success, anything else is an error. Scripts and CI pipelines rely on this:

docker run --rm alpine sh -c "echo working...; exit 3"
echo "exit code: $?"
Output

Click Run to see what this code prints.

Stopped containers pile up over time. docker container prune removes all of them at once (-f skips the confirmation question):

docker container prune -f
Output

Click Run to see what this code prints.

CommandWhat it does
docker run IMAGE [cmd]Create and start a container
docker ps / docker ps -aList running / all containers
docker logs [-f] NAMEShow (or follow) a container’s output
docker exec [-it] NAME cmdRun a command in a running container
docker stop / start / restart NAMEControl a container
docker rm [-f] NAMERemove a container
docker container pruneRemove all stopped containers

Common Mistakes

Avoid These Mistakes
  • Forgetting -p and wondering why the server cannot be reached.
  • Putting options after the image name: in docker run nginx -d, the -d is passed to Nginx, not to Docker.
  • Expecting a container to keep running after its main command has finished.
  • Using the same --name twice — names must be unique, so remove the old container first.

Frequently Asked Questions

docker run creates a new container from an image and starts it. docker start starts an existing, stopped container again.

Docker combines an adjective with the surname of a famous scientist or engineer, such as optimistic_hamilton. Give your own names with --name for anything you will refer to again.

They survive stopping and starting the same container, but are lost when the container is removed. Lesson 8 shows how to keep data safely with volumes.

Key Takeaways

  • A container runs as long as its main process; options go before the image name, the command after it.
  • -it gives an interactive terminal, -d runs in the background, --rm cleans up on exit.
  • -p HOST:CONTAINER publishes a port so you can reach the service.
  • docker logs shows output; docker exec runs extra commands inside a running container.
  • stop, start, rm -f and container prune manage the container lifecycle.

Summary

You can now run and manage containers confidently. Next you will look more closely at images: where they come from, what tags mean, how big they are and how to clean them up.

Next Lesson →

Working with Images: pull, tags, layers and cleanup