Docker Commands

Essential Docker commands for building, running, and managing containers.

Images

docker build -t myapp .

Build an image from Dockerfile in current directory

docker images

List all local images

docker pull nginx:latest

Pull an image from Docker Hub

docker rmi image_name

Remove a local image

docker tag myapp user/myapp:v1

Tag an image for pushing to registry

Containers

docker run -d -p 3000:3000 myapp

Run container in detached mode with port mapping

docker run -it ubuntu bash

Run container interactively with shell access

docker ps

List running containers

docker ps -a

List all containers including stopped ones

docker stop container_id

Gracefully stop a running container

docker rm container_id

Remove a stopped container

docker exec -it container_id bash

Open a shell inside a running container

Docker Compose

docker compose up -d

Start all services in the background

docker compose down

Stop and remove all containers and networks

docker compose logs -f

Follow logs from all services

docker compose build --no-cache

Rebuild images without using cache

docker compose ps

List running compose services

Volumes & Networks

docker volume create mydata

Create a named volume for persistent storage

docker volume ls

List all volumes

docker network create mynet

Create a custom bridge network

docker run --network mynet myapp

Run container on a specific network

Cleanup & Debug

docker system prune -a

Remove all unused images, containers, and networks

docker logs container_id

View container logs

docker inspect container_id

View detailed container/image metadata as JSON

docker stats

Live stream of container resource usage (CPU, memory)