Skip to content

Instantly share code, notes, and snippets.

@codenickycode
Last active January 2, 2023 19:43
Show Gist options
  • Save codenickycode/c880e65babd467c2748cfba8a9f79ffa to your computer and use it in GitHub Desktop.
Save codenickycode/c880e65babd467c2748cfba8a9f79ffa to your computer and use it in GitHub Desktop.
[Docker: CLI] Common Docker CLI Commands #docker #cli
# List all running containers
docker ps
# List all running and stopped containers
docker ps -a
# List all local images
docker images
# List all volumes
docker volume ls
# Stop a container
# Changes will persist until the container is removed
docker stop <CONTAINER ID>
# Start a stopped container
docker start <CONTAINER ID>
# Create and start a container:
docker run \
# with an interactive terminal
-it \
# in the background (daemon)
-d \
# which will restart when we exit
--restart unless-stopped
# mapping our local port to the container's port
-p <LOCAL PORT>:<CONTAINER PORT> \
<IMAGE ID>
# Attach to a running container
docker attach <CONTAINER ID>
# Detach from container, but leave it running
# Hold CTRL and press P, then Q
# Create a new image from a container
docker commit <FROM CONTAINER ID> <IMAGE TAG TO CREATE>:<VERSION>
### Clean-up!
# Remove a stopped container
docker rm <CONTAINER ID>
# Remove a running container
docker rm -f <CONTAINER ID>
# Remove all running and stopped containers, quietly
docker rm -f $(docker ps -aq)
# Remove image
docker rmi <IMAGE ID>
# Remove all images, quietly
docker rmi -f $(docker images -aq)
# Remove all volumes
docker volume prune
# Remove everything except running
docker system prune -af --volumes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment