Table of contents
- Introduction
- Basic Container Management ๐ข
- Intermediate Container Operations ๐
- Advanced Docker Compose and Maven Integration ๐๐ ๏ธ
- Docker Swarm: Scaling Containers ๐
- Real-time Scenarios ๐
- Docker Networking and Port Mapping ๐
- Advanced Dockerfile Configurations โ๏ธ
- Docker Commands with Emoji Usage ๐
- Real-world Use Cases ๐
- Conclusion
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.
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! ๐ณ๐