Need Two docker containers to communicate together.
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi I have a Laravel php app container and and Nginx container with a server block.

You can get the project here: https://we.tl/t-etfpxGnR9x

or from the repo here: https://bitbucket.org/danygiguere/dockerenvwebsite/src/master/

check the README.md

But basically there is a folder docker with an Nginx and a php folder. They each have a Dockerfile. In the Nginx folder there is also a dockerenv.conf with this code :

server {
listen 80;
index index.php index.html;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

server_name dockerenv.test;
resolver 172.17.0.1;

root /var/www/public;

set $upstream php-fpm:9000;

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass $upstream;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    proxy_pass http://dockerenv.test:9000;
}
location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}
}

Basically, I run these 2 commands to start both containers:

docker run -it -p 9000:9000 app:latest
docker run -it -p 8080:80 webserver:latest

The request is coming from dockerenv.test:8080 (which I have in my host file: 127.0.0.1 dockerenv.test.) it then goes into the server block above. There I need to route the request to the app container.

Right now I get a 502 with this error when I go to dockerenv.test :

[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: dockerenv.test, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:9000/index.php", host: "dockerenv.test:8080"

I'd need to get a 200 with my website.

It has to work without docker-compose because it's for aws EC2 autoscaling and I will pull the image from ECR. So I won't have the docker-compose file anymore on the server.

Are u okay with using docker-compose?
ST2-EV 1 month ago
It has to work without docker-compose. But if you have a docker-compose solution, maybe I can implement it with straight Docker. One thing for sure, there will be two separate containers.
randomhelprequested 1 month ago
docker-compose works with multiple containers..check out my solution below
ST2-EV 1 month ago
1 month ago

Crowdsource coding tasks.

2 Solutions


To be able to communicate with one another, the containers need to be in the same network.

You can either put them both in the host network, with --network host, which is not ideal.

Or you can create a new network, with docker network create myNetwork, and runt them both in that one with --network myNetwork.

To have static names to reference the php container from the nginx one, it needs a name, with --name php.

You'd be better of using docker-compose which automates all this for you

I have the same error with a custom network. What do you mean by : "To have static names to reference the php container from the nginx one, it needs a name, with --name php." ?
randomhelprequested 1 month ago
In your nginx config you're routing the requests to php-fpm and dockerenv.test on port 9000. First of all, don't they need to be the same? Secondly, you have to use the container name for it to work. So if you name your container --name php you should set $upstream php:9000; and proxy_pass http://php:9000;
Chris Werner Rau 1 month ago
I get this error: 2020/02/10 23:04:22 [emerg] 1#1: host not found in upstream "phpapp" in /etc/nginx/conf.d/dockerenv.conf:22 nginx: [emerg] host not found in upstream "phpapp" in /etc/nginx/conf.d/dockerenv.conf:22
randomhelprequested 1 month ago
or with a custom network, I get this error: dany@Danys-MacBook-Pro dockerenvwebsite % docker run -it -p 8080:80 --network=dockerenv_network webserver:latest 2020/02/10 23:08:08 [error] 6#6: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.18.0.1, server: dockerenv.test, request: "GET / HTTP/1.1", upstream: "http://172.18.0.2:9000/index.php", host: "dockerenv.test:8080"
randomhelprequested 1 month ago
docker build -f docker/nginx/Dockerfile . -t dockerenvwebsite_nginx docker build -f docker/php/Dockerfile . -t dockerenvwebsite_php docker network create dockerenvwebsite # change proxy_pass http://dockerenv.test:9000; to proxy_pass http://php-fpm:9000; docker run --rm -it --network dockerenvwebsite --name php-fpm dockerenvwebsite_php docker run --rm -it --network dockerenvwebsite -p 8080:80 dockerenvwebsite_nginx Seems to be able to start up, it might just be the nginx config, as the containers are able to communicate with one another...
Chris Werner Rau 1 month ago
Hey thanks for the help. I appreciate the effort. I still get a 502 though.
randomhelprequested 1 month ago
You seem to know docker well. Please have a look at the Readme.md you’ll find my email. Email me and we’ ll talk. I could pay more.
randomhelprequested 1 month ago
The solution is to rename the file dockerenv.conf to default.conf, as it clashed with nginx's default.conf. The modified default.conf is https://gist.github.com/cwrau/fe24bd93c94024fffb4bdb88743a5fc0
Chris Werner Rau 1 month ago

HI,
I would suggest using docker-compose,
It will make your life 100 times better.
make sure your directory setup is like this:
main folder
-laravelappfolder
-nginxappfolder
-docker-compose.yml
laravel folder should contain all the code for the php app with its own dockerfile that starts up the server at the required port.
nginx folder should contain all the code for the nginx server, essentially the nginx.conf and docker file.
Then the docker-compose.yml should look like:

use this link

This video is for flask with nginx:(it should help u)
video link
Other references:
docker-compose with php

Hi there. I can build it with docker-compose build. But then I push the built image to ECR (an AWS repo). And when I pull it, it doesn't come with the docker-compose file. The problem is I can't figure out how to start the container with docker run... I get a 502.
randomhelprequested 1 month ago
View Timeline