Docker Command Reference
Essential Docker commands for managing containers, images, networks, and volumes.
Key Concepts
docker runalways creates a new container;docker startuses an existing stopped container.- You cannot remove a running container — stop it first, or use
--force. - Containers communicate via their names as DNS, not IP addresses.
Container Management
# Run a container (host_port:container_port)
docker container run --publish 8081:80 --detach --name nginx8081 nginx
# Run with environment variables
docker container run -d -e MARIADB_USER=db_user -e MARIADB_PASSWORD=db_pass -e MARIADB_DATABASE=frontend_app -e MARIADB_ROOT_PASSWORD=mariadb_root_pass --name my_backend mariadb:latest
# List running containers
docker container ls
docker ps
# List all containers (including stopped)
docker ps -a
# Stop and remove a container
docker container stop <name>
docker container rm <name>
# Force remove a running container
docker container rm -f <name>
# Execute a command inside a running container
docker exec -it <name> bash
# View container logs
docker logs -f <name>
# Inspect container details
docker inspect <name>
Image Management
# List local images
docker image ls
# Pull an image
docker pull nginx:alpine
# Build an image from a Dockerfile
docker build -t myapp:1.0 .
# Remove an image
docker image rm <image_id>
# Remove all unused images
docker image prune -a
Volume and Network
# Mount a host directory into a container
docker run -v /host/path:/container/path myimage
# List networks
docker network ls
# Create a custom bridge network
docker network create mynetwork
# Run container on custom network
docker run --network mynetwork --name app myimage
Docker Compose
# Start services in detached mode
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs -f
# Rebuild and restart
docker-compose up -d --build
System Cleanup
# Remove stopped containers, unused networks, dangling images, build cache
docker system prune
# Remove everything including volumes
docker system prune -a --volumes