View on GitHub

docker-cheat-sheet

An overview of Docker's most common commands, and related notes (such as deployment options)

< home < toolbox

Terminology

Term Description
Images The blueprints of the application. Very roughly approximates to a git registry.
Containers are created from images. Images can be base images (i.e. not based on another image; typically just an OS), or child images (i.e. based on a base image; typically adds functionality to base)
Containers A running instance of an application.
DockerFile A tool for simplifying the reployment of multi-container applications.
Docker Compose A text file containing a list of commands for creating an image.
Docker Daemon Local background service that mnages building, running and distributing of containers.
Docker Client The command line tool used to interact with the daemon (GUIs also exist).
Docker Hub A central registry of images maintained by Docker: https://hub.docker.com/
Repositories can also be hosted locally.
Flask A micro web framework, written in Python. Typically used for web applications.

CLI Commands

Management Commands (since v1.13+)

Command Action
Repository Commands
$ docker login [server name] Login to the Docker Hub
(or, optionally, another server)
$ docker search [search-term] Search the Docker Hub for images
Image Commands  
$ docker image ls List all local images
$ docker image pull [image name][:version] Pull an image from the Docker Hub
(either latest or specified ‘[:version]’.
To use a local registry, specify
the full server name and path:
myregistry.local:5000/testing/test-image
$ docker image build -t [username/appname] [directory containing docker file] Create a new image based on the DockerFile
$ docker image push [username/appname] Push an image to the repository
Container Commands
$ docker container run [-d] [-P\|-p [external port:internal port]] -e "[key=value]" --net [network-name] [--name container-name] [image-name] [command\|-it] [--rm] Launches a container from the specified image and
runs a command or opens an sh shell
-d run container in background
-P Map internal ports to random external ports
-p Specify custom internal/external port mapping
-e Set an environment variable
--net Specify the network to join
--rm Delete container on exit
$ docker container logs [container-name] Display logs for the specified container
$ docker container port [container-name] Display ports exposed by container
$ docker container stop [container-name] Stop a container
$ docker container ls [-a] List all running containers
-a = include recently stopped containers
$ docker container rm [container-name] Delete a container
$ docker container prune Delete all stopped containers
Network Commands  
$ docker network ls List all networks.
By default, Docker creates: “bridge”, “host” and “none”,
“bridge” is the default network for containers.
$ docker network inspect [network-name] Inspect the state of a network.
$ docker network create [network-name] Create a new network.
$ docker network rm [network-name] Remove a network.
 

Legacy Commands (may be deprecated in the future)

Command Action
Image Commands
$ docker images List all local images
$ docker pull [image name][:version] Pull an image from the Docker Hub
(either latest or specified ‘[:version]’.
To use a local registry, specify the full server name and
path: myregistry.local:5000/testing/test-image
$ docker build -t [username/appname] [directory containing docker file] Create an image based on the DockerFile
$ docker push [username/appname] Push the new image to the repository
Container Commands  
$ docker run [-d] [-P\|-p [external port:internal port]] -e "[key=value]" --net [network-name] [--name container-name] [image-name] [command\|-it] [--rm] Launches a container from the specified image and runs a command or opens an sh shell
-d run container in background
-P Map internal ports to random external ports
-p Specify custom internal/external port mapping
-e Set an environment variable
--net Specify the network to join
--rm Delete container on exit
$ docker logs [container-name] Display logs for the specified container
$ docker port [container-name] Display ports exposed by container
$ docker stop [container-name] Stop a container
$ docker ps [-a] List all running containers (-a = include recently stopped containers)
$ docker rm [container-name] Delete a container
$ docker rm $(docker ps -a -q -f status=exited) Delete all stopped containers
 

DockerFile

Command Action
FROM [base-name:version] The base image on which this image is based
e.g. FROM python:3
WORKDIR [path] The directory in which the app is based.
ADD file.xyz /file.xyz Copy all the files to the image
COPY . . Copy all the files to the image
COPY --chown=user:group host_file.xyz /path/container_file.xyz Copy a specific file to the image, with specific access permissions
VOLUME ["/data"] Specification for mount point
ENV APP_HOME /myapp or ARG APP_HOME="/myapp" Set environment variables
EXPOSE [port] The port that needs to be exposed.
RUN ["executable", "arg1", "arg2", "etc."] Mainly used to install a new package on top of the base image
e.g. RUN ["pip", "install", "--no-cache-dir", "-r", "requirements.txt"
CMD ["executable", "arg1", "arg2", "etc."] A default command to be run when docker run is called
e.g. CMD ["python", "./app.py"]. If the “executable” is left out, the arguments are appended to the ENTRYPOINT command. CMD is ignored if a command is specified on the command line.
ENTRYPOINT ["executable", "arg1", "arg2", "etc."] This should be used if the container is to be used as an executable. The command will always run, regardless of any command line arguments. Any command line arguments will be appended to the ENTRYPOINT arguments.
ONBUILD [command] To be run if another image is built on top of this image.
LABEL <key>=<value> <key>=<value> <key>=<value> ... Set metadata as key-value pairs.

For further discussion of the difference between RUN, CMD and `ENTRYPOINT, see

Docker-Compose

A tool for managing a collection of containers as if they were a single app.

It is configured using a YAML file: docker-compose.yml, of which the following is an example:

version: "3"
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    volumes:
      - esdata1:/usr/share/elasticsearch/data
  web:
    #build: . # builds a new image
    image: oclipa/foodtrucks-web # uses an existing image
    command: python app.py
    depends_on:
      - es
    #environment:
    #  - DEBUG=True # set an env var for flask
    ports:
      - 5000:5000
    volumes:
      - ./flask-app:/opt/flask-app
volumes:
  esdata1:
    driver: local

This file defines two services es (the elasticsearch service) and web (the web app). The details are broadly the same as those in the DockerFile, however note that depends-on property, which indicates that the es service must be started before the web service. The volumes properties are particularly useful for logging.

docker-compose commands must be run in the same directory as the docker-compose.yml file.

If problems are experienced, might need to run docker-compose up -d --build.

Command Action
Image Commands
$ docker-compose up [-d] [--build] Launch all of the services and connect them to the same network.
-d Detached mode.
--build Rebuild the local image.
$ docker-compose ps List all services.
$ docker-compose down [-v] Shutdown all of the services. -v Destroy all data volumes.
$ docker-compose run [service-id] [command] Run a command in the context of the specified service.
 

 

 

 


Move along; nothing to see here…