0

I'm trying to reach my existing instance of Wordpress through a Docker configuration.

My plan is to have a few websites running on my VPS, so I wanted to go for this Docker setup. First, my jwilder/nginx-proxy file:

version: '3'

services:
  nginx:
    image: jwilder/nginx-proxy
    container_name: nginx
    restart: always
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: 'true'
    ports:
      - 80:80
      - 443:443
    volumes:
      - /srv/nginx/data/certs:/etc/nginx/certs:ro
      - /srv/nginx/data/conf.d:/etc/nginx/conf.d
      - /srv/nginx/data/vhost.d:/etc/nginx/vhost.d
      - /srv/nginx/data/html:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    volumes:
      - /srv/nginx/data/vhost.d:/etc/nginx/vhost.d
      - /srv/nginx/data/certs:/etc/nginx/certs:rw
      - /srv/nginx/data/html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    depends_on:
      - nginx

networks:
  default:
    external:
      name: nginxproxy_default

I run it with sudo docker-compose up and it works great.

Then, I have this other file for one of my Wordpress projects:

version: '3'
services:

  wp:
    image: wordpress:php7.3-fpm
    container_name: wp_my_project
    expose:
      - 3001
    volumes:
      - ./conf/php/conf.d/php.ini:/etc/php/7.3/fpm/php.ini
      - ./conf/php/pool.d/www.conf:/etc/php/7.3/fpm/pool.d/www.conf
    environment:
      - WORDPRESS_DB_NAME=my_project
      - WORDPRESS_TABLE_PREFIX=wp_
      - WORDPRESS_DB_HOST=mysql_my_project
      - WORDPRESS_DB_USER=my_project_user
      - WORDPRESS_DB_PASSWORD=my_project_password
      - VIRTUAL_HOST=test.local
      - VIRTUAL_PORT=3001
      - LETSENCRYPT_HOST=test.local
      - LETSENCRYPT_EMAIL=my_project@gmail.com
    depends_on:
      - db
    restart: always

  db:
    image: mysql:8.0
    container_name: mysql_my_project
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=whatever
      - MYSQL_USER=root
      - MYSQL_PASSWORD=whatever_again
      - MYSQL_DATABASE=my_project
    restart: always

networks:
  default:
    external:
      name: nginxproxy_default

Before running them, I create network:

docker network create nginxproxy_default

I have been trying for days without success.

If I go into my nginx docker container using:

docker exec -it nginx bash

I can see this in the file /etc/nginx/conf.d:

...
# test.local
upstream test.local {
                ## Can be connected with "nginx-proxy_proxy" network
            # wp_my_project
            server 172.19.0.5:3001;
}
server {
    server_name test.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    include /etc/nginx/vhost.d/default;
    location / {
        proxy_pass http://test.local;
    }
}
server {
    server_name test.local;
    listen 443 ssl http2 ;
    access_log /var/log/nginx/access.log vhost;
    return 500;
    ssl_certificate /etc/nginx/certs/default.crt;
    ssl_certificate_key /etc/nginx/certs/default.key;
}
...

It looks OK but all I get when I try to navigate to http://test.local, all I get is a 502 error.

As a final piece of information, I'll provide here the result of:

docker network inspect nginxproxy_default

[
    {
        "Name": "nginxproxy_default",
        "Id": "aa332a0666fcf91af535134bb699caeef0eaf03136cc2582c21765fe03b8da31",
        "Created": "2019-06-19T20:28:53.590261887+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3567b10ef6c755ba733571a69a2b14ccc2e10d0a3288346f874cccb23ce9e4fa": {
                "Name": "wp_my_project",
                "EndpointID": "7e7fe30107887b0a2a83c60ac15896eab144294defa6df5ff0a5b0ee2debf40f",
                "MacAddress": "02:42:ac:16:00:03",
                "IPv4Address": "172.22.0.3/16",
                "IPv6Address": ""
            },
            "68a2608c34196bd2e9da20b4edc4d6c7851f27dcd3cf9db03ed2850e3ce4296e": {
                "Name": "nginx",
                "EndpointID": "ce124164464350d736afc94095242914bb7f8b7b59c4884b06c972612a6b8f0e",
                "MacAddress": "02:42:ac:16:00:04",
                "IPv4Address": "172.22.0.4/16",
                "IPv6Address": ""
            },
            "6a87fad4472a37702647b82885ad715acad21701ef992870a2f2cc3490df8a7c": {
                "Name": "mysql_my_project",
                "EndpointID": "71dce4bc75f496e908e4a16e948328fadfa5c4982aeb2898b98f868691465257",
                "MacAddress": "02:42:ac:16:00:02",
                "IPv4Address": "172.22.0.2/16",
                "IPv6Address": ""
            },
            "7518524e7bda2c41664cc46bd4c1d18bf5af09e1d5d2a66557eb49bfc6bff95f": {
                "Name": "letsencrypt",
                "EndpointID": "a15cb4d825cd53acad64cda5070eadc8f97bc5cab1ee45974f967c7da131c2e4",
                "MacAddress": "02:42:ac:16:00:05",
                "IPv4Address": "172.22.0.5/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

rmartrenado
  • 1,516
  • 1
  • 18
  • 42

2 Answers2

0

Your containers are not on the same network so they don't see each other. First of all you should put them all on the same network nginx-proxy_proxy. I am guessing you using this because docker-compose creates it. You can create your own with docker network create some-name and use this one. This way you are not depending on the way docker-compose names the network (which is also dependent on the parent folder so if that change you are in trouble).

Once you do that, you should change server 172.19.0.5:3001; into server wp:3001. Do not use IP addresses since those one change. You also only need the expose: 3001 declaration if it is not already declared in the Dockerfile of your image.

Another note on the first docker-compose: you don't need to attach the container to the bridge network to make them accessible - you already do that with the port mapping.

Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Thanks for your answer. During my trial/error process, if I don't expose the wordpress instance port, nginx-proxy won't see it. The default.conf that I posted, is automatically created because this container is made to watch for new Wordpress instances and change config and reload. – rmartrenado Jun 19 '19 at 18:01
  • So, supposedly, I shouldn't have to touch any of the configuration files that live in nginx-proxy container. About the network, as far as I can tell, they are sharing the network OK, because trying sometimes I could read in that same file server 172.19.0.5 down; – rmartrenado Jun 19 '19 at 18:03
  • "The containers being proxied must expose the port to be proxied, either by using the EXPOSE directive in their Dockerfile or by using the --expose flag to docker run or docker create." – rmartrenado Jun 19 '19 at 18:09
  • That IP address will change at some point, I can guarantee it. But if you are sure the IP is always updated then go with it. Then the problem should only be the different network. – Mihai Jun 19 '19 at 18:11
  • 1
    In that case your `expose` makes sense. It's just that normally that is a configuration that goes into the Dockerfile so that you are also use it without docker-compose. But if you don't want to use your own Dockerfile then I guess it's fine – Mihai Jun 19 '19 at 18:13
  • I tried (again) to share an external network (after creating it) and now I'm at the same point. However, same error... – rmartrenado Jun 19 '19 at 18:44
  • can you update the post with the corrected files? You can try to ping/curl that IP address from inside the nginx container. I assume you will not get a response so that would mean that the containers are not on the same network. If you however get a response, then you really need to change the NGINX configuration: I think "server" and "test.local" names are a bit mixed up. – Mihai Jun 19 '19 at 18:53
  • I just did. Same result on /etc/nginx/conf.d/default.conf. – rmartrenado Jun 19 '19 at 19:03
  • Tried curl 172.19.0.5:3001 and I'm getting curl: (7) Failed to connect to 172.22.0.5 port 3001: Connection refused – rmartrenado Jun 19 '19 at 19:04
  • 1
    You curl 19 and get no response from 22? – Mihai Jun 19 '19 at 20:46
  • Sorry for the mistake. The IP was OK. I tried curl 172.22.0.5 and got that response. I'll update my question with further details about my network. – rmartrenado Jun 20 '19 at 14:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195287/discussion-between-rmartrenado-and-mihai). – rmartrenado Jun 20 '19 at 15:04
  • How did you fix the issue? – redstone May 23 '20 at 14:29
0

As I'm using php-fpm, I was missing this environment variable for my wp service:

VIRTUAL_PROTO: fastcgi

After that, I could manage to provide some special configuration that is needed for this kind of php version.

Something like this...

https://github.com/dhilditch/wpintense-rocket-stack-ubuntu18-wordpress/blob/master/nginx/sites-available/rocketstack.conf

However, I'm still going through issues as the website I reach is like a fresh one (when I'm providing an existing one using a volume, which I can see correctly inside the container), and all the static files are not going through.

rmartrenado
  • 1,516
  • 1
  • 18
  • 42