Date: 2020-04-09 19:50:00 00:00

Introduction

Docker is a platform that lets you run applications in containers, with all its libraries and needed software so it can run the same in any computer with Docker installed, no matter what other software is installed on the host.

Unlike Virtual Machines, docker uses the same kernel of the host and that makes it faster than running applications on virtual machines.

We will now see how to:

Install Docker on Ubuntu

Get the Ubuntu docker image

Create a container from the docker image

Install Apache and configure it in the container

Commit the changes

Upload your image to docker for future uses

Install Docker on Ubuntu

Once you have your base Ubuntu server ready, run this single command to have it on Ubuntu

sudo apt-get install docker.io

You can also install the docker version, which is called docker-ce you have to run these commands:

But I prefer to install it using the Debian package, it should work the same and does not mess with the Ubuntu libraries, it maybe outdated from time to time, but I don't think I will need the latest features.

The volume option binds a volume from the host to the container, this way you our Apache server will serve the site from the host file system

-p

This option binds ports, in our case we are binding host's ports 80 and 143 to the same ports in the container, if you have more Apache containers running you can bind different ports to each one, like 8080, 8081 and so on, and have an Nginx server running as reverse proxy forwarding traffic to each docker container

Install Apache and configure it in the container

To install Apache once inside the container run this command

sudo apt-get update && sudo apt-get install apache2

Then in order to avoid this error apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message run this command

sudo echo "ServerName localhost" >> /etc/apache2/apache2.conf

And in the file /etc/apache2/sites-enabled/000-default.conf change these lines:

In order the test if everything is working, you need to create the user user in our case.

sudo adduser user

Then create the folder /home/user/var/www/html and /home/user/var/log

su - user
mkdir -p var/www/html
mkdir var/log

Point your browser to the IP of the server and you should see the Apache default page, and now you can just change the files in /home/user/var/wwww/html and those are going to be the files served by the Apache server in the container.

Replace the container ID with yours, and user/repository with yours (get it from docker.com) and assign the image an identifier tag.

Upload the new created image to docker.com

First login into docker.

sudo docker login -u "username" -p "password" docker.io

And push your image:

sudo docker push user/repository:tag

Conclusion

For me it is great to have my server in a docker container, I can now easily move from one VPS provider to another, I just need to install Ubuntu and donwload my docker image, create the container and upload my site to the right folder, then I just need to point the DNS to the new site.