semiotmic2022
based on
https://training.play-with-docker.com/ops-s1-images/
The code of this section is here
If you are running Docker online: https://labs.play-with-docker.com/ you can upload files in the session terminal by dragging over it.
First thing you may want to do is figure out how to create our own images. While there are over 8 millions images (as of January 2022) on Docker Hub, it is almost certain that none of them are exactly what you run in your data center today. Even something as common as a Windows OS image would get its own tweaks before you actually run it in production.
We will start with the simplest form of image creation, in which we simply commit one of our container instances as an image. Then we will explore a much more powerful and useful method for creating images, the Dockerfile.
An important distinction with regard to images is between base images and child images.
Another key concept is the idea of official images and user images. (Both of which can be base images or child images.)
python
, node
, alpine
and nginx
are official (base) images.To find out more about them, check out the Official Images Documentation.
user/image-name
. The user
value in the image name is your Docker Store user or organization name.Let’s start by running an interactive shell in a ubuntu container:
As you know from before, you just grabbed the image called “ubuntu” from Docker Store and are now running the bash shell inside that container.
To customize things a little bit we will install a package called figlet in this container. Your container should still be running so type the following commands at your ubuntu container command line:
You should see the words “hello docker” printed out in large ascii characters on the screen. Go ahead and exit from this container
Now let us pretend this new figlet application is quite useful and you want to share it with the rest of your team. You could tell them to do exactly what you did above and install figlet in to their own container, which is simple enough in this example. But if this was a real world application where you had just installed several packages and run through a number of configuration steps the process could get cumbersome and become quite error prone. Instead, it would be easier to create an image you can share with your team.
To start, we need to get the ID of this container using the ls command (do not forget the -a option as the non running container are not returned by the ls command).
Before we create our own image, we might want to inspect all the changes we made. Try typing the command
for the container you just created. You should see a list of all the files that were added (A) to or changed (C ) in the container when you installed figlet. Docker keeps track of all of this information for us; this is part of the layer concept.
Now, to create an image we need to “commit” this container. Commit creates an image locally on the system running the Docker engine. Run the following command, using the container ID you retrieved, in order to commit the container and create an image out of it.
That’s it - you have created your first image! Once it has been commited, we can see the newly created image in the list of available images.
You should see something like this:
Note that the image we pulled down in the first step (ubuntu) is listed here along with our own custom image. Except our custom image has no information in the REPOSITORY or TAG columns, which would make it tough to identify exactly what was in this container if we wanted to share amongst multiple team members.
Adding this information to an image is known as tagging an image. From the previous command, get the ID of the newly created image and tag it so it’s named ourfiglet:
Now we have the more friendly name “ourfiglet” that we can use to identify our image.
Here is a graphical view of what we just completed:
Now we will run a container based on the newly created ourfiglet image:
As the figlet package is present in our ourfiglet image, the command returns the following output:
This example shows that we can create a container, add all the libraries and binaries in it and then commit it in order to create an image. We can then use that image just as we would for images pulled down from the Docker Store. We still have a slight issue in that our image is only stored locally. To share the image we would want to push the image to a registry somewhere. We'll see how to do this later…
This approach of manually installing software in a container and then committing it to a custom image is just one way to create an image. It works fine and is quite common. However, there is a more powerful way to create images. In the following exercise we will see how images are created using a Dockerfile, which is a text file that contains all the instructions to build an image.
Instead of creating a static binary image, we can use a file called a Dockerfile to create an image. The final result is essentially the same, but with a Dockerfile we are supplying the instructions for building the image, rather than just the raw binary files. This is useful because it becomes much easier to manage changes, especially as your images get bigger and more complex.
Dockerfiles are powerful because they allow us to manage how an image is built, rather than just managing binaries. In practice, Dockerfiles can be managed the same way you might manage source code: they are simply text files so almost any version control system can be used to manage Dockerfiles over time.
We will use a simple example in this section and build a “hello world” application in Node.js. Do not be concerned if you are not familiar with Node.js; Docker (and this exercise) does not require you to know all these details.
We will start by creating a file in which we retrieve the host name and display it.
Type the following content into a file named index.js
:
The file we just created is the javascript code for our server. As you can probably guess, Node.js will simply print out a “hello” message. We will:
Create a file named Dockerfile and copy the following content into it:
Let’s build our first image out of this Dockerfile and name it hello:v0.1:
This is what you just completed:
We then start a container to check that our applications runs correctly:
You should then have an output similar to the following one (the ID will be different though).
What just happened? We created two files: our application code (index.js) is a simple bit of javascript code that prints out a message. And the Dockerfile is the instructions for Docker engine to create our custom container. This Dockerfile does the following:
Recall that in previous labs we put commands like echo "hello world" on the command line. With a Dockerfile we can specify precise commands to run for everyone who uses this container. Other users do not have to build the container themselves once you push your container up to a repository (which we will cover later) or even know what commands are used. The Dockerfile allows us to specify how to build a container so that we can repeat those steps precisely everytime and we can specify what the container should do when it runs. There are actually multiple methods for specifying the commands and accepting parameters a container will use, but for now it is enough to know that you have the tools to create some pretty powerful containers.
This example will build a basic REST server based on Flask. Flask is a “micro” web application framework written in Python. It allows writing simple and lightweight REST APIs.
The server we will design will handle a set of sensors, their creation, visualization, and update using an SQLite database.
We'll do this by first pulling together the components for a REST based server built with Python Flask, then dockerizing it by writing a Dockerfile. Finally, we'll build the image, and then run it.
We have to create the following files:
api.py
add.py
Dockerfile
api.py
We want to create a Docker image with this web app. As mentioned above, all user images are based on a base image. Since our application is written in Python, we will build our own Python image based on Alpine.
The Dockerfile
looks like this:
Now that we have the Dockerfile
, we can build the image. The docker build
command does the heavy-lifting of creating a docker image from a Dockerfile
.
When you run the docker build
command given below, make sure to replace <YOUR_USERNAME>
with your username. This username should be the same one you created when registering on Docker Hub.
The docker build
command is quite simple - it takes an optional tag name with the -t
flag, and the location of the directory containing the Dockerfile
- the .
indicates the current directory:
the generated output is something similar to:
If everything went well, your image should be ready! Run:
and see if your image shows.
The next step in this section is to run the image and see if it actually works.
So now we can execute the following commands:
add.py
As an add on to this example we show the content of a simple sensor's updater that could run in a simple sensor.
Clearly the value of rest_s_url
will have to be adapted according to the specific set-up, and also the way we get the sensor's value new_s_value
will depend on the sensors itself and the corresponding HW…
IMPORTANT: When we run the app we used the parameters -p 8888:5000
these remapped the 5000 port that is inside the docker container to the port 8888 of the hosting machine… more on this ahead
All the sensors' information will be lost if the container is deleted. To make it persistent, we have two possibilities:
First possibility:
This will use the "$PWD" to "bind mounts" to the host machine filesystem. This way we can control the exact mountpoint on the host. We can use this to persist data, but it’s often used to provide additional data into containers. When working on an application, we can use a bind mount to mount our source code into the container to let it see code changes, respond, and let us see the changes right away.
Second possibility:
and then
in this way we are using a named volume. Think of a named volume as simply a bucket of data. Docker maintains the physical location on the disk and you only need to remember the name of the volume. Every time you use the volume, Docker will make sure the correct data is provided.
Where is the data physically located?
But there's a caveat: You can't directly see the contents of volumes on Mac and Windows. This occurs because Docker actually runs a Linux VM to be able to containerize, since containerization is a native functionality for Linux but not in these others OSs. So the path that appears is actually the path inside the VM, and not on your host system. The solution is to create an ephemeral container just to view the content:
Now that you've created and tested your image, you can push it to Docker Hub.
First you have to login to your Docker Cloud account, to do that:
Enter YOUR_USERNAME
and password
when prompted.
Now all you have to do is: