Skip to content

Instantly share code, notes, and snippets.

@alifahrri
Last active September 28, 2019 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alifahrri/baaa79490e8dec44e4da684be335af48 to your computer and use it in GitHub Desktop.
Save alifahrri/baaa79490e8dec44e4da684be335af48 to your computer and use it in GitHub Desktop.
docker cheatsheet

Docker Cheat Sheet

## List Docker CLI commands
docker
docker container --help

## Display Docker version and info
docker --version
docker version
docker info

## Execute Docker image
docker run hello-world

## List Docker images
docker image ls

## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

run in detached background

docker run -d -p 4000:80 friendlyhello

list container

docker container ls

stop container

docker container stop 1fa4ab2cf395

container-related commands :

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

service-related commands:

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager

stahp and remove all docker containers and images

List all containers (only IDs) .

docker ps -aq

Stop all running containers.

docker stop $(docker ps -aq)

Remove all containers.

docker rm $(docker ps -aq)

Remove all images.

docker rmi $(docker images -q)

free-up space

docker container prune
docker image prune
docker network prune
docker volume prune
# ultimate :
docker system prune

see docker image

docker run -it image_name sh
docker run -it --entrypoint sh image_name 

gdb stuff

docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined

backup

docker save myusername/myproject:latest | gzip -c > myproject_img_bak20141103.tgz
gunzip -c myproject_img_bak20141103.tgz | docker load
docker run --name=foo-build -v ~/.ssh/id_rsa:/root/.ssh/id_rsa foo-build
docker cp <containerId>:/file/path/within/container /host/path/target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment