Skip to main content

Command Palette

Search for a command to run...

Docker Cheat Sheet ๐Ÿณ: A Guide for DevOps Engineers

Updated
โ€ข5 min read
Docker Cheat Sheet ๐Ÿณ: A Guide for DevOps Engineers
P

๐Ÿ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

๐Ÿ”น Languages & Runtimes: Python, Shell Scripting, HCL, YAML ๐Ÿ”น Cloud Technologies: AWS, Microsoft Azure, GCP ๐Ÿ”น Infrastructure Tools: Docker, Terraform, AWS CloudFormation ๐Ÿ”น Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps ๐Ÿ”น Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

๐Ÿš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. ๐Ÿ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. โš™๏ธ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. ๐Ÿ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. ๐Ÿ”ง Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.

Introduction

Docker commands are the building blocks for managing containers efficiently. This cheat sheet progresses from basic to advanced commands, providing real-time examples for a comprehensive understanding.

๐Ÿ’ก
Download the Docker Cheat Sheet from below link:


Basic Container Management ๐Ÿšข

1. Show Running Containers

docker ps

Example: Display currently running containers.

2. Stop a Docker Container

docker stop <container name>

Example: Halt a running container gracefully.

3. Run a Docker Image

docker run <image name>

Example: Execute a Docker image.

4. View Console Output

docker logs <container name>

Example: Check the console output of a Docker container.


Intermediate Container Operations ๐Ÿ”„

5. Delete Stopped Containers

docker rm $(docker ps -a -q)

Example: Clean up stopped containers.

6. Delete All Images

docker rmi $(docker images -q)

Example: Remove all Docker images on your system.

7. SSH into a Running Container

sudo docker exec -it <container name> bash

Example: Access a bash shell in a running container.

8. Map Host Port to Container Port

docker run -p <host port>:<container port> <image name>

Example: Define port mappings for a Docker container.


Advanced Docker Compose and Maven Integration ๐Ÿ”„๐Ÿ› ๏ธ

9. Docker Compose Build

docker-compose build

Example: Build containers defined in docker-compose.yml.

10. Docker Compose Start

docker-compose up -d

Example: Start containers in detached mode.

11. Rebuild and Restart with Docker Compose

docker-compose stop -t 1
docker-compose rm -f
docker-compose pull
docker-compose build
docker-compose up -d

Example: Stop, remove, pull, rebuild, and restart containers.

12. Maven Build with Docker

mvn clean package docker:build

Example: Build a Docker image using Maven.

13. Maven Docker Plugin Commands

mvn docker:start
mvn docker:stop
mvn docker:push
mvn docker:run

Example: Control Docker containers through Maven.


Docker Swarm: Scaling Containers ๐Ÿ

14. Initialize Docker Swarm

docker swarm init

Example: Enable Docker Swarm on the first node.

15. Add a Node to Swarm Cluster

docker swarm join --token <token> --listen-addr <ip:port>

Example: Expand the Swarm cluster with a new node.

16. List Swarm Nodes and Services

docker node ls
docker service ls

Example: View nodes and services in the Swarm.


Real-time Scenarios ๐ŸŽ‰

  • ๐Ÿšข Basic Operations: Display running containers, stop, run, and check logs.

  •     docker ps
        docker stop <container name>
        docker run <image name>
        docker logs <container name>
    
  • ๐Ÿ”„ Intermediate Actions: Remove stopped containers, delete images, access a running container, and define port mappings.

  •     docker rm $(docker ps -a -q)
        docker rmi $(docker images -q)
        sudo docker exec -it <container name> bash
        docker run -p <host port>:<container port> <image name>
    
  • ๐Ÿ”„๐Ÿ› ๏ธ Docker Compose and Maven: Build, start, stop, push, and run containers with Docker Compose and Maven.

  •     docker-compose build
        docker-compose up -d
        mvn clean package docker:build
        mvn docker:start
    
  • ๐Ÿ Docker Swarm Scaling: Initialize Swarm, add a node, and list nodes and services.

  •     docker swarm init
        docker swarm join --token <token> --listen-addr <ip:port>
        docker node ls
        docker service ls
    

Docker Networking and Port Mapping ๐ŸŒ

17. Share Storage with a Container

docker run -v <host path>:<container path> <image name>

Example: Share storage between a host system and a Docker container.

18. Map Host Port to Container Port

docker run -p <host port>:<container port> <image name>

Example: Define port mappings for a Docker container.


Advanced Dockerfile Configurations โš™๏ธ

19. Adding Oracle Java to an Image

# Example Dockerfile snippet
FROM centos:latest

ENV JAVA_VERSION 8u31
ENV BUILD_VERSION b13

# Upgrading system
RUN yum -y upgrade
RUN yum -y install wget
# ... (rest of the Java installation steps)

Example: Incorporate Oracle Java into your Docker image.

20. Adding a Spring Boot Executable JAR to an Image

# Example Dockerfile snippet
FROM openjdk:8-jre-alpine

ADD /maven/myapp-0.0.1-SNAPSHOT.jar myapp.jar
RUN sh -c 'touch /myapp.jar'

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myapp.jar"]

Example: Prepare a Docker image for a Spring Boot application.


Docker Commands with Emoji Usage ๐ŸŽ‰

  • ๐Ÿšฎ Cleaning Up: Use delete commands with emojis for easy identification.

  •     docker rm $(docker ps -a -q)
    
  • ๐Ÿ”„ Automation: Employ Docker Compose and Maven commands for seamless automation.

  •     docker-compose up -d
        mvn clean package docker:build
    
  • ๐Ÿ› ๏ธ Dockerizing Apps: Utilize Java and Spring Boot examples for adding functionalities to Docker images.

  •     ADD /maven/myapp-0.0.1-SNAPSHOT.jar myapp.jar
    
  • ๐Ÿƒโ€โ™‚๏ธ Daily Use Commands: Run, stop, and view logs with emojis for quick recognition.

  •     docker stop <container name>
    
  • ๐ŸŒ Networking: Port mapping and volume sharing commands with emojis for clarity.

  •     docker run -p <host port>:<container port> <image name>
    
  • ๐Ÿ”„ Maven Integration: Maven commands with emojis for building, starting, stopping, and pushing Docker images.

  •     mvn clean package docker:build
        mvn docker:start
    
  • ๐Ÿ Docker Swarm: Swarm commands annotated with emojis for initiation, node management, and service handling.

  •     docker swarm init
        docker node ls
    

Real-world Use Cases ๐ŸŒ

21. Continuous Integration with Jenkins

docker-compose stop -t 1
docker-compose rm -f
docker-compose pull
docker-compose build
docker-compose up -d

Scenario: CI builds with Jenkins, ensuring the latest artifacts are used.

22. Multi-environment Deployment

docker run -p 8080:8080 <image name>

Scenario: Deploying applications on different environments using port mapping.

23. Dynamic Scaling in Cloud Environments

docker service ls
docker service rm <service name>

Scenario: Scaling services dynamically in a cloud environment using Docker Swarm.

24. Efficient Resource Utilization

docker-compose logs -f

Scenario: Real-time monitoring of container logs for resource utilization analysis.


Conclusion

This extended Docker cheat sheet covers a range of commands, from basic container management to advanced Dockerfile configurations and real-world use cases. Whether you are a beginner or an experienced DevOps engineer, these commands and examples will enhance your Docker proficiency. Happy Dockerizing! ๐Ÿณ๐Ÿš€

P

Nice article!

M

nice one!

More from this blog

D

DeployToCloud

405 posts

๐Ÿ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow ๐Ÿš€