Docker Commands
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.shBasic 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 psList all containers (including stopped ones):
docker ps -aStop 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 pruneImages
Pull an image from Docker Hub:
docker pull <image>List all images:
docker imagesBuild 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 pruneVolumes
Create a volume:
docker volume create <volume_name>List volumes:
docker volume lsRemove 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:
docker network lsGet Navya PS’s stories in your inbox
Join Medium for free to get updates from this writer.
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 upStart services in detached mode:
docker-compose up -dStop services:
docker-compose downBuild or rebuild services:
docker-compose buildView service logs:
docker-compose logsList containers:
docker-compose psDockerfile Instructions
Specify the base image:
FROM <image>Set the working directory:
WORKDIR /path/to/workdirCopy 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 statsView Docker system-wide information:
docker infoDetach from a container without stopping it:
- Press
Ctrl + pfollowed byCtrl + q.
Clean up all unused containers, networks, images, and optionally, volumes:
docker system prune




