Skip to content

Instantly share code, notes, and snippets.

@irsyadulibad
Last active February 16, 2022 05:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irsyadulibad/231678a42a47af0022fff599f3a9ed1c to your computer and use it in GitHub Desktop.
Save irsyadulibad/231678a42a47af0022fff599f3a9ed1c to your computer and use it in GitHub Desktop.
Basic Docker Cheat Sheet
# Docker Image
## List all images
docker image ls
## Pull docker images
docker image pull image:tag
## Remove docker images
docker image rm image:tag
# Docker container
## Show all list of container
docker container ls -a
## Show only running container
docker container ls
# Create container
docker container create --name name image:tag
# Run container
docker container start containerID/containerName
# Stop container
docker container stop containerID/containerName
# Delete container
docker container rm containerID/containerName
# Container Log
## View container log
docker container logs containerID/containerName
## View container log with blocking
docker container logs containerID/containerName -f
# Exec container
docker container exec -i -t containerID/containerName /bin/bash
# Port forwarding
docker container create --name name -p phost:pcontainer -p phost:pcontainer image:tag
# Passing env variable
docker container create --name name -e KEY="value" -e KEY="value" image:tag
# See container stats
docker container stats
# Limit Resources
## Memory Allocation to container
docker container create --name name -m 180m image:latest
## CPUs allocation to container
docker container create --name name --cpus 0.5 image:latest
# Mounting folder
## type=bind,volume
docker container create --name name --mount "type=bind,source=folder,destination=folder,readonly" image:tag
# Docker Volume
## Show all volume
docker volume ls
## Create Volume
docker volume create name
## Delete volume
docker volume rm name
## Mounting volume
docker container create --name name --mount "type=volume,source=volname,destination=dest" image:tag
# Backup the volume
## Stopping target container
docker container stop targetname
## Create backup container
docker container create --name backupname --mount "type=bind,source=backupsource,destination=backupdest" --mount "type=volume,source=volname,destination=backupdest" image:tag
## Start and execute the backup container
docker container start backupname
docker container exec -i -t backupname /bin/bash
## Do backup inside backup container
tar cvf /backup/volname.tar.gz /backupdest
exit
## Stop and remove backup container
docker container stop backupname
docker container rm backupname
## Start again the target container
docker container start targetname
# Backup with shorthand
docker container run --rm --name ubuntu --mount "type=bind,source=srcHostDir,destination=destDir" --mount "type=volume,source=volname,destination=destDir" ubuntu:latest tar cvf /dstdir/backup.tar.gz /data
# Restore backup
docker container run --rm --name ubuntu --mount "type=bind,source=srcHostDir,destination=destDir" --mount "type=volume,source=volname,destination=destDir" ubuntu:latest bash -c "cd /data && tar xvf /destDir/backupFilename.tar.gz --strip 1"
# Network
## Show all network
docker network ls
## Create network
docker network create --driver driverName networkName
## Delete network
docker network rm networkName
# Container Network
## Create container with network
docker container create --name containerName --network networkName image:tag
# Docker Inspect
## Inspect the image
docker image inspect imageName
## Inspect the container
docker container inspect containerName
## Inspect the volume
docker volume inspect volName
## Insepct the network
docker network inspect networkName
# Docker Prune
## Prune the container
docker container prune
## Prune the image
docker image prune
## Prune the network
docker network prune
## Prune the volume
docker volume prune
## Prune all above
docker system prune
@destroylord
Copy link

Mantap ini bang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment