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

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

ยท

5 min read

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! ๐Ÿณ๐Ÿš€

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย