· Satrajit Sengupta · Containers  · 5 min read

Docker commands reference — everything you need for day-to-day operations

A practical reference for the Docker commands that cover 95% of real-world container operations, with context on when and why to use each one.

A practical reference for the Docker commands that cover 95% of real-world container operations, with context on when and why to use each one.

Once Docker is installed and you’ve built your first image, you’ll find yourself using the same ~20 commands for almost everything. This is a practical reference — commands are grouped by what they do, with context on behaviour that trips people up.

Container lifecycle

Check the Docker version

docker -v
docker version          # more detail: client and server versions separately

List containers

docker ps               # running containers only
docker ps -a            # all containers, including stopped ones
docker ps -l            # the most recently created container
docker ps -n 3          # the last 3 created containers

Create a container (without starting it)

docker create --name my-container nginx:alpine

Creates the container in Created status. Start it separately with docker start.

Run a container

docker run nginx:alpine                         # foreground, random name
docker run --name web nginx:alpine              # named container
docker run -itd --name web nginx:alpine         # detached, interactive, tty
docker run -d -p 8080:80 --name web nginx:alpine  # detached, port published

The -itd flags: -i keeps stdin open, -t allocates a pseudo-TTY, -d detaches. You’ll use -itd for containers you want running in the background that you also want to be able to shell into.

Start and stop

docker start my-container   # start a stopped container
docker stop my-container    # graceful stop (SIGTERM, then SIGKILL after timeout)
docker kill my-container    # immediate stop (SIGKILL)
docker restart my-container # stop + start

Interacting with running containers

Execute a command inside a container

docker exec -it my-container bash     # open a bash shell
docker exec -it my-container sh       # open sh (for minimal images without bash)
docker exec my-container ls /app      # run a single command and return

docker exec creates a new process in the container. When you exit, that process ends — but the container’s main process (PID 1) keeps running, so the container stays up.

Attach to a container’s main process

docker attach my-container

Unlike exec, attach connects your terminal to the container’s PID 1 — the main process. When you exit, that process receives the signal and the container stops. Be careful with this in production.

Copy files between host and container

# Host → container
docker cp /host/path/file.txt my-container:/container/path/

# Container → host
docker cp my-container:/container/path/file.txt /host/path/

Useful for debugging — copying log files out of a container, or injecting a config change without rebuilding the image.

View container output

docker logs my-container        # all output since container started
docker logs -f my-container     # follow (live tail)
docker logs --tail 50 my-container  # last 50 lines only

Removing containers

docker rm my-container          # remove a stopped container
docker rm -f my-container       # stop and remove (force)
docker container prune          # remove all stopped containers

You cannot docker rm a running container without -f. If you get an error, check docker ps first.

To remove a running container, stop it first or use -f:

docker stop my-container && docker rm my-container
# or
docker rm -f my-container

Images

List and inspect images

docker images                   # list all local images
docker image ls                 # same thing
docker image inspect nginx:alpine  # full metadata as JSON
docker history nginx:alpine        # each layer, size, and instruction

Pull and push

docker pull nginx:alpine                    # pull from Docker Hub
docker pull registry.example.com/my-app:v1 # pull from private registry

docker tag my-app:latest myuser/my-app:v1  # tag for pushing
docker push myuser/my-app:v1               # push to Docker Hub

Remove images

docker rmi my-app:latest        # remove by name:tag
docker rmi <image-id>           # remove by ID
docker image prune              # remove all dangling (untagged) images
docker image prune -a           # remove all unused images

You cannot remove an image that is used by any container — running or stopped. Remove the container first:

docker rm my-container && docker rmi my-image

Networking (quick reference)

docker network ls                           # list all networks
docker network inspect bridge              # details of the bridge network
docker network create my-net               # create a user-defined bridge
docker network connect my-net my-container # attach running container to network
docker network disconnect my-net my-container
docker network rm my-net
docker network prune                       # remove all unused networks

Volumes (quick reference)

docker volume ls
docker volume create my-vol
docker volume inspect my-vol
docker volume rm my-vol
docker volume prune                        # remove all unused volumes

System maintenance

docker system df                # disk usage by images, containers, volumes
docker system prune             # remove all stopped containers, unused networks, dangling images
docker system prune -a          # also removes unused images (not just dangling)
docker system prune --volumes   # include volumes in cleanup

docker system prune -a --volumes is the nuclear option — it reclaims all Docker disk space on the host. Don’t run it on a production machine without knowing what’s running.

Quick-reference table

CommandWhat it does
docker ps / docker ps -aList running / all containers
docker runCreate and start a container
docker start / docker stopStart / stop a container
docker exec -itShell into a running container
docker attachConnect to container’s PID 1
docker logs -fFollow container output
docker cpCopy files host ↔ container
docker rm / docker rm -fRemove stopped / force-remove container
docker container pruneRemove all stopped containers
docker imagesList local images
docker pull / docker pushPull from / push to registry
docker rmiRemove an image
docker image prune -aRemove all unused images
docker network ls / create / rmManage networks
docker volume ls / create / rmManage volumes
docker system dfShow disk usage
docker system prune -aClean up everything unused

Part of the Docker series on The Digital Drift. Related: Docker volumes · Docker networking

Back to Blog

Related Posts

View All Posts »