Docker Images and Containers
Recommended by 0 users
A Docker container is just a basic version of the Linux operating system. The image below shows the software that is loaded into the container.
After you ran the previous command, Docker did the following:
1. Checked whether the “hello-world” software image had been installed or not.
2. Visited the Docker hub, found the software image, and then downloaded it onto your system.
3. The image was then loaded into the container and then run.
It is the first time we have mentioned the Docker Hub. At this stage, just know that it is a repository where Docker images can be found. Once you develop or create your own Docker image, you can upload it to the Docker Hub. From there other people can find it to download and use. You can also download images which have been created by others from the Docker Hub and then use them on your system. A Docker hub can also be made either private or public. If you choose a private one, it will only be visible to yourself while a public one can be shared by multiple people.
There are different variations of the Docker image. Some of the images, such as the one we have used in this book, execute just a simple and single command and then it exits. This is what happened with our previous “hello-world” command. In most cases, however, people want to use Docker to perform complex tasks.
This means that Docker can be used to perform extra tasks. It can be used for the to start and operate complex software such a database for the user. You can add data to the database and then store it via Docker. The data can also wait to be operated on by the next person.
With Docker, people can create software and then share is via the Docker images. The good thing with this is that you do not have to worry about whether an image can run on the Docker image. Docker container can run any type of software. This shows the kind of flexibility and portability which it creates. The process of developing the software can also be done inside Docker and then the software can be uploaded on the Docker Hub.
Finding and Running a Whalesay Image
People from different parts of the world can create Docker images. The images that are uploaded to the Docker Hub can then be accessed by anyone using Docker. Feel free to browse it and then find the images that you want. However, it is good for you to understand the kind of image that you need. This chapter of the book will help to guide
you in this.
Finding a Whalesay Image
1. Launch your browser and then search for the “Docker Hub”.
The Hub has the assembled Docker images as well as official images from some companies such as the IBM and RedHat among others.
2. At the top right of the page, look for the button labled “Browser & Search”. Just click on this button.
Clicking on this button will open the search engine for you.
3. On the search bar that appears, type the word “whalesay”. This is shown below:
4. After typing, hit the “Return” key or click on the search icon. You will be presented with the results of the search process. An example of this is shown below:
5. Click on the result shown in the figure above. A repository for the whalesay image will be displayed by the browser. A section of this repository is shown below:
The repository has information which tells us more about the image. The kind of information the image contains and directions on how to use it will be provided. You will also realize that the whalesay image is solely based on Ubuntu, which is one of the many distributions of Linux. You now need to know how to run this image on your computer.
How to Run the Image
You can open the terminal for the Boot2Docker.
1. Begin by opening the Launchpad and finding the “Boot2Docker” icon.
2. Click on the icon to start the Boot2Docker terminal.
3. Place your cursor at the terminal of the Boot2Docker. This is just after the “$” symbol.
4. On the command line, type the command “docker run docker/whalesay cowsay boo”. Once you have typed the command, just hit the “Return” key and then observe what happens. What the above command does is run the image in the container. The following will be observed on the terminal:
Whenever a software image is run on a system for the first time, Docker begins by looking for it in the local system. If the image is not found there, then Docker will have to find it from the Hub and then download it.
5. While being on the command line of the Docker, type the command “docker images” and then hit the “Return” key. All the images which are saved on your local system will be displayed. The image “docker/whalesay” will be
part of the list.
Whenever an image is run on your system, Docker just downloads it onto the system. This means that you will save time since the image is located in your local system. The second download will only be done if the location of the
image is changed. You can also choose to delete the image. However, for this case, let it stay on your local system since we will use it later.
You have now learned the procedure that you can use to search for images from the Docker Hub. You also learned how to use the command line for running your image. Note that once you run an image it will be saved on your system.
It is possible for us to improve the whalesay image. This is what we want to do in this chapter by building a new version of the image.
Writing a Dockerfile
We need to begin by writing a Dockerfile. You should open the Mac text editor and then write a Dockerfile, just a short one. This will define or describe the software from which the image has been created. This can also serve to instruct the software on the commands to run or the environment to be used.
Begin by opening the terminal if it is closed. You can follow the steps given below:
1. Open Boot2Docker by opening the Launchpad and then finding the icon for this. The icon is shown below:
2. Click on the icon to open the Boot2Docker terminal. It will be opened.
3. On the Boot2Docker terminal, just place your cursor at the prompt.
4. You should then create a new directory in your system. Just type the command “mkdir dockerdirectory” and then hit the “Return” key. This directory should provide you with the context for your build image. It will provide you with all the resources necessary for you to build the image.
5. Change to the directory which you have just built. Use the following command:
At this moment, the directory contains nothing.
6. You now need to create a new Dockerfile in this directory. On the command
line, type the command “touch Dockerfile” and then hit the “Return” key. You might think that the command will have no effect or does nothing. It will have created a new file in the directory that you specified. In this case it is the
new directory. To verify whether the file was created or not just type the command “ls Dockerfile” and observe the result.
This is shown below:
The above figure shows that the file was created successfully. If this was not successful, we would have been notified of this as well.
7. You should then open the new file in the Text editor for Mac. Just run the command “open –e Dockerfile” on the terminal and observe what happens. The text editor will be opened but it will have nothing in it:
8. Inside the opened file, just type “FROM docker/whalesay:latest”. This means that it should be as follows for now:
9. The keyword “FROM” informs the Docker of the image which forms the basis for your image. In our case, our image will be based on the “whalesay” image.
10. You should then add the “fortunes” program to your image. This can be done as follows:
11. This program provides us with a command which prints out wise sayings which will be said by our whale . This means that we should begin by installing this. The line added above uses the “apt-get” command to install the fortune program. However, if you find them added for you, just type them correctly and they will work.
12. After the image has obtained the required software, we need to tell the software to run the image which has already been loaded.
This can be done by adding the following line of command to our file:
13. You can then save the work you have done. Just click on “File” and then choose “Save”. To access these just press “CMD + S” on the keyboard or from the TextEdit menu. At this point, the behavior and the ingredients needed by your software have been described in the text file which we have created.
Building the Image
At this step, we need to use the Dockerfile we have created to build our image. Just follow the steps given below:
1. Open the terminal of the Boot2Docker and then place your cursor on it.
2. On this terminal, type the command “cat Dockerfile”. This will make sure the text file is already in the current directory.
3. On the same terminal, type the command “docker build -t docker-whale.” and then execute it. This command will create the image for you. However, make sure that you do not forget the period (.) at the end of the command.
You need to be patient as the command might take seconds to run and then report the outcome to you. However, you need to understand the build process of the Dockerfile so that you can then do something to your image. Let us explore this.
The build process
The command “docker build -t docker-whale.” plays the major role of building the image. What it does is takes the file “Dockerfile” from the current directory and then creates the image from it. The image is given the name “docker-whale” and then saved on your local machine. Although the command takes less than a minute to run, you will notice that its output is too long and seems to be a bit complex. It is good for you to understand what the messages you get during the build process mean.
You must have seen the message shown in the figure below:
At this step, the Docker is checking to be sure that it has everything necessary for it to build the image. After this step, the Docker together with the whalesay image will be loaded. Remember that the image is already in the local system since we downloaded it, meaning that there is no need for downloading it at this time.
The next step for the Docker is to update the “apt-get” package manager. The process of doing this will be too lengthy, meaning that you will notice numerous messages as the process progresses.
As the messages are numerous there is no need for us to discuss all of them. After this, a new “fortunes” software will be installed by the Docker. During this process, you will observe the following messages:
Finally, the process will be completed and the outcome will be reported. This will be as follows:
At this stage, you will have created your own Docker image which you can use.
Running the Image
This is the step at which you can verify whether or not the image is on your local system and then run it if it is found. Kindly follow the steps given below:
1. If the image is not there, just place your cursor on your terminal.
2. On this terminal, type the command “docker images” and then hit the “Return” key. As you know, this command will list all of the Docker images which are located on your local system. This is shown below:
3. You should then run the newly created image. On the same terminal, type the command “docker run docker-whale” and then hit the “Return” key. You will notice that the whale will be somehow shorter compared to what you
You will also notice that it has its own things which it can say. The command line will also be somehow shorter. The Docker also did not download since all the needed images were already on our local system. These were available for use by Docker during the build process.