Point domain to a Docker container with apache

Asked

Viewed 3,258 times

2

Talk personal, I’m starting to use Docker, but I still have many questions and one of them is how to point a domain to a specific container.

First I created a Container with a php and apache image in the version I needed and made the link with two other containers

docker run -i -t -p 8080:80 --name apache-php --link postgresdb:postgresdb --link mysqldb:mysqldb -v /home/volumes/html:/var/www/html php:5.6-apache /bin/bash

As I use Linode hosting, I followed this tutorial to set up my domain in the container apache and it worked as expected so far.

"Name-based Virtual Hosts": https://www.linode.com/docs/websites/hosting-a-website

But obviously it is necessary to do some more configuration, because if I access my domain by setting the port I can see the page (example.com:8080), but if I do not put the port, the page does not open (example.com)

I read several tutorials, questions and etc and came to the conclusion that I would need to set up a proxy in apache for this, so I arrived at the following configuration file:

example.com.conf:

# domain: example.com
# public: /var/www/html/example.com/public_html/

<VirtualHost *:80>

  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin [email protected]
  ServerName  example.com
  ServerAlias example.com www.example.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/example.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/example.com/log/error.log
  CustomLog /var/www/html/example.com/log/access.log combined

    ProxyRequests On
    ProxyVia On
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    ProxyRequests Off
    ProxyPass / http://www.example.com:8080


</VirtualHost>

But it still doesn’t work, so neither does the domain with the door work.

The question is, can anyone help me finally solve this problem? rs

2 answers

3

I use Docker in production, and went through the same problem, found the solution in the container jwilder/nginx-proxy who proxy between the VIRTUAL_HOST and the containers , the github page for the project here

Basically you start the container running

docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

And the container you want to redirect adds an environment variable with the VIRTUAL_HOST

docker run -e VIRTUAL_HOST=foo.bar.com  ...

after that any request that comes to foo.bar.com will be redirected to your container internally

It is not necessary to give Publish on container port, doors work together with the VIRTUAL_HOST usually as foo.bar.com:8080 for example

1


I was able to find a solution

Follows:

1 - First let’s create a container and install Apache in it, this container will respond on port 80 of the same server and we will give the name of APACHE

docker run -it -p 80:80 -p 443:443 --name apache ubuntu:16.04 /bin/bash

2 - Inside the APACHE container, we will execute the following commands

apt-get update && apt-get install apache2 -y && apt-get install nano && a2enmod proxy && a2enmod proxy_http && service apache2 restart

3 - After leaving the APACHE container, we will create our container where our application will be allocated and we will call PHP-CONTAINER and use the image "php:5.6-apache"

docker run -i -t -p 8080:80 php:5.6-apache /bin/bash

4 - Within the PHP-CONTAINER, we will run the following commands to install apache, php and git as well

apt-get update && apt-get install apache2 php5 git -y 

service apache2 restart

5 - Create a php file inside the html Folder

nano /var/www/html/info.php

<?php phpinfo(); ?>

6 - Quit PHP-CONTAINER and log into APACHE and create your domain configuration file (PS: change all example.com to your domain.com)

nano /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>

ServerName example.com

ProxyPass / http://example.com:8080/

</VirtualHost>

7 - Execute the following commands within APACHE

a2ensite example.com.conf

service apache2 reload

8 - Test your domain in the browser now without the port and it will open

This response was based on the article: https://medium.com/@jmarhee/running-Multiple-web-Applications-on-a-Docker-host-with-apache-85f673f02803

Browser other questions tagged

You are not signed in. Login or sign up in order to post.