| |

LFCS 153 ๐Ÿง Docker โ€” Basic Commands

โฑ๏ธ Estimated read: 10 min | |

The previous chapter covered the conceptual model: the client-server architecture, the image-container-registry triad, and the difference between containers and virtual machines. This chapter covers the commands that implement that model: pulling images, running containers, inspecting the state, executing commands inside the container, and cleaning up. The LFCS exam tests the command syntax, the flag semantics, and the lifecycle. The critical trap is the docker run command: it creates a new container every time it is executed. The docker start command starts an existing stopped container. The docker exec command runs a command in a running container. The three commands are often confused, and the confusion is the source of the duplicated containers and the missing state.

Key point: docker run creates and starts a new container from an image. docker start starts an existing container. docker exec runs a command inside a running container. docker ps lists the running containers; docker ps -a lists all containers, including the stopped ones. The -d flag detaches the container to the background. The -it flags allocate an interactive terminal. The --rm flag removes the container when it stops.


Why Docker commands require a specific mental model

The create versus start problem. The docker run command is the combination of docker create and docker start. It creates a new container from the image and starts it. The container has a new ID and a new name (if not specified). The docker start command starts an existing container, preserving its state, its filesystem, and its configuration. The docker run on the same image creates a second container; the two containers are independent.

The attach versus detach problem. The docker run -it attaches the container to the terminal. The container’s stdin, stdout, and stderr are connected to the terminal. The docker run -d detaches the container. The container runs in the background, and the command returns the container ID. The docker attach reconnects to the running container. The docker exec -it opens a new shell inside the running container, which is the safer way to interact with a running service.

The container lifecycle problem. The container has states: created, running, paused, stopped, and exited. The docker run transitions from created to running. The docker stop sends SIGTERM and transitions to stopped. The docker kill sends SIGKILL. The docker rm removes the stopped container. The docker ps shows the running containers; the docker ps -a shows all containers, including the exited ones.

The --rm problem. The docker run --rm removes the container automatically when it stops. This is the correct flag for the one-off commands and the short-lived jobs. Without it, the stopped containers accumulate and consume disk space. The docker container prune removes the stopped containers in bulk.

The port mapping problem. The -p host:container maps the host port to the container port. The -p 8080:80 maps the host 8080 to the container 80. The -P flag publishes all the exposed ports to the random host ports. The port mapping is required for the container to be reachable from the host network.

The volume problem. The -v host:container mounts the host directory into the container. The changes are persisted on the host. The --mount flag is the modern alternative with the explicit options. The volumes are the persistence mechanism; the container filesystem is ephemeral.

The trade-off. The docker run is the simplest way to start a container, but it creates a new container every time. The docker start preserves the state, but it is only for the existing containers. The docker exec is the correct way to interact with the running container without stopping it.


a. The image commands and the container run

The docker pull downloads the image. The docker images lists the local images. The docker run creates and starts the container.

# Pull an image
docker pull nginx:1.25

# List the local images
docker images

# Run a container in the background
docker run -d --name web -p 8080:80 nginx:1.25

# Run a container with a shell
docker run -it --rm ubuntu:22.04 bash

The docker pull nginx:1.25 downloads the specific tag. The :latest tag is the default when no tag is specified, but it is the moving target. The pinned tag is the reproducibility.

The docker run -d starts the container in the background. The -d is the detach. The --name web assigns the name. The -p 8080:80 maps the port.

The docker run -it --rm ubuntu:22.04 bash starts the interactive container. The -i keeps the stdin open, the -t allocates the terminal, the --rm removes the container on exit. The bash is the command to run.


b. The container state and the exec

The docker ps lists the running containers. The docker ps -a lists all containers. The docker inspect shows the detailed state. The docker exec runs the command inside the running container.

# List running containers
docker ps

# List all containers
docker ps -a

# Inspect a container
docker inspect web

# Execute a command inside a running container
docker exec -it web bash

# Execute a single command
docker exec web cat /etc/nginx/nginx.conf

The docker ps output has the columns: CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, and NAMES. The docker ps -a adds the stopped containers. The STATUS shows the Up 5 minutes or the Exited (0) 2 minutes ago.

The docker exec -it web bash opens an interactive shell in the running container. The docker exec web cat /etc/nginx/nginx.conf runs the single command and returns the output. The exec is the correct way to inspect the running container.


c. The stop, start, rm, and logs

The docker stop stops the running container. The docker start starts the stopped container. The docker rm removes the stopped container. The docker logs shows the container output.

# Stop the container
docker stop web

# Start the stopped container
docker start web

# Show the logs
docker logs web

# Follow the logs
docker logs -f web

# Remove the container
docker rm web

# Remove the running container (with stop)
docker rm -f web

The docker stop sends SIGTERM, waits 10 seconds, and sends SIGKILL. The docker stop -t 30 web changes the timeout to 30 seconds. The docker kill web sends SIGKILL immediately.

The docker start web starts the stopped container. The container preserves its filesystem and its configuration. The docker run on the same image creates a new container, not the start of the existing one.

The docker logs web shows the stdout and stderr. The -f flag follows the output, which is the equivalent of tail -f. The --tail 100 shows the last 100 lines.

The docker rm web removes the stopped container. The docker rm -f web stops and removes the running container.


Complete Example Session

# ============================================
# PART 1: PULL THE IMAGE
# ============================================

docker pull nginx:1.25
# ============================================
# PART 2: LIST THE IMAGES
# ============================================

docker images
# nginx  1.25  abc123  2 weeks ago  187MB
# ============================================
# PART 3: RUN THE CONTAINER
# ============================================

docker run -d --name web -p 8080:80 nginx:1.25
# ============================================
# PART 4: LIST THE CONTAINERS
# ============================================

docker ps
# web  nginx:1.25  ...  Up 5 seconds  0.0.0.0:8080->80/tcp
# ============================================
# PART 5: EXEC INTO THE CONTAINER
# ============================================

docker exec -it web bash
# Inside the container
cat /etc/nginx/nginx.conf
exit
# ============================================
# PART 6: SHOW THE LOGS
# ============================================

docker logs web
# ============================================
# PART 7: STOP AND START
# ============================================

docker stop web
docker ps -a
# web  Exited (0) 2 seconds ago
docker start web
docker ps
# web  Up 1 second
# ============================================
# PART 8: REMOVE THE CONTAINER
# ============================================

docker stop web
docker rm web
# ============================================
# PART 9: RUN A ONE-OFF CONTAINER
# ============================================

docker run -it --rm ubuntu:22.04 bash
# The container is removed on exit.

The nine parts trace the basic commands: pull, images, run, ps, exec, logs, stop/start, rm, and one-off.


Quick Reference

Image Commands

CommandPurpose
docker pull <image>Download image
docker imagesList images
docker rmi <image>Remove image

Container Commands

CommandPurpose
docker run <image>Create and start
docker psList running
docker ps -aList all
docker start <id>Start existing
docker stop <id>Stop running
docker rm <id>Remove stopped
docker rm -f <id>Stop and remove

Inspection

CommandPurpose
docker inspect <id>Full state
docker logs <id>Stdout/stderr
docker logs -f <id>Follow logs
docker exec -it <id> bashShell inside

Run Flags

FlagPurpose
-dDetach
-itInteractive terminal
--rmRemove on exit
--nameAssign name
-pPublish port
-vMount volume

Best Practices

โœ… Do This:

docker run -d --name web -p 8080:80 nginx:1.25
<!-- Use the pinned tag and the name -->              # โœ…
docker run -it --rm ubuntu:22.04 bash
<!-- Use --rm for the one-off containers -->          # โœ…
docker exec -it web bash
<!-- Use exec to enter the running container -->     # โœ…

โŒ Don’t Do This:

docker run nginx
# Every time creates a new container
<!-- Use docker start for the existing -->           # โŒ
# Forget the --rm on the one-off containers
<!-- Stopped containers accumulate -->               # โŒ
# Use docker attach for the debugging
<!-- exec is safer; attach stops when you Ctrl+C -->  # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Duplicated containersUsed run repeatedlyUse start
Port conflictHost port already in useChange the host port
Container exitsNo foreground processUse -d or tail -f /dev/null
Cannot execContainer not runningdocker start first
Stopped containers pile upNo --rmdocker container prune

Real-World Examples

1. Pull Image

docker pull nginx:1.25

2. List Images

docker images

3. Run Detached

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

4. Run Interactive

docker run -it --rm ubuntu bash

5. List Containers

docker ps -a

6. Exec

docker exec -it web bash

7. Logs

docker logs -f web

8. Stop

docker stop web

9. Remove

docker rm web

10. Prune

docker container prune

Visual

Lifecycle

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  docker run <image>                          โ”‚
โ”‚  โ†’ Create + start                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Running                                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚                       โ”‚
           โ”‚ docker stop           โ”‚ docker kill
           โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Stopped             โ”‚  โ”‚  Killed              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚                         โ”‚
           โ”‚ docker start            โ”‚ docker rm
           โ–ผ                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Running             โ”‚  โ”‚  Removed             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Run vs Start vs Exec

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  docker run โ†’ new container                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  docker start โ†’ existing container           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  docker exec โ†’ command in running container  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Pull imagedocker pull
List imagesdocker images
Create and startdocker run
List runningdocker ps
List alldocker ps -a
Start existingdocker start
Stopdocker stop
Removedocker rm
Enter containerdocker exec -it
Logsdocker logs

Key takeaways:

  • docker run creates a new container. Every execution creates a new container with a new ID. The docker start starts an existing container and preserves its state. The two commands are not interchangeable.
  • The -d flag detaches the container. The -it flag attaches the terminal. The --rm flag removes the container on exit. The three flags cover the three use cases: background service, interactive shell, and one-off command.
  • The docker exec enters the running container. It opens a new shell or runs a command. The docker attach connects to the existing stdin/stdout, which is the different behavior. The exec is the safer choice for the debugging.
  • The docker logs shows the output. The -f flag follows the output. The logs are the container’s stdout and stderr, not the files inside the container.
  • The stopped containers accumulate. The --rm flag prevents the accumulation. The docker container prune removes the existing stopped containers.

Remember: The run is create + start. The start is the existing container. The exec is the running container. The ps is the list. The logs is the output. The rm is the removal. The exam tests the distinction between the run, the start, and the exec.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!