zonyang
zonyang

Reputation: 878

Docker: communication between web container and php container

I'm trying to dockerizing a project runs with php + Apache http server. I learned that I need to have a container for apache http server and another container for php script. I searched a lot but still don't understanding how that works. What I know now is I should resort to docker networking, as long as they are in the same network they should be communicating with each other.

The closest info I got is this, but it uses nginx:

https://www.codementor.io/patrickfohjnr/developing-laravel-applications-with-docker-4pwiwqmh4

enter image description here

quote from original article:

vhost.conf

The vhost.conf file contains standard Nginx configuration that will handle http
requests and proxy traffic to our app container on port 9000. Remember from
earlier, we named our container app in the Docker Compose file and linked it to the web container; here, we can just reference that container by its name and Docker will route traffic to that app container.

My question is what configuring I should do to make the communication between php container and web container happen using Apache http server like above? what is the rationale behind this? I'm really confused, any information will be much much appreciated.

Upvotes: 0

Views: 1390

Answers (2)

chip
chip

Reputation: 411

The example that you linked to utilizes two containers:

  1. a container that runs nginx
  2. a container that runs php-fpm

The two containers are then able to connect to each other due to the links directive in the web service in the article's example docker-compose.yml. With this, the two containers can resolve the name web and app to the corresponding docker container. This means that the nginx service in the web container is able to forward any requests it receives to the php-fpm container by simply forwarding to app:9000 which is <hostname>:<port>.


If you are looking to stay with PHP + Apache there is a core container php:7-apache that will do what you're looking for within a single container. Assuming the following project structure

/ Project root
- /www/ Your PHP files

You can generate a docker-compose.yml as follows within your project root directory:

web:
  image: php:7-apache
  ports:
    - "8080:80"
  volumes:
    - ./www/:/var/www/html

Then from your project root run docker-compose up and will be able to visit your app at localhost:8080

The above docker-compose.yml will mount the www directory in your project as a volume at /var/www/html within the container which is where Apache will serve files from.

Upvotes: 1

Andy Shinn
Andy Shinn

Reputation: 28483

The configuration in this case is Docker Compose. They are using Docker Compose to facilitate the DNS changes in the containers that allow them to resolve names like app to IP addresses. In the example you linked, the web service links to the app service. The name app can now be resolved via DNS to one of the app service containers.

In the article, the web service nginx configuration they use has a host and port pair of app:9000. The app service is listening inside the container on port 9000 and nginx will resolve app to one of the IP addresses for the app service containers.

The equivalent of this in just Docker commands would be something like:

App container:

docker run --name app -v ./:/var/www appimage

Web container:

docker run --name web --link app:app -v ./:/var/www webimage

Upvotes: 0

Related Questions