docker is a tool that lets you manage containers.

-- List running containers
docker ps


-- List all containers
docker ps -a


-- Stop a running container
docker stop 7a0340a51051


-- Remove container(s) by container id or name
docker rm 7a0340a51051
docker rm friendly_kapitsa
docker rm 16474400994b 6f57233df341 3490ef8a6dc7
-- Force remove container
docker rm -f 7a0340a51051


-- Remove all containers
docker rm $(docker ps -q)
docker rm $(docker ps -a -q)


-- List images
docker images


-- Remove image(s)
docker rmi 14dd2bb058f8
docker rmi 5bfac5601b51 19018a4211ce d17fa5049d44


-- Remove all images
docker rmi $(docker images -q)


-- Run a container temporarily for testing
-- localhost:8080 mapped to port 80 inside the container
-- will auto delete container when you quit
docker run --rm --name webserver -p 8080:80 nginx


-- Run an image detached
-- use localhost:8080 pointing to port 80 inside the container
docker container run --name webserver -d -p 8080:80 nginx


-- Get an interactive shell on an already running container
docker exec -it webserver bash


-- Run a container with an interactive shell
docker run -i -t --rm -v `pwd`:/usr/src/app -w /usr/src/app nimlang/nim:1.2.0 bash


-- Run mutliple commands in a container
docker run --rm -v `pwd`:/usr/src/app -w /usr/src/app nimlang/nim:1.2.0 bash -c "echo $HOME; printenv; ls -al /root"

Port mapping

Map the localhost port 8080 to the container port 80

-p <local port>:<container port>
-p 8080:80

Volume mapping

Map the current working directory inside the container as /var/run/local

-v <local dir>:<container dir>
-v `pwd`:/var/run/local

Troubleshooting

Can’t remove a running container

docker ps                                              
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
5370b4ea7c8c        nginx               "nginx -g 'daemon of…"   8 seconds ago       Up 7 seconds        0.0.0.0:8080->80/tcp   webserver

$ docker rm webserver
Error response from daemon: You cannot remove a running container 5370b4ea7c8c5f005507bd634f40ffede96f8d2b74dad4e679e68c12217126a1. Stop the container before attempting removal or force remove

Two ways to resolve:

  • Stop container, then remove
docker stop webserver
docker rm webserver
  • Force remove container
docker rm -f webserver

Can’t remove an image used in multiple repositories

$ docker images                                    
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nimlang/nim         1.0.2               8e87b95abe76        5 months ago        581MB
nimlang/nim         1.0.2-ubuntu        8e87b95abe76        5 months ago        581MB
nimlang/nim         latest              8e87b95abe76        5 months ago        581MB

$ docker rmi 8e87b95abe76
Error response from daemon: conflict: unable to delete 8e87b95abe76 (must be forced) - image is referenced in multiple repositories

$ docker rmi nimlang/nim
Untagged: nimlang/nim:latest

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nimlang/nim         1.0.2               8e87b95abe76        5 months ago        581MB
nimlang/nim         1.0.2-ubuntu        8e87b95abe76        5 months ago        581MB

-- or you can force delete
$ docker rmi -f 8e87b95abe76
Untagged: nimlang/nim:1.0.2
Untagged: nimlang/nim:1.0.2-ubuntu

Glossary

image
A collection of files grouped as a package. The package can be run as a container but otherwise only takes up disk space.
container
A running (or stopped) process. It’s an instance of an image.

Resources