Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running virtual hosts in apache docker container

I have two php applications in the same apache container and I'm trying to run one of them on a port since it needs to be accessible via a root domain and not a subfolder.

I want to run the application on port 8060 which I've tried doing using apache virtual hosts but it won't load the page (http://192.168.99.100:8060/) it just says connection refused. However the normal root ip - http://192.168.99.100 works fine. My docker file is as follows

version: '3.2'
  services:
    php-apache:
      build:
        context: ./apache-php
      ports:
        - 80:80
        - 8060:8060

      expose:
        - '8060'
      volumes:
        - ./DocumentRoot:/var/www/html:z

My apache configuration

<VirtualHost *:60>
    DocumentRoot /var/www/html/api
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Any help would be greatly appreciated.

like image 840
Luke.T Avatar asked May 01 '19 15:05

Luke.T


People also ask

Can I run Apache in Docker?

To run the Apache container, you will need to run the Docker command as follows: 1. Invoke the docker run command to create a new container based on your downloaded Apache Docker image. The docker run command then returns the unique container ID of the container you've just created.

Can you use localhost in Docker?

Docker provides a host network which lets containers share your host's networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself. Now your container can reference localhost or 127.0. 0.1 directly.

Is Docker good for hosting?

Container HostingHost your Docker applications on the best cloud platform. Docker is a popular containerization tool in DevOps. It runs your application inside a container, uses minimum resources, can be deployed faster, and it can scale quickly.

Can Docker container communicate with host?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.


1 Answers

Here is a solution -- Step by Step:

Step 1:

Add/Update extra_hosts, hostname, domainname and in docker-compose.yml and in my case I'm running it over PORT 80.

This is how my php service looks like in docker-compose.yml file.

php:
   build: .
   image: php:7.2-apache
   working_dir: /var/www/html
   volumes:
     - ./:/var/www/html
     - ./php.ini:/usr/local/etc/php/php.ini
   extra_hosts:
     - "lara.local:127.0.0.1"
   hostname: lara.local
   domainname: local 
   ports:
     - 80:80  
   environment:
     - "DB_PORT=3306"
     - "DB_HOST=database"

Step 2:
Update your hosts file with following line:

127.0.0.1 lara.local

Step 3: Test our hostname by running this command

docker exec -it <your-php-container-name> hostname

If you see output lara.local then you are good to go!

Step 4: Rebuild app

docker-compose build

Step 5: Start the services and check the app is running at http://lara.local

docker-compose up -d

Note: If you are using a different port for example 8080 then it would be http://lara.local:8080

PS. If you want to change the DocumentRoot then ssh to your container and cd /etc/apache2/sites-available/000-default.conf and change DocumentRoot path as per your needs.

like image 199
Hardik Raval Avatar answered Oct 12 '22 09:10

Hardik Raval