See also: Docker Documentation.
Setup
Start the daemon (on Fedora): sudo systemctl start docker
Start the daemon on boot (on Fedora): sudo systemctl enable docker
Run as user:
First create a docker group:
sudo groupadd docker
Then add the actual user to that group:
sudo usermod -aG docker $USER
After that log out and log in again.
Images and Containers
List Images
Command: docker images
Columns:
REPOSITORY: name of the repository on the Docker HubTAG: the tag (version) of the imageIMAGE ID: first 12 digits of the unique (64 bit) image IDCREATED: the creation dateSIZE: size of the image
Search Images
Command: docker search <image-name>
Columns:
NAME: name of the repositoryDESCRIPTION: Small descriptionSTARS: Showing how many people like the repositoryOFFICIAL: If it has been approved by the Docker teamAUTOMATED: If the build is automanted (on Docker Hub)
Pull Images
Command: docker pull <image-name>
Remove Images
Command: docker rmi <image-id>
or: docker rmi <image-name>:<tag>
Run Image
Command: docker run [flags] <image-name>:<tag> [command]
Flags:
-i: interactive shell-t: pseudo tty-d: run as daemon-p <host_port>:<container_port>: map ports
If the interactive (-i) flag is provided a command sould be executed.
This will override the default command that is run when the container is started.
A random name is assigned to the image.
Example: docker run -i -t fedora:latest /bin/bash
Execute Command in running Container
Command docker exec [flags] <image-name>:tag [command]
-i: interactive shell-t: pseudo tty-d: run as daemon-w: use working directory
Detach from running Container
CTRL+p CTRL+q
Show Running Images
Command: docker ps
To show also containers that are not running: docker ps -a
Logs
Command: docker logs <container-id-or-name>
Stopping Containers
Shutdown the container:
Command: docker stop <container-id-or-name>
Force quit the container:
Command: docker kill <container-id-or-name>
Renaming Containers
Command: docker rename <old-name> <new-name>
Getting Information from Containers
Command: docker stats <container-name>
Command: docker top <container-name>
Removing Containers
Command: docker rm <container-name>
Registries
There are three kinds of registries:
- Docker Hub: Hosted registry service by Docker
- Docker Trusted Registry: Hosted or on premise (backend maintained by Docker)
- Docker Registry: For running own Docker registry
Docker Machine
Create Machines
Create and manage machines running Docker.
Example (creating a VirtualBox machine running Docker): docker-machine create -d virtualbox node1
List Machines
Command: docker-machine ls
Columns:
NAME: node nameACTIVE: docker commands are run on the active nodeDRIVER: which driver is used (e.g. virtualbox)STATE: if it’s runningURL: its URLSWARM: if it’s part of a Docker Swarm clusterDOCKER: version of DockerERRORS: any errors that occurred
Restart a Node
Commmand: docker-machine restart <node-name>
Dockerfile
Instructions
FROM: The base image for this Docker image (scratchfor empty base image).MAINTAINER: Responsible maintainer (name and e-mail).RUN: Install packages and run other commands.ADD: Add files or folders to the Docker image. URLs can be provided. Automatically unpack or untar a compressed files.COPY: Same asADDbut without URL handling or unpacking/untarring.EXPOSE: Expose ports from the image to the outside world.LABEL: Additional information (version number, text …). Each label add a layer to the image.CMD: Execute commands and keep the container alive.ENTRYPOINT: Similar toCMD. Can be used in conjunction withCMD.USER: Specify which username to use. InfluencesRUN,CMDandENTRYPOINTinstructions that follow in the Dockerfile.WORKDIR: Specify the directory where to execute instructions. InfluencesRUN,CMDandENTRYPOINTinstructions that follow in the DockerfileONBUILD: Used if image is used as base in another Dockerfile. Executed as first statement afterFROM. Use in conjunction withADDorRUN.
Label Informations
Command: docker inspect <image-id>
Best Practices
- Use a
.dockerignorefile. - Install only necessary packages.
- Limit the number of layers. Every
RUNcommand adds a layer. - Execute only one process per container.
Docker build
docker build -t <docker-hub-username>/<repository-name> <directory>
or
docker build -f <path-do-dockerfile> -t <repository>:<tag>
<repository>: repository name prefixed by Docker Hub username
The .dockerignore file
This file can be used to exclude files and folders from being sent to the Docker daemon for the build. It should be placed in the same
folder as the Dockerfile.