G
GuideDevOps
Lesson 4 of 13

Docker CLI Commands

Part of the Docker tutorial series.

Efficiency in Docker comes from mastering the Command Line Interface (CLI). Here are the essential commands every DevOps engineer uses daily.

1. Lifecycle Commands

Run and Interactive Mode

Run a container and get a shell inside it.

Action:

docker run -it --name test-ubuntu ubuntu bash

Result:

root@45eb2345a1b2:/#

(You are now inside the container. Type exit to leave)

Execute a Command in a Running Container

Action:

docker exec -it my-web-server ls /etc/nginx

Result:

conf.d	fastcgi_params	mime.types  modules  nginx.conf ...

2. Inspection & Logging

View Container Logs

Crucial for troubleshooting why a container won't start.

Action:

docker logs --tail 5 my-web-server

Result:

2026/04/10 12:00:01 [notice] 1#1: using the "epoll" event method
2026/04/10 12:00:01 [notice] 1#1: nginx/1.25.1
...

Inspect Container Details

Returns a massive JSON with every detail about the container (IP, Volumes, etc.).

Action:

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-web-server

Result:

172.17.0.2

3. Cleanup Commands

Docker can quickly consume your disk space.

Remove Unused Data (The Prune)

Action:

docker system prune -f

Result:

Deleted Containers:
...
Deleted Networks:
...
Total reclaimed space: 1.45GB

Command Quick Reference

CommandDescription
docker buildBuild an image from a Dockerfile
docker pullDownload an image from a registry
docker runCreate and start a container
docker stopStop a running container
docker rmRemove a stopped container
docker rmiRemove an image
docker logsFetch logs of a container
docker execRun a command in a running container

Summary

  • Use -it for interactive shells.
  • Use exec for running commands in existing containers.
  • Use prune regularly to save disk space.