docker

Synopsis

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating-system kernel and are thus more lightweight than virtual machines. Containers are created from images that specify their precise contents. Images are often created by combining and modifying standard images downloaded from public repositories.

Run a new Container

Container is a running instance of an image. You can run a container from an image using the docker run command.

# Run a container from an image
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

# Run a container from an image and attach to it
docker run -it [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
CommandDescription
docker run IMAGEStart A New Container from an image
docker run --name CONTAINER IMAGEAssign a name
docker run -p HOSTPORT:CONTAINERPORT IMAGEMap a Port
docker run -P IMAGEMap All ports
docker run -d IMAGEStart Container in Background
docker run --hostname HOSTNAME IMAGEAssign a Hostname
docker run --add-host HOSTNAME:IP IMAGEAdd a dns entry
docker run -v HOSTDIR:TARGETDIR IMAGEMap a local Directory into the Container
docker run -it --entrypoint EXECUTABLE IMAGEChange the Entrypoint

Manage Containers

CommandDescription
docker psShow a list of running containers
docker ps -aShow a list of all containers
docker rm CONTAINERDelete a container
docker rm -f CONTAINERDelete a running container
docker CONTAINER pruneDelete stopped containers
docker stop CONTAINERStop a running container
docker start CONTAINERStart a stopped container
docker cp CONTAINER:SOURCE TARGETCopy a file from a container to the host
docker cp TARGET CONTAINER:SOURCECopy a file from the host to a container
docker exec -it CONTAINER EXECUTABLEStart a shell inside a running container
docker rename OLD_NAME NEW_NAMERename a container
docker commit CONTAINERCreate an image out of a container

Manage Images

Images are the building blocks of containers. You can manage images using the docker image command.

CommandDescription
docker pull IMAGE[:TAG]Download an image
docker push IMAGEUpload an image to a repository
docker rmi IMAGEDelete an image
docker imagesShow list of all images
docker image pruneDelete dangling images
docker image prune -aDelete all unused images
docker build DIRECTORYBuild an image from a Dockerfile
docker tag IMAGE NEWIMAGETag an image
docker build -t IMAGE DIRECTORYBuild and tag an image from a Dockerfile
docker save IMAGE > FILESave an image to a .tar file
docker load -i TARFILELoad an image from a .tar file

Info and Stats

CommandDescription
docker logs CONTAINERShow the logs of a container
docker statsShow stats of a running container
docker top CONTAINERShow processes of a container
docker versionShow installed docker version
docker inspect NAMEGet detailed info about an object
docker diff CONTAINERShow all modified files in a container
docker port CONTAINERShow mapped ports of a container

Docker compose

Docker compose is a tool for defining and running multi-container Docker applications. You can use it to run a single container or a complex application with multiple containers.

# Run a container from a docker-compose.yml file
docker-compose up

# Run a container in the background
docker-compose up -d

# Run a container with a custom docker-compose.yml file

docker-compose -f FILENAME.yml up

# Run a container with a custom docker-compose.yml file and a custom .env file
docker-compose -f FILENAME.yml --env-file .env up
CommandDescription
docker compose buildBuild all services from a docker-compose.yml file
docker compose upStart all services
docker compose up NAMEStart named service and all dependencies
docker compose run NAME COMMANDRun COMMAND in the service NAME
docker compose downStop all services
docker compose logs -fsee all logs for the services, following them

Docker Swarm

Docker Swarm is a native clustering tool for Docker. It turns a pool of Docker hosts into a single, virtual Docker host.

CommandDescription
docker swarm initInitialize a swarm
docker swarm joinJoin a swarm
docker swarm leaveLeave a swarm
docker swarm join-tokenManage join tokens
docker node lsList nodes
docker node inspectInspect a node
docker node promotePromote a node
docker node demoteDemote a node
docker node updateUpdate a node
docker node rmRemove a node
docker service lsList services
docker service createCreate a new service
docker service inspectInspect a service
docker service logsFetch the logs of a service or task
docker service psList the tasks of a service
docker service rmRemove a service
docker service scaleScale a service
docker service updateUpdate a service

Docker Machine

Docker Machine is a tool for provisioning Docker hosts on your computer, on cloud providers, and inside your own data center. It creates servers, installs Docker on them, then configures the Docker client to talk to them.

CommandDescription
docker-machine createCreate a machine
docker-machine lsList machines
docker-machine sshLog into or run a command on a machine with SSH
docker-machine startStart a machine
docker-machine stopStop a machine
docker-machine restartRestart a machine
docker-machine regenerate-certsRegenerate TLS Certificates for a machine
docker-machine envDisplay the commands to set up the environment for the Docker client
docker-machine inspectInspect information about a machine
docker-machine ipGet the IP address of a machine
docker-machine provisionRe-provision existing machines
docker-machine rmRemove a machine
docker-machine scpCopy files between machines
docker-machine sshLog into or run a command on a machine with SSH
docker-machine statusGet the status of a machine
docker-machine upgradeUpgrade a machine to the latest version of Docker
docker-machine urlGet the URL of a machine
docker-machine helpShows a list of commands or help for one command

Docker Registry

Dockers Registry is a tool to store and distribute Docker images. It is the default registry for Docker Hub.

CommandDescription
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker pullPull an image or a repository from a registry
docker pushPush an image or a repository to a registry
docker searchSearch the Docker Hub for images
docker tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

CommandDescription
FROMSet the base image for subsequent instructions
RUNExecute any commands in a new layer on top of the current image and commit the results
CMDProvide defaults for an executing container
LABELSet metadata for an image
EXPOSEInform Docker that the container listens on the specified network ports at runtime
ENVSet environment variables
ADDCopy new files, directories or remote file URLs from SOURCE to the filesystem of the container at the path DEST
COPYCopy new files or directories from SOURCE to the filesystem of the container at the path DEST
ENTRYPOINTConfigure a container that will run as an executable
VOLUMECreate a mount point with the specified name and mark it as holding externally mounted volumes from native host or other containers
USERSet the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile
WORKDIRSet the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile
ARGDefine a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag
ONBUILDAdd a trigger instruction to an image
STOPSIGNALSet the system call signal that will be sent to the container to exit
HEALTHCHECKConfigure a test to perform to check that the container is still working
SHELLSet the default shell for the shell-form of commands

Docker Hub

Docker Hub is a cloud-based registry service which allows you to link code repositories to Docker images. It offers automated builds, webhooks, and team collaboration features.

CommandDescription
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker pullPull an image or a repository from a registry
docker pushPush an image or a repository to a registry
docker searchSearch the Docker Hub for images
docker tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Docker Networking

Docker networking is a feature that allows you to connect containers to each other and to the outside world. It allows you to create a network of containers that can communicate with each other and the outside world.

CommandDescription
docker network createCreate a network
docker network connectConnect a container to a network
docker network disconnectDisconnect a container from a network
docker network inspectDisplay detailed information on one or more networks
docker network lsList networks
docker network pruneRemove all unused networks
docker network rmRemove one or more networks

Docker Volumes

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.

CommandDescription
docker volume createCreate a volume
docker volume inspectDisplay detailed information on one or more volumes
docker volume lsList volumes
docker volume pruneRemove all unused local volumes
docker volume rmRemove one or more volumes

Docker Secrets

Docker secrets are sensitive data which you don’t want to store in a Dockerfile or in your application’s source code unencrypted. Docker secrets are encrypted during transit and at rest in a Docker swarm. A given secret is only accessible to those services which have been granted explicit access to it, and only while those service tasks are running.

CommandDescription
docker secret createCreate a secret
docker secret inspectDisplay detailed information on one or more secrets
docker secret lsList secrets
docker secret rmRemove one or more secrets

Docker Stack

Docker stack is a set of interrelated services that share dependencies, and can be orchestrated and scaled together. A stack is defined in a docker-compose.yml file.

CommandDescription
docker stack deployDeploy a new stack or update an existing one
docker stack lsList stacks
docker stack psList the tasks in the stack
docker stack rmRemove one or more stacks
docker stack servicesList the services in the stack

Docker BuildKit

Docker BuildKit is a toolkit for converting source code to build artifacts in an efficient, expressive and repeatable manner. It provides a common set of operations for building container images and artifacts.

CommandDescription
docker buildBuild an image from a Dockerfile
docker buildxBuild with BuildKit

Docker Security

Docker security is a set of features that allow you to secure your Docker containers and images.

CommandDescription
docker scanScan the image for vulnerabilities