Skip to content

Instantly share code, notes, and snippets.

@fr-xiaoli
Last active March 27, 2020 07:04
Show Gist options
  • Select an option

  • Save fr-xiaoli/e23accbe4219090874cb31d003174929 to your computer and use it in GitHub Desktop.

Select an option

Save fr-xiaoli/e23accbe4219090874cb31d003174929 to your computer and use it in GitHub Desktop.
Docker cheatsheet

Images

Build a Docker image and tag it using the Dockerfile in the current folder

$ docker build -t {my_tag_name} --build-arg SSH_KEY="`cat ~/.ssh/id_rsa`" --no-cache .

List images

$ docker image ls

or

$ docker images
  • List all images including intermediate ones
$ docker images -a

Prune dangling images

$ docker image prune
  • Options
    • -a prune not only dangling but also currently un-referenced images
    • --filter "until=12h" only remove images created before 12h
    • --filter "label=<key>" or --filter "label!=<key>"

Remove image(s)

$ docker image rm {image ID} {image ID2}

or

$ docker rmi {image ID} {image ID2}

Containers

List Docker containers

  • List running containers
$ docker ps
  • useful flages

    • -a List all containers both running and stopped
    • -q quiet (only shows the container IDs)
  • New syntax to list containers

$ docker container ls

Run: Build and start a container from an image

$ docker run {Image name}
  • Options (goes before image name)
    • --name name the container
    • --rm remove the container when it exits
    • -it sh interactive and use shell in the container
    • -d detached mode

Start Docker container(s)

$ docker start {container ID}

Stop Docker containers

  • Stop specific containers
$ docker stop {Container ID 1} {Container ID 2}

or

$ docker kill {Container ID}
  • Stop all running containers
$ docker stop $(docker ps -aq)

Remove Docker Containers

  • Remove specific containers
$ docker container rm {Container ID 1} {Container ID 2} ...
  • List all stopped containers
$ docker container ls -a --filter status=exited --filter status=created
  • Remove all containers (the running ones will not be stopped and give an error)
$ docker rm $(docker ps -aq)

Execute commands inside a container

  • Execute shell script in a Node container
$ docker exec -it {container ID} sh
  • Execute the psql shell inside a postgres docker container
$ docker exec -it {container ID} psql -U postgres

Show logs of a running container

$ docker logs {container ID} -f
  • -f follow the output so that the logs keep coming

Volumes

List Docker Volumes

$ docker volume ls

Remove a volume (the container using the volume has to be removed first)

$ docker volume rm {VOLUME NAME}

Prune unused volumes

$ docker volume prune

Networks

List Docker Networks

$ docker network ls

Remove networks

  • Remove a specific network
$ docker network rm {network id}
  • Remove unused network
$ docker network prune
  • Remove with filter
$ docker network prune -a --filter "until=12h"

Remove all unused objects

$ docker system prune -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment