Nginx load balancer with docker-compose resolving url but not the web

I have this docker-compose file that deploys my 3 services

version: '3'

services:
    db:
        image: mariadb:10.3.9
        volumes:
            - data:/var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=secret
            - MYSQL_DATABASE=wordpress
            - MYSQL_USER=manager
            - MYSQL_PASSWORD=secret
    wp:
        image: wordpress:4.9.8
        depends_on:
            - db
        volumes:
            - ./target:/var/www/html
        environment:
            - WORDPRESS_DB_USER=manager
            - WORDPRESS_DB_PASSWORD=secret
            - WORDPRESS_DB_HOST=db
        ports:
            - "80"
              #deploy:
              #mode: replicated
              #replicas: 3
    lb:

        image: nginx:latest
        volumes:
             - ./nginx.conf:/etc/nginx/nginx.conf
        depends_on:
            - wp
        ports:
          - "8080:80"


volumes:
    data:

You can escalate to more than 1 wordpress any time you want, just uncommenting that. The idea is to have 3 exact same wp and nginx as load balancer.

My nginx.conf is like this

user  nginx;

events {
    worker_connections   1000;
}
http {
        server {
              listen 80;
              location / {
                proxy_pass http://wp:80;
              }
        }

Now if I go to localhost:8080 the url resolves to http://wp/wp-admin/install.php but the web is blank and I can’t connect. What did I do wrong?

I don’t use wordpress, but it seems that the configuration of worpress redirects the client to the location you see in the browser. You can easily check the behavior yourself: run curl -i localhost:8080 in a terminal and you will see that the container responds with a 301 pointing to the location you see in your browser.

Neither docker, nor nginx are responsible to make your host resolve to container servicenames. You will have to take care of this yourself.

You have severall options for a solution:

  • configure wordpress as localhost:8080 (there must be config php file or a database setting for this)
  • run your nginx on port 80 and add “wp 127.0.0.1” to /etc/hosts
  • run you nginx on port 80 and use a local dns server like dnsmask, pihole or unbount to add your own custom name resolutions for “wp” as a record for the ip 127.0.0.1, then point your host’s first nameserver to your local dns server and let it take care of the name resolution.