{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Docker Cheatsheet\n", "\n", "**Eric Kerfoot\\\n", "School of Biomedical Engineering & Imaging Sciences\\\n", "King's College London**\n", "\n", "\n", "This is a short rundown of the most used Docker commands and common arguments.\n", "Use the `--help` flag with the docker command for more info on the other commands.\n", "\n", "Examples: `docker --help`, `docker build --help`, `docker container ls --help`\n", "\n", "Useful links:\n", "* Docker docs: https://docs.docker.com/\n", "* Docker command reference: https://docs.docker.com/engine/reference/commandline/cli/\n", "* Dockerfile reference: https://docs.docker.com/engine/reference/builder/ \n", "* Docker samples: https://docs.docker.com/samples/ https://github.com/docker/labs\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker run`\n", "\n", "Create and run a container from a named image available locally or from the connected registry. \n", "\n", "Usage: `docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]`\n", "\n", "* `IMAGE` has form `[HOSTNAME/][NAMESPACE/]REPOSITORY` where `HOSTNAME` is the address/port of the user registry if used, `NAMESPACE` is the namespace or user under which the repository is found, and `REPOSITORY` is the name under which the image is stored. \n", "\n", "* `TAG` names different versions of images in a repository, default is `latest`.\n", "\n", "* `COMMAND` is command to run in container, default is to run command given by `ENTRYPOINT/CMD`.\n", "\n", "* `ARG` are further arguments to pass to command.\n", "\n", "Common options:\n", "* `-t`: allocate a terminal\n", "* `-i`: interactive mode\n", "* `--rm`: remove container when completed\n", "* `-d`: run container detached, ie. in background\n", "* `--net=host`: connect container's network to host's network\n", "* `-e ENV=VAL`: specify an environment variable set inside the container\n", "* `-p [HOSTADDR:]HOSTPORT:CONTAINERPORT`: map a host port to a container port, eg. `-p 8080:80`\n", "* `-v HOSTDIR:CONTAINERDIR[:PERMS]`: mount host directory as a directory accessible in the container, eg. `-v $HOME/mydata:/data`\n", "* `--gpus all`: mount all host GPUs as devices in the container, can choose devices with \"device=\" specification, eg. `--gpus \\\"device=0,1\\\"`\n", "\n", "Example:\n", "```sh\n", "$ docker run erickerfoot/pytorch-test:testtag\n", "1.5.0+cu101 0\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker build`\n", "\n", "Builds an image from a Dockerfile and stores it locally. This will pull images from a connected registry if needed.\n", "\n", "Usage: `docker build [OPTIONS] PATH | URL | -`\n", "\n", "* `PATH`: directory containing Dockerfile and associated data files, this directory will be copied during the build\n", "* `URL`: location of git repository, targball, or text file to use as build directory\n", "\n", "Common options:\n", "* `-f`: specify a Dockerfile, default is `./Dockerfile` in the build directory, can use `-` to read from stdin\n", "* `-t REPONAME:TAG`: tag the built image with the given repository name (eg. `NAME` or `NAMESPACE/NAME`) and tag\n", "\n", "Example:\n", "\n", "```sh\n", "$ docker build .\n", "Sending build context to Docker daemon 3.072kB\n", "Step 1/3 : from python\n", " ---> 659f826fabf4\n", "Step 2/3 : COPY hello.py /\n", " ---> 35343acd247a\n", "Step 3/3 : CMD [\"python\",\"hello.py\"]\n", " ---> Running in 732d46910d66\n", "Removing intermediate container 732d46910d66\n", " ---> 27e884f60275\n", "Successfully built 27e884f60275\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker image`\n", "\n", "View and manipulate stored images.\n", "\n", "Usage: `docker image COMMAND`\n", "\n", "Common commands:\n", "* `ls`: view images, `-a` to see all \n", "* `prune`: remove unused images\n", "* `rm`: remove named images, by repository name or image ID\n", "* `pull`: download a named image from a registry\n", "* `push`: upload a named image to a registry\n", "* `save`: save a named image to a tar archive, `-o` to specify file otherwise sent to stdout\n", "\n", "Example:\n", "```sh\n", "$ docker image rm redis\n", "Untagged: redis:latest\n", "Untagged: redis@sha256:ec277acf143340fa338f0b1a9b2f23632335d2096940d8e754474e21476eae32\n", "Deleted: sha256:36304d3b4540c5143673b2cefaba583a0426b57c709b5a35363f96a3510058cd\n", "Deleted: sha256:0a0f29e43c3a5555d675d253ff51d73e4d238bd558f11ae9b63d2a2a14251b36\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker container`\n", "\n", "View and manipulate running containers\n", "\n", "Usage: `docker container COMMAND`\n", "\n", "Common commands:\n", "* `ls`: view containers, `-a` to see all including stopped containers\n", "* `prune`: remove stopped containers\n", "* `stop`: stop running containers\n", "* `rm`: more containers, `--force` to forcibly stop named running containers\n", "* `attach`: attach streams to a container for interaction\n", "* `exec`: execute a command in the named container\n", "\n", "Example:\n", "```sh\n", "$ docker container ls -a\n", "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\n", "03d7b9923d63 hello-flask \"flask run --host=0.…\" 13 seconds ago Up 12 seconds 5000/tcp practical_herschel\n", "d87e700df2fb hello-flask \"flask run --host=0.…\" 20 seconds ago Exited (0) 16 seconds ago youthful_moser\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker kill`\n", "Kill named running containers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker rm`\n", "\n", "Remove named containers, `-f` or `--force` to forcibly stop running containers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker rmi`\n", "Remove named images, `-f` or `--force` to forcible removal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `docker login/logout`\n", "Log into or out of a registry server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `docker version`\n", "Generate Docker version information for client and server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Dockerfile Reference\n", "\n", "Dockerfiles are scripts for setting up an image. From a base image a new one is created by installing packages, copying files, and configuring the image as if it were a full Linux environment. This script is composed of a sequence of commands which are run in order, with Docker caching the state of the image between each one, allowing the developer to make changes without having to repeat unchanged steps.\n", "\n", "The most-used commands are:\n", "\n", "* `FROM`: states the image to base the configuration on, `scratch` for a completely blank image.\n", "* `COPY`: copy file(s) into a location in the image.\n", "* `RUN`: run a command in the image, if given as a string command (eg. `RUN echo Hello run`) the command is run using `/bin/sh`, if in exec form (eg. `RUN [\"/bin/echo\",\"Hello run\"]`) this is run without shell invocation or interpretation. By default the command is run as root.\n", "* `ENV`: set an environment variable, either as `ENV key value...` or `ENV key=value, key=value, ...`.\n", "* `EXPOSE`: states that the nominated port number is meant to be exposed by the image when run but doesn't ensure this is the case.\n", "* `WORKDIR`: sets the working directory in the image of any subsequent commands.\n", "* `USER`: switch the user executing commands to the nominated username, which must already exist in the image\n", "* `ENTRYPOINT`: state the command to run when the container starts, this can be as a shell command (`ENTRYPOINT echo Hello run`) or exec command (`ENTRYPOINT [\"/bin/echo\",\"Hello run\"]`). Arguments passed to the container in `docker run`, or those in the `CMD` command if none provided, are appended to the arguments of the command to run.\n", "* `CMD`: like `ENTRYPOINT` however if arguments are passed to `docker run` these must state a new executable to run as the command followed by its arguments. \n", "\n", "Example Dockerfile from https://github.com/docker/labs/blob/master/beginner/flask-app/Dockerfile:\n", "\n", "```dockerfile\n", "# our base image\n", "FROM alpine:3.12\n", "\n", "# Install python and pip\n", "RUN apk add --update py2-pip\n", "\n", "# upgrade pip\n", "RUN pip install --upgrade pip\n", "\n", "# install Python modules needed by the Python app\n", "COPY requirements.txt /usr/src/app/\n", "RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt\n", "\n", "# copy files required for the app to run\n", "COPY app.py /usr/src/app/\n", "COPY templates/index.html /usr/src/app/templates/\n", "\n", "# tell the port number the container should expose\n", "EXPOSE 5000\n", "\n", "# run the application\n", "CMD [\"python\", \"/usr/src/app/app.py\"]\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }