2

I was trying to learn docker and this is something I can't understand. I was trying to set up apache2 and php(fpm) in seperate containers. My folder structure is like this

.
├── apache
|   ├── dockerfile
|   └── demo.apache.conf
├── php
|   ├── dockerfile
|   └── www.conf
├── public_html
|   ├── index.php
|   └── test.html
└── docker-compose.yml

This is my apache docker file

FROM ubuntu
RUN ["apt-get","update"]
RUN ["apt-get","install","apache2","-y"]
RUN apache2ctl start
RUN a2enmod proxy_fcgi
COPY demo.apache.conf /etc/apache2/sites-available/000-default.conf
VOLUME  /etc/apache2/
EXPOSE 80
ENTRYPOINT apache2ctl start && tail -f /dev/null

and PHP docker file

FROM ubuntu
RUN ["apt-get","update"]
RUN apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y
EXPOSE 9000
COPY www.conf /etc/php/7.2/fpm/pool.d/www.conf
ENTRYPOINT service php7.2-fpm start &&  tail -f /dev/null

and the docker-compose.yml file

version: "3.2"
services:
  php:
    build: './php/'
    networks:
      - backend
    volumes:
      - ./public_html/:/var/www/html/
  apache:
    build: './apache/'
    depends_on:
      - php
    networks:
      - backend
    ports:
      - "8080:80"
    volumes:
      - ./public_html/:/var/www/html/
networks:
  backend:

When I run it using

docker-compose up -d

I can access both index.php (and test.html) at localhost:8080. However if I try to create images for both docker files seperately and create containers using the run command like this

docker build -t apache2 .
docker build -t phptest .
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 100:80 --name apachecontainer apache2
docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 9000:9000 --name phpcontainer phptest

I get the following error when I try to reach localhost:100

Proxy Error The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request
Reason: DNS lookup failure for: php

However I could access the html file at localhost:100/test.html

I suspected the backend network defined in the docker-compose file might be the key. So I tried to create a new network and add both the containers to it and disconnecting it from the default bridge network but it didn't work. Why doesn't creating two seperate containers manually not working here? what am I doing wrong? How do I fix it

UPDATE 1: I also tried by creating the images with

docker build -t apache2 . docker build -t phptest .

and in the docker compose used image: "phptest" and image: "apache2" instead of the build commands and it works. But it does not work if I create the containers with the same images using the above mentioned docker run commands

UPDATE 2: I used the demo.apache.conf used here

1 Answers1

3

I fixed the problem. I used the docker container inspect command to inspect the four containers, the two created with docker run command and the two created with docker-compose up -d command. And on comparing them found that both the containers, created with the docker-compose command, had a network-alias and the php container did not map the exposed port 9000. So I changed my commands and ran the following

docker network create testnet

docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html --network testnet --network-alias php --name phpcontainer phptest

docker run -d -v /home/user/Desktop/DockerTesting/public_html:/var/www/html -p 100:80 --network testnet --network-alias apache --name apachecontainer apache2

and now everything seems to work.