Score:0

How can I fix docker networking issues with multiple containers when working locally

gb flag

So I have a basic docker setup with nginx and php-fpm that gets deployed to AWS ECS:

Dockerfile for nginx

# Install nginx
FROM --platform=linux/amd64 nginx:latest

# Use custom configs
ADD nginx/conf/nginx.conf /etc/nginx/conf.d/default.conf

Nginx config file

server {

    server_name localhost;

    root /usr/share/nginx/html/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

}

Dockerfile for php-fpm

# Install nginx
FROM --platform=linux/amd64 php:8.1-fpm

# Add the files
COPY dist /usr/share/nginx/html

Question

Note: This setup works well when I deploy to AWS.

But when I try the images locally, it does not work (Bad Gateway) because the nginx container does not find localhost:9000 defined in the nginx config. I have to use localhost due to the AWSVPC network mode.

How can I also make it work locally without editing too many things? Is there a config option when I run the containers locally? Right now, I run them like this:

docker run -p 8001:80 -d --name $DEPLOY_NAME_NGINX $DEPLOY_NAME_NGINX
docker run -d --name $DEPLOY_NAME_PHP_FPM $DEPLOY_NAME_PHP_FPM
Tom Yan avatar
in flag
I think you need `--network="host"` for at least the nginx `docker run` command and a `-p` that maps port 9000 of the docker host to the port the php-fpm listens inside its container. (Or maybe, if that's also 9000, just use `--network="host"` for both `docker run` commands.) See [this answers](https://stackoverflow.com/a/24326540) for more details.
andreas avatar
gb flag
This was a good idea but I found that the host network option does not work on MacOS (we work mostly on Mac at this time for development).
Tom Yan avatar
in flag
Well, obviously the simplest alternative would be to replace `localhost` with the IP of the php-fpm container, assuming the two containers can communicate with each other (e.g. connected to the same bridge).
andreas avatar
gb flag
I tried that (successfully), but the communication then breaks on AWS. It seems that on the VPC that AWS generates the local IPs are different...
Tom Yan avatar
in flag
Yeah I don't expect you can have entirely portable configs if you can't `docker run` identically on different places. Not familiar enough with docker to tell what exactly are the "best practices" to deal with this kind of problem.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.