The one-page guide to Docker CLI: usage, examples, links, snippets, and more. Devhints.io Edit; Docker CLI cheatsheet. Manage images docker build docker build options. Docker Compose Cheatsheet. GitHub Gist: instantly share code, notes, and snippets. 1 Docker CheatSheet 1.1 Docker Trouble Shooting 1.2 Docker Basic 1.3 Docker start service 1.4 Container Runtime 1.5 Container Basic 1.6 Docker Cleanup 1.7 Dockerfile 1.8 Docker Compose 1.9 Docker Containers 1.10 Docker Images 1.11 Docker Socket file 1.12 Docker Conf 1.13 Ubuntu docker: Install missing packages 1.14 Check Status 1.15 Resource.
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>
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> ...
Example Dockerfile¶
Docker is an increasingly popular tool designed to make it easier to create, deploy and run applications within a container. We recently published an article – Data Scientist guide for getting started with Docker – which hopefully laid out some of the basics. As we have done in the past with SQL, Python Regular Expressions and many others, we thought it would be useful to have a centralized cheat sheet for Docker commands, which we’ve based on Docker’s official cheat sheet:
Install
First off, you’ll need to head over to the Docker site to install a version of the software.
To ensure it’s been installed correctly, open the command line and type docker version. This should display something like the below:
Build
docker build [OPTIONS] PATH | URL | -
Common Options | Explanation |
--add-host | Add custom host-to-IP mapping (host:IP) |
--cache-from | Images to consider as cache sources |
--compress | Compress the build using gzip |
--file, -f | Name and route of Docker file |
--force-rm | Always remove intermediate containers |
--label | Set the metadata for the image |
--memory, -m | Set a memory limit |
--no-cahe | Do not use cache |
--rm | Remove intermediate containers after a successful build |
--tag, -t | Name and optionally tag an image in the ‘name:tag’ format |
--target | Set the target build stage |
Builds an image from a Dockerfile
docker images
Lists all of the images that are locally stored in the docker engine
docker rmi IMAGE_NAME
Removes one or more images
Ship
docker pull IMAGE_NAME[:TAG]
Pulls an image or a repository from a registry
docker push IMAGE_NAME[:TAG]
Pushes an image or a repository to a registry
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Creates a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker login [OPTIONS] [SERVER]
Common Options | Explanation |
--password, -p | Password |
--username, -u | Username |
Logs into a docker registry
docker logout [SERVER]
Logs out of a docker registry
Run
docker create [OPTIONS] IMAGE_NAME
Creates a new container. This is the same as docker run, but the container is never started. See docker run for options
docker run [OPTIONS] IMAGE_NAME
Common Options | Explanation |
--add-host | Add custom host-to-IP mapping (host:IP) |
--attach, -a | Attach to STDIN, STDOUT, STDERR |
--hostname, -h | Container host name |
--interactive, -i | Keep STDIN open even if not attached |
-it | Connect the container to the terminal |
--label, -l | Set metadata on the container |
--memory, -m | Set a memory limit |
--mount | Attach a filesystem mount to the container |
--name | Assign a name to the container |
--network | Connect a container to a certain network |
--publish, -p | Expose certain ports to the container |
--rm | Automatically remove the container when it exits |
--volume, -v | Bind mount a volume |
--workdir, -w | Set the working directory inside the container |
Run a command in a new container
docker start CONTAINER_NAME
Start one or more containers
docker stop CONTAINER_NAME
Stop one or more running containers
docker kill CONTAINER_NAME
Kill one or more running containers
docker ps
List all containers
docker rm CONTAINER_NAME
Remove one or more containers
Data Science Examples
FROM ubuntu
RUN apt-get install python3
This Dockerfile would install python3 on top of Ubuntu layer. Dockerfiles are text files that define the environment inside the container
This Dockerfile would:
- install python3 on top of Ubuntu layer
- create a /home/jupyter directory on the container
- copy in contents from the /src/jupyter folder on the user's machine
- Expose port 8000
- Run Jupyter notebook
Docker Cheat Sheet Pdf Printable
docker pull tensorflow/tensorflow
Pull a latest TensorFlow image down
docker run -it -p 8000:8000 tensorflow/tensorflow
Docker Cheat Sheet
Run TensorFlow image in a container on port 8000
Related: