0

I want to use a dockerized SQL database server, an additional client (here: phpmyadmin) and a reverse proxy to make the client interface reachable from outside servers.

So far I used this docker-compose file:

version: '3'

services:
        mariatest:
                image: mysql
                restart: always
                networks:
                       - dbnet
                environment:
                        MYSQL_ROOT_PASSWORD: password
                        MYSQL_DATABASE: test
                        MYSQL_USER: test
                        MYSQL_PASSWORD: test

        phpmatest:
                depends_on:
                        - mariatest
                image: phpmyadmin/phpmyadmin
                restart: always
                networks:
                        - dbnet
                ports:
                        - "9080:80"
                environment:
                        MYSQL_ROOT_PASSWORD: password
                        PMA_HOST: mariatest

        reverse:
                image: nginx
                networks:
                        - dbnet
                ports:
                        - "8000:80"
                volumes:
                        - /var/dockervolumes/nginx:/etc/nginx

networks:
     dbnet:

The taken from the volume I get the nginx default.conf file as

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://localhost:9080/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

When using the local machine with http://localhost:9080 I can use the PMA without any problems. When trying to use the reverse proxy address (http://localhost:8000) I only get an nginx error the page I am looking for is unavailable. Inside the logs I see that the reverse proxy fails on the connect() with connection refused.

What am I missing for such setups?

2 Answers 2

2

Most likely because localhost is container-based. Have you tried putting in your host's ip address, eg:

proxy_pass http://[HOSTIP]:9080/;

You may need to either static your container IPs or request them by their hostname. To use DNS, set the container host name (one way is to set container_name), eg:

services:
    phpmatest:
        container_name: phpmatest

and in nginx conf file:

proxy_pass http://phpmatest:80/;

should work because they are on the same container network.

You will need to also set up nginx to receive DNS from docker, in case a container needs to restart or change. There are tutorials on how to do this elsewhere.

Sign up to request clarification or add additional context in comments.

4 Comments

I added the container name and the nginx conf for the container name - after restarts, the nginx handles the name in the url, but fails with "connection refused" while connecting to upstream
have you tested connectivity from inside your nginx container? curl phpmatest:80
ok, I found my misunderstanding. I thought, the nginx has to target the exposed port, but phpmatest:80 is correct as it connects via the dbnet network directy. Thank you for the clarification and help!
@R.StackUser I'm late to the party but I have a near identical problem. I have tried to connect directly with curl, and I am getting connection refused. This is in my local dev environment as well.
2

Two issues with your code :

First :-

/var/dockervolumes/nginx:/etc/nginx This doesn't let nginx start inside container because nginx configuration doesn't get auto-created due to volume binding.

Try using docker ps command to verify.

Using /var/dockervolumes/nginx:/etc/nginx/conf.d works

I am assuming you will place your default.conf file inside conf.d folder

Note: If you bind any directory inside container to host system, that directory is not initialised by container.

Second :-

proxy_pass http://localhost:9080/;

You assuming that localhost inside your container and you real host is same.

Use proxy_pass http://phpmatest; that will work because your service name is phpmatest

Note: If you create a customer docker network i.e dbtest in your case. All services using same network are directly accessible by their name i.e phpmatest in your case.

Following works :-

docker-compose file

version: '3'

services:
        mariatest:
                image: mysql
                restart: always
                networks:
                       - dbnet
                environment:
                        MYSQL_ROOT_PASSWORD: password
                        MYSQL_DATABASE: test
                        MYSQL_USER: test
                        MYSQL_PASSWORD: test

        phpmatest:
                depends_on:
                        - mariatest
                image: phpmyadmin/phpmyadmin
                restart: always
                networks:
                        - dbnet
                ports:
                        - "9080:80"
                environment:
                        MYSQL_ROOT_PASSWORD: password
                        PMA_HOST: mariatest

        reverse:
                image: nginx
                networks:
                        - dbnet
                ports:
                        - "8000:80"
                volumes:
                        - /var/dockervolumes/nginx:/etc/nginx/conf.d

networks:
     dbnet:

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://phpmatest;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

1 Comment

default.conf works fine. i was running phpmyadmin on a random port & proxy_pass localhost:PORT;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.