Skip to content

Docker — Commands

Core Docker CLI commands for day-to-day container and image management.

Version and general info

Check what version of Docker is installed, and get a general summary of the Docker installation (storage driver, number of containers/images, etc.):

docker -v
docker info

Listing containers and images

List running containers:

docker ps

or, equivalently:

docker container ls

List images available locally:

docker images ps

Running containers

Run a container in the background (detached), so it doesn't tie up the terminal:

docker container run -d hello-world

Give the container a custom name instead of Docker's auto-generated one, by appending :name:

docker container run hello-world:name

Publish a container on a port so it's reachable from outside the host — this example runs nginx detached and maps host port 80 to container port 80:

docker container run --detach --publish 80:80 --name n1 nginx

Stop a running container:

docker container stop <container name>

Open an interactive shell inside a running container — useful for troubleshooting or inspecting what's happening on the inside:

docker exec -it <docker name> <command(bash)>

Inspecting containers

Get detailed metadata about a container or image (config, mounts, network settings, etc.):

docker inspect <docker name>

Get all logs for a container:

docker logs <image ID>

Get a live view of a container's resource usage (CPU, memory, network I/O):

docker stats <image ID>

Get just the container's IP address from its network settings:

docker inspect --format='{{range.NetworkSettings.Networks}}{{IPAddress}}{{end}}' <image ID>

Docker tags

A Docker tag is a reference to a specific docker image (e.g. a version or variant of it).

Building images with a Dockerfile

Docker builds images automatically by reading instructions from a Dockerfile — a text file containing every command a user could otherwise type manually to assemble an image.

The first instruction in a Dockerfile is normally FROM, which decides what base OS/image the new image will build on top of.

Build an image from a Dockerfile in the current directory (the . means "build context is here"):

docker build .

Networking

List all Docker networks:

docker network ls

Inspect a specific network:

docker network inspect <docker name>

Create a network with a chosen driver (network type):

docker network create --driver <type of network> <name of the network>

Run a container attached to a specific network:

docker run --network=<network name> -d -it Ubuntu

Create a network with an explicit subnet:

docker network create --driver <type of network> --subnet 172.25.0.0/16 <network name>

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. Instead of starting each container by hand, you describe all of an application's services in a single YAML ("Yet Another Markup Language") file, then bring the whole stack up or down together.

Check the installed Compose version:

docker-compose -v