Docker Cheatsheet¶
Useful Links¶
Concepts¶
A Docker image is a read-only template. For example, an image could contain an Ubuntu operating system with Apache and your web application installed. Images are used to create Docker containers. Docker provides a simple way to build new images or update existing images, or you can download Docker images that other people have already created. Docker images are the buildcomponent of Docker.
Docker registries hold images.
Cheatsheet¶
To show only running containers use:
To show all containers use:
Show last started container:
Download an image:
Create then start a container: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
* Docker run reference
Run with interactive terminal (i = interactive t = terminal):
Start then detach the container (daemonize):
If you want a transient container, docker run --rm
will remove the container after it stops.
Looks inside the container (use -f
to act like tail -f
):
Stop container:
Delete container:
To check the environment:
Docker version / info:
Port Mapping¶
-p 80:5000
would map port 80 on our local host to port 5000 inside our container.
Full format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a range of ports. When specifying ranges for both, the number of container ports in the range must match the number of host ports in the range, for example: -p1234-1236:1234-1236/tcp
The -P
flag tells Docker to map any required network ports inside our container to our host (using random ports).
Linking¶
--link <name or id>:alias
where name is the name of the container we’re linking to and alias is an alias for the link name.
The --link
flag also takes the form: --link <name or id>
docker run -d --name myES -p 9200:9200 -p 9300:9300 elasticsearch
docker run --name myK --link myES:elasticsearch -p 5601:5601 -d docker-kibana-sense
Networks¶
Find out the container’s IP address:
Data Volumes¶
Create a new volume inside a container at /webapp:
You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image.
Mount the host directory, /src/webapp
, into the container at /opt/webapp
.
On Windows, use: docker run -v /c/Users/<path>:/<container path> ...