docker
abstract
Docker is a software platform which builds and runs containers. Containers can simulate other computers with different software installed. Development containers can help increase reproducibility and decrease coupling.
basics
- A host machine is a computer which can run containers.
- A container acts like a simulated computer with its own OS and filesystem.
- An image uses the host's filesystem to store files needed to run a container.
- A Dockerfile is a text file which tells Docker how to build a custom image.
- A volume stores files which can be accessed by one or more containers.
- Host files must be copied or mounted before a container can access them.
- Containers on the same host use networks to communicate with each other.
- The Docker daemon manages containers, images, volumes, and networks.
commands
docker build myimage . |
build myimage from a Dockerfile in the current folder |
docker image ls |
show all images |
docker image rm myimage |
delete myimage |
docker network ls |
show all networks |
docker ps --all |
show all containers |
docker run myimage |
run a container from myimage |
docker run myimage ls -la |
run ls -la in a container from myimage |
docker system prune |
delete unused docker objects |
docker volume ls |
show all volumes |
dependencies
examples
Build an image named myimage:v2
from a Dockerfile
in some/folder
:
docker build --tag myimage:v2 some/folder
Run bash
interactively in a self-destructing container from the myimage:v2
image:
docker run --rm --interactive --tty myimage:v2 bash
Run the default myimage:v2
command with the current folder mounted read-only as /context
:
docker run --volume "$(pwd):/context:ro" myimage:v2
Delete the myimage:v2
image and any containers using it:
docker ps --quiet --filter ancestor="myimage:v2" | xargs docker rm --force docker image rm myimage:v2 --force
Stop all containers, then delete stopped containers and unused objects:
docker ps --quiet | xargs docker kill docker system prune --force
faq
- Where is the official documentation?
- Docker docs
-
Dockerfile
reference -
docker run
reference - Do containers always run as root?
-
No. Add a USER in a
Dockerfile
or calldocker run
with the --user option. - Can I run a container in a container?