Bobcares

How to setup Apache webserver in Docker containers

by | Nov 29, 2016

In our previous post on Docker web hosting, we saw the benefits of Docker that makes it suitable for specialized hosting at low costs.

In web hosting, there are many key components such as web server, email server, dns server, etc. that are required for the website functioning.

Setup and manage your Docker system

GET IN TOUCH WITH THE DOCKER EXPERTS NOW!

 

Today, we’ll discuss how to setup an Apache webserver in Docker container.

1. Create Apache Docker container

The first step is to create a Docker container with the Apache image ‘httpd’. This can be done using the command ‘docker run’, giving the Apache directory as parameter:

docker run -dit --name apache-web -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

 

A container with the Apache webserver image was created, and listed, as seen from the ‘docker ps’ command.

Create Apache Docker container

Create Apache Docker container

2. Edit the Apache configuration file

Once the webserver container is created, the Docker container can be accessed using ‘docker exec’ command to generate a bash session.

The configuration file for the webserver would be in the folder ‘/usr/local/apache2/conf/’ and can be edited further to suit your requirements.

 

Edit the apache config file

Edit the apache config file

 

[ Are your spending too much time managing your Docker containers? Our Docker experts take care of your infrastructure and ensure its smooth functioning. ]

3. Enable PHP support in Apache

To enable PHP support in the Apache webserver, you would have to use an image with Apache and PHP support, say ‘php:7.0-apache'.

Also, other relevant modules such as php-mysql and tools such as curl, would have to be installed.

The apache webserver would be now running inside the container and can be verified from within the bash session.

Apache processes running in Docker container

Apache processes running in Docker container

4. Enable SSL support

Most webservers require SSL support for encryption. You can setup Let’s Encrypt, the free SSL, in the Docker container. Let’s Encrypt would generate cert and key files, which can be used for encrypting the webserver.

The certificate and key files can be copied over to ‘/usr/local/apache2/conf/’ and specified in the httpd.conf file. Restart the apache webserver to enable SSL support.

[ Running a Docker infrastructure doesn’t have to be hard, or costly. Get world class Docker management services at affordable pricing. ]

5. Expose the webserver port

By default, port 80 is the webserver port. But the Docker container port cannot be accessed from outside the host machine.

A webserver is meant to be accessible from outside. So, we need to expose the host machine port to bind the port 80 in host to the webserver port in the container.

This can be done with the command ‘docker run -d -p 80:80 -t httpd:2.4′  while creating the webserver container. For using SSL with webserver, the https port 443 need to be exposed by forwarding host port 443 to container port 443.

In our post on ‘How to expose docker ports‘, we’ve discussed about exposing Docker ports to make the web server content in the container externally accessible.

6. Configure Apache webserver in custom OS

The image we used for Apache webserver is ‘docker http server’ based on Alpine Linux. You can install and configure Apache in your server OS, say Ubuntu.

For that, you need to first create a container with Ubuntu image and then install apache web server in it. The custom images can be uploaded to the repository.

At Bobcares, we maintain a private docker registry for our internal Docker infrastructure, with custom images that are secure and optimized.

If you’d like to know how to manage your Docker system and custom images to get the best out of them for your business, we’d be happy to talk to you.

 

Do you provide Docker hosting services?

Wish you had more time to focus on your business? Let us help you.

Our engineers will assist you to setup, monitor and manage your Docker infrastructure 24/7.

GET IN TOUCH WITH THE EXPERTS NOW!

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

1 Comment

  1. prashant

    docker: Error response from daemon: driver failed programming external connectivity on endpoint hardcore_goodall (bc97a359e5143431dceb035960c7590ecad6c45dd38fc89feee808a4de8686bd): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.