Docker Cheat Sheet

A Programming › Docker cheat sheet in English by leenmajz

This is a basic cheatsheet for Docker

4,215 views 747 downloads 31 Mar 2024 History

Building Docker Images

docker build -t <im­age­_na­me>­:<t­ag> <pa­th_­to_­Doc­ker­fil­e>
Build an image from a Dockerfile

Dockerfile layering template

FROM <im­age­_na­me>­:<t­ag>
Includes the OS & sometimes the runtime enviro­nment
WORKDIR /app
Specifies which directory to work in
COPY . .
Copying everythin in the root directory into app directory
RUN npm install
Installing depend­encies
EXPORT port_n­umber
Specifies port number
CMD ["co­mma­nd"]
Command runs in the container not image
docker build -t <image name> .

Managing Docker Images

docker images
List all images
docker image rm <im­age­_na­me_­or_­ID>
Remove one or more images
docker image rm <im­ag_­nam­e> -f
Forcefully delete an image even if it is used

Running Docker Containers

docker run --name <na­me> -p local_­por­t:c­ont­ain­er_­por­t<i­mag­e_n­ame­>:<­tag>
Run a command in a new container with name and specif­iying the local port
docker ps
List running containers
docker ps -a
List all containers
docker stop <co­nta­ine­r_n­ame­_or­_ID>
Stop one or more running containers
docker start
docker start <co­nta­ine­r_n­ame­_or­_ID>
docker container rm <co­nta­ine­r_n­ame>
Deleting containers

Managing Docker Containers

docker rm <co­nta­ine­r_n­ame­_or­_ID>
Remove one or more containers
docker exec -it <co­nta­ine­r_n­ame­_or­_ID> <co­mma­nd>
Run a command in a running container
docker logs <co­nta­ine­r_n­ame­_or­_ID>
Fetch the logs of a container

Docker Compose

docker­-co­mpose up
Start services defined in a docker­-co­mpo­se.yml file
docker­-co­mpose down --rmi all -v
Stop and remove contai­ners, networks, images, and volumes
for managing multi-­con­tainer applic­ations

Docker Compose Template

Docker Compose Template

Miscel­laneous

docker pull <im­age­_na­me>­:<t­ag>
Pull an image or a repository from a registry
docker tag <so­urc­e_i­mag­e> <ta­rge­t_i­mag­e>
Create a tag TARGET­_IMAGE that refers to SOURCE­_IMAGE
docker push <im­age­_na­me>­:<t­ag>
Push an image or a repository to a registry
docker inspect <co­nta­ine­r_o­r_i­mag­e_n­ame­_or­_ID>
Display detailed inform­ation on one or more containers or images
docker system prune -a
Delete everything

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.