Overview#
This page is an overview of Docker’s main features and entities. So you can easily get familiar with what Docker is in general.
Instalation and configuration#
Docker instalation USE ONLY COMMANDS PROVIDED BY DOCKER DOCUMENTATION.
May be useful after installing.
How not to always put
sudobefore thedockercommand.Something still incomprehensible.
Start daemon
sudo systemctl start docker.
Images#
Image is a template that is used for creating docker containers. Checkout more on specific section.
List available images.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fastapi_experiment latest 319ed841174c 7 days ago 1.04GB
httpd latest c0c20df5e7be 10 days ago 148MB
python 3.11 e9a675734068 2 weeks ago 1.01GB
alpine latest a606584aa9aa 3 weeks ago 7.8MB
wine_image latest 70b93dd1b320 5 weeks ago 3.72GB
localhost:5001/wine_image latest 70b93dd1b320 5 weeks ago 3.72GB
ghcr.io/mlflow/mlflow latest ed826ca3d0c7 5 weeks ago 793MB
ubuntu 20.04 5f5250218d28 5 weeks ago 72.8MB
gcr.io/k8s-minikube/kicbase v0.0.44 5a6e59a9bdc0 2 months ago 1.26GB
registry latest d6b2c32a0f14 9 months ago 25.4MB
Pull image from dockerhub
There is a special resource where really free to use docker images are stored - dockerhub. Using the docker pull command, you can load any image you like.
The following cell shows process of pulling hello-world docker image and then show that it was added to the list of available docker images.
echo "=====image pulling====="
docker pull hello-world
echo "=====image displaying====="
docker images | grep hello
=====image pulling=====
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
=====image displaying=====
hello-world latest d2c94e258dcb 14 months ago 13.3kB
Remove image
You can use the docker rmi command to remove any image you don’t want in your image store. The following cell shows the process of removing the docker image we created in the previous example.
docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
Deleted: sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e
Containers#
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
Run container
You must use the docker run command.
In the following example we have created a Ubuntu container. Later, we’ll show you that it’s a fully functional Ubuntu instance that just runs on your computer - later we’ll show it.
Some additional options are used here:
itdallows the container not to stop immediately after starting.nameallows you to specify the name of the container.
docker run -itd --name show_ubuntu ubuntu:20.04
f693d0227db3a19ee63a14e0cda7a5970c97c8a80d90a14e5d50eb63046235de
List containers
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f693d0227db3 ubuntu:20.04 "/bin/bash" 4 seconds ago Up 3 seconds show_ubuntu
The result is the docker container we created in the previous cell.
Stop container
You can stop a docker container with the docker stop command. This will stop execution of the container, but not delete it, so you can reload it later.
The following cell applies a docekr stop to the container we have just created.
docker stop show_ubuntu
echo "=====docker ps====="
docker ps
echo "=====docker ps -a====="
docker ps -a
show_ubuntu
=====docker ps=====
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
=====docker ps -a=====
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f693d0227db3 ubuntu:20.04 "/bin/bash" 21 seconds ago Exited (137) Less than a second ago show_ubuntu
As you can see, it’s not listed in the docker ps output - by default it only prints active running containers. But you can still see containers in the docker ps -a command - which prints all available images.
Remove container
You can kill the container and everything associated with it using the docker rm command.
In the foolowing cell we applied docker rm to the container we used as the example for this section, and then printed docker ps -a.
docker rm show_ubuntu
docker ps -a
Error response from daemon: No such container: show_ubuntu
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
As a result, we can’t see the sample container - it has been completely deleted from the system.
Volumes#
Volume is place at the computer storage that can be counted to the docker container - so they share that memory any change.
Find out more in the specific page.
Create volume To create new volueme you have to use command docker volume create.
docker volume create temp_volume
temp_volume
List volumes to list all volumes in system use command docker volume ls.
docker volume ls
DRIVER VOLUME NAME
local temp_volume
Remove volume to remove created volume use command docker volume rm <volume name>.
docker volume rm temp_volume
temp_volume
Networks#
Networks in Docker allow communication between containers.
List awailable networks
docker network ls
NETWORK ID NAME DRIVER SCOPE
e677bc8beaed ansible_example bridge local
5390a24fd218 bridge bridge local
37741ce59a7f host host local
1a4652d7c977 minikube bridge local
969663d04b2d none null local
Create network
To create a network, you can use the docker network create <container_name> command. The following example creates a network named temp_network and shows it in the docker network list.
docker network create temp_network
docker network ls
060e2d8778111dd202dfa13652b3d41dd7e012416b97c72ddcb92616caed9c76
NETWORK ID NAME DRIVER SCOPE
5390a24fd218 bridge bridge local
37741ce59a7f host host local
1a4652d7c977 minikube bridge local
969663d04b2d none null local
060e2d877811 temp_network bridge local
Remove network
The following cell deletes the network we’ve created before and shows the list of networks to make sure that the network temp_network has been deleted.
docker network rm temp_network
docker network ls
temp_network
NETWORK ID NAME DRIVER SCOPE
5390a24fd218 bridge bridge local
37741ce59a7f host host local
1a4652d7c977 minikube bridge local
969663d04b2d none null local
Authentification#
In order to pull images from the private registry, you will need to authenticate with that registry. You may also need to authentificate on DockerHub, as it has pulling limits for a sets of IP addresses. If someone in your network actively pulls images, you may be banned from pulling images without authentification.
Manipulate your authentification with:
The login information is stored in the ~/.docker/config.json.
Filter#
You can filter the output of the Docker commands using the -f (--filter) argument.
The input of the argument is a <key>=<value> pair. The <key> is filter sepcific for the command, and the <value> is a pattern to which the values of the <key> will correspond. To apply multiple filters, simply pass the -f argument multiple times.
This argument is ususally available for the commands that display resources or prunning commands. You can find a list of available filters in the documentation section corresponding to the -f description of the particular command. Some of the commands with reference to the documentation are:
docker ps(alias fordocker container ls).docker images/docker images ls.docker volume ls.docker network ls.
The following cell creates some containers from which we’ll make filtering.
docker run -d --name wow_python python:latest &> /dev/null
docker run -d --name wow_alpine alpine:latest &> /dev/null
docker run -d --name hey_nginx nginx:latest bash &> /dev/null
The following cell searches for containers based on the nginx:latest image.
docker ps -a -f "ancestor=nginx:latest"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5be9c3d9a5ba nginx:latest "/docker-entrypoint.…" About a minute ago Exited (0) About a minute ago hey_nginx
The following cell searches for the containers whose name starts with wow.
docker ps -a -f "name=^wow"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ff3e0557d44 alpine:latest "/bin/sh" 23 seconds ago Exited (0) 22 seconds ago wow_alpine
4e1fe78118e2 python:latest "python3" 23 seconds ago Exited (0) 23 seconds ago wow_python
And here’s an unusual way to release resources, just for extra example. For docker rm command passed outputs of the search based on the several filters.
Note: -f for the docker rm have different meaning: the force removal.
docker rm -f $(docker ps -q -a -f "name=wow_python" -f "name=wow_alpine" -f "name=hey_nginx")
5be9c3d9a5ba
6ff3e0557d44
4e1fe78118e2
Docker in docker#
It is typical for this resource to show some examples on clean docker. It’s convenient to use docker in docker to demonstrate such cases. The following cell shows docker in docker dockerfile, which is used for such cases. It installs tools that allow you to run bash code in Jupyter. You can connect to it (e.g. using vscode dev container extension) and use it as a regular docker.
cat overview_files/dockerfile
FROM docker:dind
RUN apk update && apk add bash python3-dev py-pip gcc musl-dev linux-headers
RUN pip3 install --break-system-packages bash_kernel && python -m bash_kernel.install
To build it use:
docker build -t knowledge_docker ./overview_files/ &> /dev/null
And from the root direcotry of the repo with:
docker run -itd --privileged --rm --name knowledge_docker -v ./:/knowledge knowledge_docker &> /dev/null