1.1. Images
Docker images
You can search for images available on Docker Hub
by clicking the Explore link or by typing mariadb
into the search field: https://hub.docker.com/search/?q=mariadb&type=image
You will get a list of results and the first hit will probably be the official image: https://hub.docker.com/_/mariadb
This page contains instructions on how to pull the image. Let’s pull a certain version of mariadb:
docker pull mariadb:11.5
When using images from Docker Hub or other sources, always follow these practices:
Is it an official image ? Official Images are a good starting point, please read here why.
What is installed in the image?
- Read the Dockerfile that was used to build the image
- Check the base image
- Check the vulnerabilities of this image. Does it affect your application?
- Check the dependencies of the image.
- Compare your images Digest to the sha256 value shown on dockerhub.
After an image has been downloaded, you may then run a container using the downloaded image with the sub-command run
. If an image has not been downloaded when Docker is executed with the sub-command run
, the Docker client will first download the image, then run a container using it:
docker run hello-world:linux
Note
Here we use thelinux
tag of the hello-world image instead of using latest
again. Avoid using the latest
tag in production settings, as it can lead to unexpected behavior if the image updates. Always specify a version.To see the images that have been downloaded to your computer type:
docker images
The output should look similar to the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 11.5 58730544b81b 2 weeks ago 397MB
hello-world latest 1815c82652c0 2 months ago 1.84kB
The hello world container you ran in the previous lab is an example of a container that runs and exits, after emitting a test message. Containers, however, can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, but they are more resource-friendly.
For example, let’s run a container using the downloaded image of MariaDB. The combination of the -i and -t switches gives you interactive shell access to the container:
docker run -it mariadb:11.5
An error has popped up!
2022-08-09 08:19:21+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
2022-08-09 08:19:21+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-08-09 08:19:21+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
2022-08-09 08:19:21+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD
🤔 Why do I get an error? Is this a bug in the image?
Everything is fine, to run this image there is some configuration needed. Read the following excerpt carefully.
error: database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD
We will add the configuration later.
🤔 What's an image?
Think of an image as a blueprint of what will be in a container when it runs.
- An image is a collection of files + some metadata (or in technical terms: those files form the root filesystem of a container)
- Images are made of layers, conceptually stacked on top of each other
- Each layer can add, change or remove files
- Images can share layers to optimize disk usage, transfer times, and memory use
- You build these images using Dockerfiles (in later labs)
- Images are immutable, you cannot change them after the creation
🤔 What's the difference between a container and an image?
When you run an image, it becomes a container.
- An image is a read-only filesystem
- A container is an encapsulated set of processes running in a read-write copy of that filesystem
- To optimize container boot time, copy-on-write is used instead of regular copy
- docker run starts a container from a given image