Sitemap

Docker Commands

2 min readJul 29, 2024

--

Here’s a comprehensive list of the most commonly used Docker commands and concepts, providing a quick reference for managing containers, images, volumes, and networks.

Docker Installation

Install Docker on Linux:

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Basic Commands

Run a Docker container:

docker run <image>

Run a Docker container in the background (detached mode):

docker run -d <image>

Run a Docker container with a name:

docker run --name <name> <image>

Run a Docker container with port mapping:

docker run -p <host_port>:<container_port> <image>

List running containers:

docker ps

List all containers (including stopped ones):

docker ps -a

Stop a running container:

docker stop <container_id>

Start a stopped container:

docker start <container_id>

Remove a container:

docker rm <container_id>

Remove all stopped containers:

docker container prune

Images

Pull an image from Docker Hub:

docker pull <image>

List all images:

docker images

Build an image from a Dockerfile:

docker build -t <image_name> <path_to_dockerfile>

Tag an image:

docker tag <image_id> <repository>:<tag>

Push an image to Docker Hub:

docker push <repository>:<tag>

Remove an image:

docker rmi <image_id>

Remove all unused images:

docker image prune

Volumes

Create a volume:

docker volume create <volume_name>

List volumes:

docker volume ls

Remove a volume:

docker volume rm <volume_name>

Mount a volume to a container:

docker run -v <volume_name>:<container_path> <image>

Networks

Create a network:

docker network create <network_name>

List networks:

Get Navya PS’s stories in your inbox

Join Medium for free to get updates from this writer.

docker network ls

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 a network:

docker network rm <network_name>

Docker Compose

Start services defined in docker-compose.yml:

docker-compose up

Start services in detached mode:

docker-compose up -d

Stop services:

docker-compose down

Build or rebuild services:

docker-compose build

View service logs:

docker-compose logs

List containers:

docker-compose ps

Dockerfile Instructions

Specify the base image:

FROM <image>

Set the working directory:

WORKDIR /path/to/workdir

Copy files from host to container:

COPY <src> <dest>

Add files from host to container (can handle URLs and tar files):

ADD <src> <dest>

Run a command during image build:

RUN <command>

Set environment variables:

ENV <key>=<value>

Expose a port:

EXPOSE <port>

Set the default command for the container:

CMD ["executable", "param1", "param2"]

Set an alternative command (overrides CMD):

ENTRYPOINT ["executable", "param1", "param2"]

Advanced Commands

Inspect a container’s details:

docker inspect <container_id>

Check container logs:

docker logs <container_id>

Execute a command in a running container:

docker exec -it <container_id> <command>

View real-time resource usage statistics:

docker stats

View Docker system-wide information:

docker info

Detach from a container without stopping it:

  • Press Ctrl + p followed by Ctrl + q.

Clean up all unused containers, networks, images, and optionally, volumes:

docker system prune

--

--

Navya PS
Navya PS

Written by Navya PS

Senior Automation Engineer coding with precision, unraveling mysteries with tarot, feline nurturer https://www.linkedin.com/in/navyaps/