Introduction to Docker, and a Practical Example

July 24, 2019

Table of Contents


What is Docker and what you can do with it?

Docker is a tool that allows us to prepare packaged applications. Those packages are called containers.


One of the main advantages of using Docker is that you would need to set up the application you want to use, only once.

To the contrary, if you were to set up the application in the usual way, you may need to repeat the set up process when, for instance, you had to install it on another machine.


Let me clarify this with an example. Let's say that the application we are talking about is a web server ... like Apache Web Server.


In the usual way, to install a web server, the first steps would be to download, install, and configure it.
Then, you may need to install additional extensions or add ons. In some cases, you would also have to compile from source code programs.


So...what would happen if you need to set up the same web server on another machine?...Well, you would need to repeat all those steps again.


With Docker you would have defined all those steps on a text file--called Dockerfile--and then run it with the Docker command.


Docker automates the set-up process, so that when you require to repeat the process, you only have to copy the Dockerfile and run it. This is good when you need to install the application on another machine, and also if you want your friends, or coworkers to have the same set-up as yours.


Practical Example: Creating a Docker Image and Container

Let's try Docker with an actual example.
We are going to create a container of an Apache Web Server running on Centos 7.


We start by creating a text file named "Dockerfile" and adding the following content on it:


FROM centos:7

RUN yum -y update
RUN yum -y install httpd httpd-tools

EXPOSE 80

CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
Download this Dockerfile here

• The first line of our Dockerfile defines that we will be using Centos 7 as the basis of our container.
• Then we indicated that we are going to install the "httpd" and "httpd-tools" packages. (The Apache Web Server)
• And lastly we indicate to start the web server by running the httpd command.


Now we are ready to run our first Docker command.

On a terminal window, and on the same folder that we have our Dockerfile, type the following command, to create a Docker Image:


docker build -t image_apache .

• The parameter after the "-t" flag, is the name of the image--which, in this example, we are naming "image_apache"
• You can use any name you want.


The output of the command will show all the required software being downloaded, installed and configured automatically.








Once that the above installation process has finished, we can list the images installed on our system, and confirm that the one we just created is there.


docker images

The docker images command will show the images:




Now, we are ready to create our first container.

Enter the following command in your terminal window.


docker run -tid -p 4000:80 --name=container_apache image_apache

• The parameter "-p 4000:80" indicates that the web server will be accessed using the port 4000.
• The parameter "--name=container_apache" indicates the name of our container.
• The last parameter is the the name of the image from where we are creating the container.




Then we can list the existing containers, to see the one we just created.


docker ps -a

On the list of containers, you will notice that the status of the containers is UP.




To test our container, open your favorite browser and go to the following url, http://localhost:4000

You should see the following screen, which is the default page of the Apache Web Server on Centos 7.





Attach a Local Folder to the Docker Container

Now that we have a web server running, you may be wondering how to a put my website in there.
Because if you are going to have a web server, it is because you want to run a website locally.


Well, what we have to do is to attach a local folder, containing our website files, to the container.


Let's say that we have a local folder with the following file:


/my_website/
 └─ index.html

And with the following content on the index.html files:

/my_website/index.html

<html>
<head>
    <title>My Website</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

We want now to use our local folder as the root folder our web server on the Docker container.


First, we would need to stop and delete the container that we have just created above.

If you didn't create the container, just skip this step.


docker container stop <container_id>

docker rm <container_id>

• In the above command replace <container_id> with the actual container id, which is displayed when the container was created.
• On the commands, for the container id, you only need to specify the first three characters.






Now we create the Docker container with the following command.


docker run -tid -p 4000:80 --name=container_apache -v /my_website:/var/www/html image_apache

• The parameter "-v /my_website:/var/www/html" indicates that our local folder will be mapped as the root folder of the webserver.
• On Centos 7 the root folder of the web server is /var/www/html.

If you open the browser at the url http://localhost:4000, you will see your website's index page.





Additional Docker Commands

There are some additional Docker Commands that may be very useful.


The following command will allow you to open a terminal window into the Docker Container.


docker exec -it container_apache bash

To view the logs.


docker logs -f container_apache

To start a container.


docker container start <container_id>

To stop a container.


docker container stop <container_id>