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. Try it out with an image you have not downloaded yet:
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 d2c94e258dcb 22 months ago 13.3kB
hello-world linux 74cc54e27dc4 5 weeks ago 10.1kB
In the previous lab, you ran a “Hello World” container, which simply printed a test message and then exited. While this is a basic example, containers can be far more powerful—they can run interactively, much like virtual machines, but with significantly lower resource overhead.
For example, let’s start an interactive container using the MariaDB image. The -i (interactive) and -t (TTY) options allow you to access the container’s shell directly:
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