Here are some of the most commonly used Docker and Docker Compose commands.


Docker Commands

Basic Commands

- CLI documentation:

docker

- Show the Docker version information:

docker version

- Display system-wide information:

docker info

- Show running containers:

docker ps

- Show all containers (including stopped ones):

docker ps -a

- List all containers id:

docker ps -a -q

- List all images:

docker images

- Pull an image from a registry (e.g., Docker Hub):

docker pull <image>

- Run a container from an image:

docker run <image>

- Stop a running container:

docker stop <container_id>

- Stop all running containers:

docker stop $(docker ps -a -q)

- Start a stopped container:

docker start <container_id>

- Remove a stopped container:

docker rm <container_id>

- Delete all stopped containers:

docker rm $(docker ps -a -q)

- Remove an image:

docker rmi <image_id>

- Remove all images:

docker rmi $(docker images -q)

- Remove unused objects:

docker system prune --all --volumes

- Rename an existing container:

docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]

- Execute a command inside a running container (e.g., bash):

docker exec -it <container_id> <command>
docker exec -it <container_id> bash

- List the logs from a running container:

docker logs <container_id>

- List the logs from a running container in real-time:

docker logs -f <container_id>

- Inspect detailed information about a container or image: 

docker inspect <container_id>

- Show port (or specific) mapping for a container:

docker port <container_id>

- Show running processes in a container:

docker top <container_id>

- Show live resource usage statistics of all containers:

docker stats

- Show live resource usage statistics of specific container:

docker stats <container_id>

- Running containers size:

docker container ls -s

- Copy files from a container to the host (or vice versa):

docker cp <container_id>:<source_path> <destination_path>

Networking

- List all networks:

docker network ls

- Create a new network:

docker network create <network_name>

- Inspect network details:

docker network inspect <network_name>

- Connect a container to a network:

docker network connect <network_name> <container_id>

- Disconnect a container from a network:

docker network disconnect <network_name> <container_id>

- Remove one or more networks:

docker network rm <network_name>

Volumes

- List all volumes:

docker volume ls

- Create a new volume:

docker volume create <volume_name>

- Remove a volume:

docker volume rm <volume_name>

- Inspect a volume:

docker volume inspect <volume_name>

Docker Compose Commands

- Check Docker Compose version:

docker compose version

- Create and start containers defined in docker-compose.yml:

docker compose up

- Start containers in detached mode:

docker compose up -d
docker compose up nginx -d

- Stop and remove containers, networks, and volumes created by docker compose up:

docker compose down

- List containers created by Docker Compose:

docker compose ps

- Start existing containers:

docker compose start

- Stop running containers:

docker compose stop

- Restart all containers:

docker compose restart

- Build images defined in docker-compose.yml:

docker compose build

- Build images without using the cache:

docker compose build --no-cache

- View logs for all services:

docker compose logs

- Follow the logs (real-time output):

docker compose logs -f

- Run a one-time command in a new container for a service:

docker compose run <service_name> <command>
docker compose run composer create-project laravel/laravel
docker compose run artisan migrate

- List all services in the docker-compose.yml:

docker compose config --services

- List all volumes in the docker-compose.yml:

docker compose config --volumes