Skip to content

Latest commit

 

History

History
209 lines (141 loc) · 5.14 KB

Docker-CheatSheet.md

File metadata and controls

209 lines (141 loc) · 5.14 KB

Docker Cheat Sheet

Handy Aliases

alias dc='docker-compose'
alias de='docker-machine env'
alias di='docker images'
alias dl='docker-machine ls'
alias dm='docker-machine'
alias dps='docker ps'
alias dpsa='docker ps -a'

Docker boilerplates

See Docker boilerplates a.k.a templates for most programming language.

Run an image

Run a Docker container in detached mode and expose port 8080.

$ docker run -d --name my_docker -p 8080:8080 image_name

Get the IP Address

$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" my_docker

Attach to a container

Attach to a running Docker container by ID.

$ docker exec -it 32adfbf6eb62 /bin/bash

Attach to a running Docker container by name and enter a Bash shell.

$ docker exec -it my_docker /bin/bash

To detach use the following escape sequence: CTRL + p CTRL + q.

Mount current directory

docker run --rm -v ${PWD}:/home/jovyan/work jupyter/scipy-notebook

On Windows. Note the above works with PowerShell.

docker run --rm %cd%:/home/jovyan/work jupyter/scipy-notebook

See https://stackoverflow.com/a/41489151

Stop a container

$ docker kill <container_id>

Remove stopped container

$ docker rm <container_id>

Remove multiple containers

docker rm $(docker ps --filter status=exited -q)

Or, using xargs:

docker ps --filter status=exited -q | xargs docker rm

docker rm

Start container with remove switch

$ docker run -d --name my_docker image_name --rm

See single command to stop and remove docker container

See all containers

$ docker ps -a

Clean up everything

$ docker system prune

Remove all exited containers

$ docker rm $(docker ps -a -f status=exited -q)

Remove all stopped containers

$ docker rm $(docker ps -a -q)

See all images

$ docker images -a

Format image list

$ docker images --format "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}"

Store the format in ~/.docker/config.json. E.g.

{
    "psFormat": "table {{.Names}}\\t{{.Image}}\\t{{.RunningFor}} ago\\t{{.Status}}\\t{{.Command}}",
    "imagesFormat": "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.Size}}"
}

See Docker Quicktip #7: docker ps --format.

Remove all untagged images

$ docker rmi $(docker images -q -f dangling=true)
$ docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
$ docker images -q -a | xargs --no-run-if-empty docker rmi

Remove images by tag

docker rmi $(docker images --filter reference=*/*/my-image*:* -q)

How can I delete Docker images by tag, preferably with wildcarding?
Docker | Using filters

List volumes

$docker volume ls
$docker volume ls -f dangling=true

Remove all unused volumes

$ docker volume prune
$ docker volume prune -f # Bypasses prompt

Create a named volume

$ docker volume create db_data

Create a minimal container to view a volume's files

docker run --rm -i -v=postgres-data:/tmp/myvolume busybox find /tmp/myvolume

Rebuild when composing

$ docker-compose up -d --build

Tag image after building it

$ docker build -t dev:latest .

Other Cheat Sheets

Docker Cheat Sheet by wsargent

Resources