LFCS 153 ๐ง Docker โ Basic Commands
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
| Command | Purpose |
|---|---|
docker pull <image> | Download image |
docker images | List images |
docker rmi <image> | Remove image |
Container Commands
| Command | Purpose |
|---|---|
docker run <image> | Create and start |
docker ps | List running |
docker ps -a | List all |
docker start <id> | Start existing |
docker stop <id> | Stop running |
docker rm <id> | Remove stopped |
docker rm -f <id> | Stop and remove |
Inspection
| Command | Purpose |
|---|---|
docker inspect <id> | Full state |
docker logs <id> | Stdout/stderr |
docker logs -f <id> | Follow logs |
docker exec -it <id> bash | Shell inside |
Run Flags
| Flag | Purpose |
|---|---|
-d | Detach |
-it | Interactive terminal |
--rm | Remove on exit |
--name | Assign name |
-p | Publish port |
-v | Mount 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
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Duplicated containers | Used run repeatedly | Use start |
| Port conflict | Host port already in use | Change the host port |
| Container exits | No foreground process | Use -d or tail -f /dev/null |
| Cannot exec | Container not running | docker start first |
| Stopped containers pile up | No --rm | docker 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
| Item | Value |
|---|---|
| Pull image | docker pull |
| List images | docker images |
| Create and start | docker run |
| List running | docker ps |
| List all | docker ps -a |
| Start existing | docker start |
| Stop | docker stop |
| Remove | docker rm |
| Enter container | docker exec -it |
| Logs | docker logs |
Key takeaways:
docker runcreates a new container. Every execution creates a new container with a new ID. Thedocker startstarts an existing container and preserves its state. The two commands are not interchangeable.- The
-dflag detaches the container. The-itflag attaches the terminal. The--rmflag removes the container on exit. The three flags cover the three use cases: background service, interactive shell, and one-off command. - The
docker execenters the running container. It opens a new shell or runs a command. Thedocker attachconnects to the existing stdin/stdout, which is the different behavior. Theexecis the safer choice for the debugging. - The
docker logsshows the output. The-fflag follows the output. The logs are the container’s stdout and stderr, not the files inside the container. - The stopped containers accumulate. The
--rmflag prevents the accumulation. Thedocker container pruneremoves 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.
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!