Docker CLI Commands Handbook: A Reference Cheatsheet blog banner image

Docker CLI Commands Handbook: A Reference Cheatsheet

Understanding Docker:

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.

docker.jpg

Key Concepts of Docker:

Before diving into Docker's CLI, let's review some essential concepts:

  • Images: Templates containing instructions for creating containers, built using Dockerfile.
  • Containers: Isolated instances of Docker images running as processes, with their filesystem, network, and process space.
  • Registry: Repository for storing and sharing Docker images, with Docker Hub as the official public registry, and the option to set up private registries.

Docker CLI

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:

  • Building Docker Images: Users can create Docker images using a Dockerfile and the docker build command.
  • Running Containers: Users can start, stop, restart, and remove Docker containers using commands like docker run, docker stop, docker start, and docker rm.
  • Managing Images: Docker CLI enables users to list, pull, push, and remove Docker images using commands like docker images, docker pull, docker push, and docker rmi.
  • Logging and Monitoring: Users can view container logs and monitor container resource usage with commands like docker logs and docker stats.

Dockerfile Commands

  • FROM: Specify the base image for your Docker image
  • RUN : Execute commands in the Docker image during build time
  • COPY : Copy files or directories from the host machine to the Docker image
  • WORKDIR : Set the working directory for subsequent instructions in the Dockerfile
  • EXPOSE : Expose ports from the container to the host machine
  • CMD : Define the default command to execute when the container starts
  • ENV : Set environment variables in the Docker image
  • USER : Set the user or UID (user identifier) to use when running the Docker image
  • ARG : Define build-time arguments that can be passed to the Docker image

Example:


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"]

Image Management:

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>

Container Management:

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>

Network Management:

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>

Volume Management:

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>

Registry Management:

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>

System Management:

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

Docker Compose:

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

Conclusion

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!

References:

  • Docker Documentation: https://docs.docker.com/
  • Docker Hub: https://hub.docker.com/