Docker is an open-source platform that allows developers to bundle their applications and necessary components into containers. These containers are self-contained environments, housing all elements required for an application to operate, such as code, runtime, libraries, and configurations.
Before diving into Docker's CLI, let's review some essential concepts:
The Docker CLI (Command Line Interface) is a tool that allows users to interact with Docker containers and manage Docker images and containers from the command line. With the Docker CLI, users can perform various tasks such as:
docker build
command.docker run
, docker stop
, docker start
, and docker rm
.docker images
, docker pull
, docker push
, and docker rmi
.docker logs
and docker stats
.FROM
: Specify the base image for your Docker imageRUN
: Execute commands in the Docker image during build timeCOPY
: Copy files or directories from the host machine to the Docker imageWORKDIR
: Set the working directory for subsequent instructions in the DockerfileEXPOSE
: Expose ports from the container to the host machineCMD
: Define the default command to execute when the container startsENV
: Set environment variables in the Docker imageUSER
: Set the user or UID (user identifier) to use when running the Docker imageARG
: Define build-time arguments that can be passed to the Docker imageExample:
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 80
ENV NAME Application
CMD ["python", "app.py"]
Commands related to handling Docker images, such as building, tagging, and removing images.
List Available Images: List all locally available Docker images
docker images
Build Image: Build an image from a Dockerfile.
docker build -t <image_name>:<tag> <path/to/Dockerfile>
Remove Image: Remove one or more images.
docker rmi <image>
Tag Image: Tag an image to a repository.
docker tag <source_image>:<tag> <target_repository>/<target_image>:<tag>
Commands for managing Docker containers, including creating, starting, stopping, and removing containers.
List Running Containers: List all running containers
docker ps
List All Containers: List all containers (running and stopped)
docker ps -a
Create Container: Create a new container from an image (does not start it)
docker create <image>
Start Container: Start a stopped container
docker start <container>
Stop Container: Stop a running container
docker stop <container>
Remove Container: Remove one or more containers
docker rm <container>
Execute Command in Container: Execute a command inside a running container
docker exec -it <container> <command>
Fetch Container Logs: Fetch the logs of a container
docker logs <container>
Commands for managing Docker networks such as creating, listing, connecting, and disconnecting containers from networks.
List Networks: List all Docker networks
docker network ls
Inspect Network: Display detailed information about a network
docker network inspect <network_name>
Create Network: Create a new Docker network
docker network create <network_name>
Connect Container to Network: Connect a container to a network
docker network connect <network_name> <container_name>
Disconnect Container from Network: Disconnect a container from a network
docker network disconnect <network_name> <container_name>
Remove Network: Remove a Docker network
docker network rm <network_name>
Commands related to Docker volumes, including creating, listing, inspecting, mounting, and removing volumes.
List Volumes: List all Docker volumes
docker volume ls
Inspect Volume: Display detailed information about a volume
docker volume inspect <volume_name>
Create Volume: Create a new Docker volume
docker volume create <volume_name>
Mount Volume: Mount a volume to a container
docker run -v <volume_name>:<container_path> <image_name>
Remove Volume: Remove a Docker volume
docker volume rm <volume_name>
Commands for interacting with Docker registries, like logging in, logging out, searching, and managing registry authentication.
Login to a Registry: Log in to a Docker registry.
docker login <registry_url>
Logout from a Registry: Log out from a Docker registry.
docker logout <registry_url>
Search for Images in Registry: Search for Docker images in a registry.
docker search <image_name>
Pull Image from Registry: Pull an image from a Docker registry.
docker pull <registry_url>/<image_name>
Push Image to Registry: Push an image to a Docker registry.
docker push <local_image_name> <registry_url>/<image_name>
Commands for managing Docker system resources, including checking system information, managing Docker daemon, and configuring Docker settings.
Display Docker System Information: Display detailed information about the Docker system.
docker system info
Prune Unused Data: Remove unused data like stopped containers, networks not used by any container, and dangling images.
docker system prune
Start Docker Daemon: Start the Docker daemon.
sudo systemctl start docker
Check Docker Daemon Status: Check the status of the Docker daemon.
sudo systemctl status docker
Restart Docker Daemon: Restart the Docker daemon.
sudo systemctl restart docker
Commands specific to Docker Compose, a tool for defining and running multi-container Docker applications.
Start Docker Compose: Start Docker Compose services defined in the docker-compose.yml file.
docker-compose up
Start Docker Compose in Detached Mode: Start Docker Compose services in the background.
docker-compose up -d
Stop Docker Compose: Stop Docker Compose services.
docker-compose down
List Docker Compose Services: List Docker Compose services.
docker-compose ps
With this handy docker cheatsheet, you'll navigate Docker easily. Whether you're experienced or new, mastering these commands will make your work smoother and boost productivity. Happy Dockering!
https://docs.docker.com/
https://hub.docker.com/