Docker Command Cheat Sheet
Containers
Lifecycle
docker renameallows the container to be renameddocker runcreates and starts a container in one operationdocker rmdeletes a container
Starting and Stopping
docker startstarts a container so it is running.docker stopstops a running container.docker restartstops and starts a container.docker killsends a SIGKILL to a running container.docker attachwill connect to a running container.
Info
docker psshows running containers.docker portshows public facing port of container.docker topshows running processes in container.docker statsshows containers’ resource usage statistics.docker ps -ashows running and stopped containers.docker stats --allshows a running list of containers.
Import / Export
docker cpcopies files or folders between a container and the local filesystem.
Executing Commands
docker execto execute a command in container.
Images
Lifecycle
docker imagesshows all images.docker buildcreates image from Dockerfile.docker commitcreates image from a container, pausing it temporarily if it is running.docker rmiremoves an image.
Info
docker historyshows history of image.docker tagtags an image to a name (local or registry).
Registry & Repository
A repository is a hosted collection of tagged images that together create the file system for a container.
A registry is a host – a server that stores repositories and provides an HTTP API for managing the uploading and downloading of repositories.
Docker.com hosts its own index to a central registry which contains a large number of repositories. Having said that, the central docker registry does not do a good job of verifying images and should be avoided if you’re worried about security.
docker loginto login to a registry.docker logoutto logout from a registry.docker pullpulls an image from registry to local machine.docker pushpushes an image to the registry from local machine.
Dockerfile
The configuration file. Sets up a Docker container when you run docker build on it. Vastly preferable to docker commit.
Here are some common text editors and their syntax highlighting modules you could use to create Dockerfiles:
- If you use jEdit, I’ve put up a syntax highlighting module for Dockerfile you can use.
- Sublime Text
- Atom
- Vim
- Emacs
- TextMate
- VS Code
- Also see Docker meets the IDE
Instructions
- .dockerignore
- FROM Sets the Base Image for subsequent instructions.
- MAINTAINER (deprecated - use LABEL instead) Set the Author field of the generated images.
- RUN execute any commands in a new layer on top of the current image and commit the results.
- CMD provide defaults for an executing container.
- EXPOSE informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
- ENV sets environment variable.
- ADD copies new files, directories or remote file to container. Invalidates caches. Avoid
ADDand useCOPYinstead. - COPY copies new files or directories to container. Note that this only copies as root, so you have to chown manually regardless of your USER / WORKDIR setting. See https://github.com/moby/moby/issues/30110
- ENTRYPOINT configures a container that will run as an executable.
- VOLUME creates a mount point for externally mounted volumes or other containers.
- USER sets the user name for following RUN / CMD / ENTRYPOINT commands.
- WORKDIR sets the working directory.
- ARG defines a build-time variable.
- ONBUILD adds a trigger instruction when the image is used as the base for another build.
- STOPSIGNAL sets the system call signal that will be sent to the container to exit.
- LABEL apply key/value metadata to your images, containers, or daemons.
Tutorial
Examples
- Examples
- Best practices for writing Dockerfiles
- Michael Crosby has some more Dockerfiles best practices / take 2.
- Building Good Docker Images / Building Better Docker Images
- Managing Container Configuration with Metadata