Day 20 Essential Docker Cheat Sheet for DevOps Engineers

FauzeyaFauzeya
3 min read

Basics

CommandDescription
docker --versionCheck the installed Docker version.
docker infoDisplay system-wide information about Docker.

Images

CommandDescription
docker pull <image>Download an image from Docker Hub (or a registry).
docker imagesList all images on the local machine.
docker rmi <image>Remove an image from the local machine.
docker build -t <name>:<tag> <path>Build an image from a Dockerfile in the specified path.
docker tag <source> <target>Tag an image for easier reference.

Containers

CommandDescription
docker run <options> <image>Create and start a container from an image.
docker psList running containers.
docker ps -aList all containers (running and stopped).
docker stop <container>Stop a running container.
docker start <container>Start a stopped container.
docker restart <container>Restart a running container.
docker rm <container>Remove a stopped container.
docker exec -it <container> <command>Run a command inside a running container.
docker logs <container>View logs for a specific container.

Volumes

CommandDescription
docker volume create <volume-name>Create a new volume.
docker volume lsList all Docker volumes.
docker volume inspect <volume-name>View detailed information about a volume.
docker volume rm <volume-name>Remove a specific volume.

Networks

CommandDescription
docker network lsList all Docker networks.
docker network create <network-name>Create a new network.
docker network inspect <network-name>View detailed information about a network.
docker network rm <network-name>Remove a specific network.

Docker Compose

CommandDescription
docker-compose upStart all services defined in docker-compose.yml.
docker-compose up -dStart services in detached mode.
docker-compose downStop and remove all services, networks, and volumes.
docker-compose psList the status of all services.
docker-compose logs <service>View logs for a specific service.
docker-compose buildBuild images for services defined in docker-compose.yml.
docker-compose scale <service>=<num>Scale a specific service to a defined number of replicas.

Useful Options

OptionDescription
-dRun the container in detached mode (in the background).
-itAllocate a pseudo-TTY and keep STDIN open (interactive mode).
--rmAutomatically remove the container when it exits.
-p <host_port>:<container_port>Map a port on the host to a port in the container.
--name <container_name>Assign a name to the container.

Common Tasks

Accessing Container Shell

docker exec -it <container> /bin/bash

Removing Unused Images and Containers

docker system prune

Building and Running an Application

  1. Create a Dockerfile in your project directory.

  2. Build the image:

     docker build -t <image-name> .
    
  3. Run the container:

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

    This cheat sheet covers the most commonly used Docker commands for managing containers, images, networks, volumes, and Docker Compose. Let me know if you'd like further details or examples!

    We appreciate❤️ you taking the time to read and connect with us! Your engagement means a lot to us, and we look forward to hearing more from you📝

3
Subscribe to my newsletter

Read articles from Fauzeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fauzeya
Fauzeya

Hi there! I'm Fauzeya 👩‍💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀