Skip to content

Instantly share code, notes, and snippets.

@frankchang0125
Last active September 11, 2018 08:43
Show Gist options
  • Save frankchang0125/5d0f44501c6b49183adb3fc5297f5a72 to your computer and use it in GitHub Desktop.
Save frankchang0125/5d0f44501c6b49183adb3fc5297f5a72 to your computer and use it in GitHub Desktop.
Docker useful commands
# Docker connect to host machine:
1. Use `--net="host"` in docker `run` command, then connet with `127.0.0.1`
2. Use: `host.docker.internal`
# Docker's localhost IP:
172.17.0.1
# Run docker in interactive Bash session
$ docker run -it <image> /bin/bash
# With ENTRYPOINT defined
$ docker run -it --entrypoint /bin/bash <image>
# Attach to the running container with Bash session
$ docker exec -it <container> /bin/bash
# List all exited containers
$ docker ps -aq -f status=exited
# Remove stopped containers
$ docker container prune
$ docker ps -aq --no-trunc -f status=exited | xargs docker rm
# Remove dangling images
$ docker image prune
# Remove all images which are not used by existing containers
$ docker image prune -a
# Remove dangling/untagged images
$ docker images -q --filter dangling=true | xargs docker rmi
# Remove unused volumes
$ docker volume prune
$ docker volume rm $(docker volume ls -qf dangling=true)
# Stop all containers
$ docker stop $(docker ps -a -q)
# Remove all containers
$ docker rm $(docker ps -a -q)
# Remove all images
$ docker rmi $(docker images -a -q)
# Save images to a tar archive base on docker-compose.yml
$ docker save -o <tar archive filename> $(docker-compose config | awk '{if ($1 == "image:") print $2;}')
# Load images from tar archive
$ docker load -i <tar archive filename>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment