List of Docker CLI Commands
To execute Docker CLI commands, you must first install Docker on your system based on your operating system. Refer to the Installation section for detailed setup instructions specific to macOS, Windows, and Linux.
General Commands#
- Check docker version:
docker --version - Start the docker daemon:
docker -d - Get help with Docker. Can also use
--helpon all subcommands:docker --help - Display system-wide information:
docker info
Images#
- List local images:
docker images - Pull an image from Docker Hub:
docker pull <image_name> - Delete an image:
docker rmi <image_name> - Remove all unused images:
docker image prune - Build an image from Dockerfile:
docker build -t <image_name> - Build an image from Dockerfile without the cache:
docker build -t <image_name> . -no-cache
Container#
- Create and run a container from an image, with a custom name:
docker run --name <container_name> <image_name> - Run a container with and publish a container’s port(s) to the host:
docker run -p <host_port>:<container_port> <image_name> - Run a container in the background:
docker run -d <image_name> - Start or stop an existing container:
docker start|stop <container_name> (or <container-id>) - Remove a stopped container:
docker rm <container_name> - Open a shell inside a running container:
docker exec -it <container_name> sh - Fetch and follow the logs of a container:
docker logs -f <container_name> - To inspect a running container:
docker inspect <container_name> (or <container_id>) - To list currently running containers:
docker ps - List all docker containers (running and stopped):
docker ps --allordocker ps -a - View resource usage stats:
docker container stats - Manage Persistent Storage:
docker run -v [Source]:[Destination]:[Mode]/-mount type=[type],source=[src],target=[dst][Source]: The location on your Host machine (Name of volume or absolute path).[Destination]: The absolute path inside the container.[Mode]: Optional permissions, likero(read-only) orrw(read-write).type: Eithervolume(Docker managed) orbind(Direct path).source: The volume name or host directory.target: The destination path inside the container.
Example:
docker run -d --name db_container -v postgres_data:/var/lib/postgresql/data postgres
docker run -d --name app_dev --mount type=bind,source=$(pwd)/src,target=/app/src node:latest
Docker Hub#
- Login into Docker:
docker login -u <username> - Publish an image to Docker Hub:
docker push <username>/<image_name> - Search Hub for an image:
docker search <image_name>
Conclusion#
Docker CLI commands provide complete control over images, containers, and registries directly from the terminal. By mastering these essential commands, developers can efficiently build, run, manage, and distribute containerized applications across different environments with precision and speed.