Point to a Docker container with apache

1

Personally, I'm starting to use Docker, but I still have a lot of questions and one of them is how to point a domain to a specific container.

Firstly 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's apache and it worked as expected until then.

"Name-based Virtual Hosts": link

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 a lot of tutorials, doubts and etc and I came to the conclusion that I would need to configure a proxy in apache for this, so I came up with 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 does not work, so neither does the domain with the port work.

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

    
asked by anonymous 18.04.2017 / 16:31

2 answers

0

I was able to find a solution

Follows:

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

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 "php: 5.6-apache" image

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

4 - Within the container PHP-CONTAINER, we will execute the following commands to install apache, php and git also

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 - Exit PHP-CONTAINER and enter the APACHE and create the configuration file of your domain (PS: change all example.com for yourdomain.com)

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

<VirtualHost *:80>

ServerName example.com

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

</VirtualHost>

7 - Run the following commands inside 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: link

    
18.04.2017 / 17:43
2

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

You basically start the container by running

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

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

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

After that every request that comes to foo.bar.com will be redirected to your container internally

It is not necessary to publish on the container port, the ports work together with VIRTUAL_HOST normally like foo.bar.com:8080 for example

    
18.04.2017 / 16:57