Skip to content

Instantly share code, notes, and snippets.

@anhphamt
Forked from ediblecode/common-docker-commands.sh
Created October 4, 2023 01:52
Show Gist options
  • Save anhphamt/002f8ebdc62afd4ac65aa498f4262c90 to your computer and use it in GitHub Desktop.
Save anhphamt/002f8ebdc62afd4ac65aa498f4262c90 to your computer and use it in GitHub Desktop.
Common docker commands
# If the commands fail on Windows, then make sure you're running in GitBash/Cygwin and NOT cmd
# HELP!
docker --help
# List docker images
docker images
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Build an image from a Dockerfile. Don't forget the dot!
docker build -t <name> .
# Create and run a container from an image
docker run <image_name>
# Check if container is running
# http://stackoverflow.com/a/24560463/486434
docker inspect -f "{{.State.Running}}" <container_name> 2> /dev/null
# Check if an image exists
# http://stackoverflow.com/a/30543453/486434
if [[ "$(docker images -q <image_name> 2> /dev/null)" == "" ]]; then
# Image doesn't exist, create it?
fi
# Stop ALL docker containers
docker stop $(docker ps -a -q)
# Kill ALL docker containers
# Kill vs Stop? http://superuser.com/questions/756999/whats-the-difference-between-docker-stop-and-docker-kill
docker kill $(docker ps -q)
# Remove all docker containers
docker rm $(docker ps -a -q)
# Remove all docker images
docker rmi $(docker images -q)
# Remove dangling images
docker rmi -f $(docker images -q -a -f dangling=true)
# Remove stopped containers, remove volumes not being used by any containers, all networks not being used by any containers, all dangling images
docker system prune
# Attach command prompt to running container
winpty docker exec -it <container_name> bash
# Or on Alpine
winpty docker exec -it <container_name> ash
# Less common but useful if you get the error "Unable to query docker version"
docker-machine regenerate-certs default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment