Apache in a Docker container
Written by Guillermo Garron
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:
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
You have to change 0EBFCD88 with the last eight numbers of the output of the previous command
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get install docker-ce docker-ce-cli containerd.io
For more info, read here
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.
Get the Ubuntu docker image
Now that we have docker installed, get the Ubuntu image
sudo docker pull ubuntu
Verify you have it:
sudo docker images
Something like this should appear:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB
Create a docker container from the image
Once you have the image, you can create a running container from it with this command:
docker run -it --name=my-apache-container -v /home/user/var/www/html:/var/www/html -v /home/user/var/log:/var/log -p 80:80 -p 143:143 4e5021d210f6 /bin/bash
This is what each of the options means
- -i
- Interactive on, it will keep STDIN open
- -t
- Allocate a pseudo-tty
- --name
- Creates a name for your container so you can refer to id later
- -v
- 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:
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
To these
ErrorLog /var/log/apache_error.log
CustomLog /var/apache_access.log combined
Now exit the container
exit
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.
Commit changes to docker image
In order to commit the changes you need the container ID.
sudo docker ps -a
You should get something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
316c5886ebf6 4e5021d210f6 "/bin/bash" 2 hours ago Up 24 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:143->143/tcp my-apache-container
Then run this command:
docker commit 316c5886ebf6 user/repository:tag
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.