FROM
itsme-mittal / Docker cheatsheet
Docker cheatsheet
Dockerfile Instructions
|
Specifies the base image to use. |
|
Executes commands during image build. |
|
Specifies the default command to run when the container starts. |
|
Informs Docker of the ports the container listens on. |
|
Sets environment variables. |
|
Copies files/directories from source to the container filesystem. |
|
Copies files/directories from source to the container filesystem (similar to ADD, but without URL or tar extraction support). |
|
Sets the working directory for subsequent instructions. |
|
Sets the user to run subsequent commands as. |
|
Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. |
Basic Docker Commands
|
Create and run a new container from an image. |
|
List running containers. |
|
List all containers (running and stopped). |
|
Stop a running container. |
|
Start a stopped container. |
|
Restart a container. |
|
Remove a stopped container. |
|
Execute a command inside a running container. |
|
Forcefully stop a running container. |
Docker Images
|
List all available images. |
|
Download an image from Docker Hub. |
|
Remove an image. |
|
Build an image from a Dockerfile in the current directory. |
|
Push an image to Docker Hub. |
|
Tag an image for pushing to a registry. |
|
Show the history of an image. |
Docker Networking
|
List Docker networks. |
|
Create a new Docker network. |
|
Connect a container to a network. |
|
Disconnect a container from a network. |
|
Inspect a Docker network. |
|
List port mappings for a specific container. |
Docker Compose
|
Builds, (re)creates, starts, and attaches to containers defined in a |
|
Runs the |
|
Stops and removes containers, networks, volumes, and images created by |
|
Lists the containers created by |
|
View the logs of the containers. |
|
Stop running services without removing them. |
|
Start previously stopped services. |
|
Restart services. |
|
Build or rebuild services. |
Docker Volumes
|
List Docker volumes. |
|
Create a new Docker volume. |
|
Mount a volume to a container. |
|
Inspect a Docker volume. |
|
Remove a Docker volume. |
|
Mount a host directory as a volume. |
Container Resource Limits
|
Limit container’s memory usage to 512MB. |
|
Limit container’s CPU usage to 2 CPUs. |
|
Update container’s memory limit to 1GB. |
|
Display live resource usage statistics for containers. |
Docker Registry
|
Log in to a Docker registry (e.g., Docker Hub). |
|
Log out from a Docker registry. |
|
Search for images on Docker Hub. |
|
Tag an image for a private registry. |
|
Push an image to a private registry. |
Docker Network Commands
|
Create a new Docker network. Example: |
|
List all Docker networks. Example: |
|
Inspect a Docker network to view its details. Example: |
|
Connect a container to a Docker network. Example: |
|
Disconnect a container from a Docker network. Example: |
|
Remove a Docker network. Example: |
Docker Volume Commands
|
Create a new Docker volume. Example: |
|
List all Docker volumes. Example: |
|
Inspect a Docker volume to view its details. Example: |
|
Mount a Docker volume to a container. Example: |
|
Remove a Docker volume. Example: |
|
Remove all unused Docker volumes. Example: |
Docker Compose Commands
|
Build, (re)create, start, and attach to containers based on docker-compose.yml. Example: |
|
Run containers in detached mode. Example: |
|
Stop and remove containers, networks, volumes, and images created by Example: |
|
List containers. Example: |
|
View output from containers. Example: |
|
Execute a command in a running container. Example: |
Docker Image Commands
|
Show the history of an image. Example: |
|
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE. Example: |
|
Remove unused images. Example: |
|
Save one or more images to a tar archive. Example: |
|
Load an image from a tar archive. Example: |
|
Import the contents from a tarball to create an image. Example: |
Docker Container Commands
|
Display live stream of container(s) resource usage statistics. Example: |
|
Inspect changes to files or directories on a container’s filesystem. Example: |
|
Copy files/folders between a container and the local filesystem. Example: |
|
Pause all processes within a container. Example: |
|
Unpause all processes within a container. Example: |
|
Update configuration of one or more containers. Example: |
Docker System Commands
|
Show docker disk usage. Example: |
|
Remove all unused containers, networks, images (dangling and all untagged). Example: |
|
Get real time events from the server. Example: |
|
Display system-wide information. Example: |
|
Show the Docker version information. Example: |
|
Log in to a Docker registry. Example: |
ECR Authentication
Authenticate Docker CLI to AWS ECR using the AWS CLI. This command retrieves an authentication token and configures Docker to use it.
|
Authenticate Docker CLI to AWS ECR (alternative method):
|
Note: Ensure the AWS CLI is configured with appropriate credentials (IAM user/role with ECR permissions) before running these commands. If you encounter issues, double-check your AWS CLI configuration and IAM permissions. |
Creating ECR Repositories
Create a private ECR repository using the AWS CLI:
|
Create a public ECR repository (ECR Public):
|
Tagging Docker Images
Tag your Docker image with the ECR repository URI before pushing:
|
Example:
|
Pushing Images to ECR
Push the tagged Docker image to your ECR repository:
|
Pushing to ECR Public:
Replace |
Pulling Images from ECR
Pull an image from ECR:
|
Pulling from ECR Public:
|
Deleting Images and Repositories
Delete a specific image in an ECR repository:
|
Delete an entire ECR repository (ensure it’s empty first):
|
Deleting images by digest:
|
ECR Permissions and IAM
Example IAM policy for allowing users to push and pull images from ECR:
|
Important: Carefully manage ECR permissions to control who can access and modify your container images. Use IAM roles for EC2 instances or other AWS services that need to interact with ECR. |