1

I need to run multiple WordPress containers linked all to a single MySQL container + Nginx Reverse Proxy to easy handle VIRTUAL_HOSTS.

Here is what I'm trying to do (with only one WP for now):

  • Wordpress (hub.docker.com/_/wordpress/)
  • Mysql (hub.docker.com/_/mysql/)
  • Nginx Reverse Proxy (github.com/jwilder/nginx-proxy)

I'm working on OSX and this is what I run on terminal:

docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
docker run -e VIRTUAL_HOST=wordpress.mylocal.com --name wordpress --link some-mysql:mysql -p 8080:80 -d wordpress

My Docker is running on 192.168.99.100 and that brings me to a 503 nginx/1.9.12 error ofc.

Then 192.168.99.100:8080 brings me to the WordPress as expected.

But http://wordpress.mylocal.com it's not working; it's not redirecting to 192.168.99.100:8080 and I don't understand what I'm doing wrong.

Any suggestions? Thanks!

4
  • Try without exposing wordpress on port 8080 and let nginx route directly to the container rather than go through the host. Commented Apr 2, 2016 at 17:46
  • Tried without exposing 8080 but still same result :( As described here 'github.com/jwilder/nginx-proxy' the containers need to expose a port Commented Apr 2, 2016 at 17:59
  • The containers need to expose a port, but there's no need to publish them (so, you could remove the -p 8080:80 (and -p 3306:3306 if you don't use external tools to connect to MySQL). Do you have a wildcard DNS setup, or edited your hosts-file to make wordpress.mylocal.com resolve as 192.168.99.100? Commented Apr 2, 2016 at 20:29
  • I'm trying to set up the same thing on Linux, but without the automated proxy tool jwilder provides. Have you had any success with this since April, @Kianda? Commented Oct 31, 2016 at 4:37

3 Answers 3

1

First of all I recommend you start using docker-compose , running your containers and finding errors will become much easier.

As for your case it seems that you should be using VIRTUAL_PORT to direct to your container on 8080.

Secondly you cannot have two containers(the nginx-proxy + wordpress) napped to the same port on the host.

Good luck!

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

2 Comments

basically you cannot run any container on the same port as if you have already one running and bounded on the same port.
That's correct. Thats where the proxy helps it can send your request to the correct container based on the url and directs it to the correct container using the VIRTUAL_PORT parameter.
1

One:
Use docker compose.

vi docker-compose.yaml

Two:
paste this into the file:

version: '3'

services:
  nginx-proxy:
    image: budry/jwilder-nginx-proxy-arm:0.6.0
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certs:/etc/nginx/certs:ro
      - confd:/etc/nginx/conf.d
      - vhostd:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
    labels:
      - com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
    environment:
      - DEFAULT_HOST=example2.com
    networks:
      - frontend

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion:stable
    restart: always
    volumes:
      - certs:/etc/nginx/certs:rw
      - confd:/etc/nginx/conf.d
      - vhostd:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
#      - LETSENCRYPT_SINGLE_DOMAIN_CERTS=true
#      - LETSENCRYPT_RESTART_CONTAINER=true
      - [email protected]
    networks:
      - frontend
    depends_on:
      - nginx-proxy
#########################################################
..The rest of the containers go here..
#########################################################
networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

volumes:
  certs:
  html:
  vhostd:
  confd:
  dbdata:
  maildata:
  mailstate:
  maillogs:

Three:
Configure as many docker as you need and configure them to your liking. Here are some examples:

mysql (MariaDB):

  mysql:
    image: jsurf/rpi-mariadb:latest #MARIADB -> 10 #82eec62cce90
    restart: always
    environment:
      MYSQL_DATABASE: nameExample
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: passwordRoot
      MYSQL_ROOT_HOST: '%'
    ports:
      - "3306:3306"
    networks:
      - backend
    command: --init-file /data/application/init.sql
    volumes:
      - /path_where_it_will_be_saved_on_your_machine/init.sql:/data/application/init.sql
      - /physical_route/data:/var/lib/mysql

nginx-php7.4:

  nginx_php:
    image: tobi312/php:7.4-fpm-nginx-alpine-arm
    hostname: example1.com
    restart: always
    expose:
      - "80"
    volumes:
      - /physical_route:/var/www/html:rw
    environment:
      - VIRTUAL_HOST=example1.com
      - LETSENCRYPT_HOST=example1.com
      - [email protected]
      - ENABLE_NGINX_REMOTEIP=1
      - PHP_ERRORS=1
    depends_on:
      - nginx-proxy
      - letsencrypt
      - mysql
    networks:
      - frontend
      - backend

WordPress:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=exampleuser
      - WORDPRESS_DB_PASSWORD=examplepass
      - WORDPRESS_DB_NAME=exampledb
      - VIRTUAL_HOST=example2.com
      - LETSENCRYPT_HOST=example2.com
      - [email protected]
    volumes:
      - wordpress:/var/www/html #This must be added in the volumes label of step 2

You can find many examples and documentation here
You must be careful since in some examples I put images that are for rpi and it is very likely that they will give problems in amd64 and intel32 systems.You should search and select the images that interest you according to your cpu and operating system

Four:
Run this command to launch all dockers

docker-compose up -d --remove-orphans

"--remove-orphans" serves to remove dockers that are no longer in your docker-compose file

Five:
When you have the above steps done you can come and ask what you want, we will be happy to read your dockerFile without dying trying to read a lot of commands

Comments

0

According to your case I think that the best solution for you is to use an nginx reverse proxy that is listening on the docker socket and can pass request to different virtual hosts.

for example, let's say you have 3 WPs.

WP1 -> port binding to 81:80
WP2 -> port binding to 82:80
WP3 -> port binding to 83:80 

for each one of them you should use a docker environment variable with the virtual host name you want to use.

WP1-> foo.bar1
WP2-> foo.bar2
WP3-> foo.bar3

After doing so you should have 3 differnt WP with ports exposed on 81 82 83.

Now download and start this nginx docker container (reverse proxy) here it should listen on the docker socket and retrives all data coming to you machine on port 80. and when you started the WP container and by the environment variable that you provide he will be able to detect which request shouuld get to which WP instance...

This is an example of how you should run one of you WP docker images

> docker run -e VIRTUAL_HOST=foo.bar1.com -p 81:80 -d wordpres:tag

In this case the virtual host will be the virtual host coming from the http request

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.