Docker useful commands

Build a container from Dockerfile

docker build -t container_name /path/to/dockerfile

Run docker container

docker run --rm -v "`pwd`/project:/project" -w /project -it container_name /bin/bash
  • --rm will automatically clean up the container and remove the file system when the container exits
  • -v mount volume to the container
  • -i interactive mode

List Docker local images

docker images

Start a container

# using image name
docker run -it python:3.6 /bin/bash

# using image id
docker run -it 8dbd9e392a96 /bin/bash

List docker processes

docker ps

# list included stopped processes
docker ps -a

Delete docker container

docker rm <container_id>

Delete docker image

docker rmi <image_id>

Stop all containers

docker stop $(docker ps -a -q)

Remove all containers

docker rm $(docker ps -a -q)

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see:

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

delete networks

$ docker network ls  
$ docker network ls | grep "bridge"   
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

// see:

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

// see:

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

Resize disk space for docker vm

$ docker-machine create --driver virtualbox --virtualbox-disk-size "40000" default