helloMundo
helloMundo

Reputation: 87

Nginx, Wordpress, and SQL

I currently am trying to learn how to use docker and was wondering if there is a way to make a Docker stack that includes Wordpress, SQL, and Nginx.

Right now I want to have 3 containers running, 1 for each and use nginx as a reverse proxy for my wordpress app.

However, every time I attempt to get this stack up and running through a composer file, only Wordpress and SQL get linked, but not the Nginx.

version: '2'
services:
  db:
    image: mysql:5.7
    volumes:
      - "./.data/db:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:fpm
    links:
      - db
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress
  nginx:
    restart: always
    image: nginx
    ports:
      - "80:80"

This is all I have in my docker-compose.yml

Upvotes: 0

Views: 664

Answers (2)

opHasNoName
opHasNoName

Reputation: 20736

You could go with the jwilder-nginx docker image. It is using docker-gen to detect containers, and will register them in nginx.conf.

This should work, if you add "VIRTUAL_HOST" the domain will be added to nginx.conf. Please note: You don't have to expose ports on WordPress with this Setup. jwilder-nginx will use default port to forward traffic.

version: '2'
services:
  db:
    image: mysql:5.7
    volumes:
      - "./.data/db:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:fpm
    links:
      - db
      - nginx
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress
      VIRTUAL_HOST: myblog.mydomain.de
  nginx:
    restart: always
    image: jwilder/nginx-proxy
    ports:
      - "80:80"

Upvotes: 1

ldg
ldg

Reputation: 9402

Your basic approach should work. I have a feeling there is a configuration issue somewhere, possibly with nginx that is preventing it from working as you intend.

You can try this similar docker-compose.yml file as a sample to see how it may differ from what you are doing:

docker-compose.yml

version: '2'
services:
  php:
     image: phpmyadmin/phpmyadmin
     links:
       - mysql:db
     depends_on:
       - mysql

  mysql:
    image: k0st/alpine-mariadb
    volumes:
      - ./data/mysql:/var/lib/mysql
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=mypass 

  nginx:
    image: nginx:stable-alpine
    ports:
      - "81:80"
    volumes:
      - ./nginx/log:/var/log/nginx
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/files:/var/www/nginx:ro
    depends_on:
      - php

nginx/nginx.conf

worker_processes  1;
events {
  worker_connections  1024;
}
http {
  sendfile  off;
  server {
    listen 80;

    location / {
      proxy_pass  http://php;
      proxy_set_header Host $host;
      proxy_redirect     off;
    }
  }
}

The nginx config is simplified but that should work for testing -- basically all it's doing is proxying the php app. Maps to port 81 to avoid conflicts on the host. (Note this is just a rough demo, would need to be fleshed out for any use more than that.)

Regarding linking, you can see that if you run: docker-compose exec mysql ping -c2 nginx to ping from the mysql container to the nginx container, you will succeed even though there are no links specified between these containers. Docker Compose will maintain those links in the default network for you.

If you like, you can fetch a working version from this repo here and run docker-compose up, and (assuming you don't have anything running on port 81) see results on http://localhost:81/ (or whatever your corresponding hostname/IP is).

For more info on Docker Compose networking see: https://docs.docker.com/compose/networking/

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.

Upvotes: 2

Related Questions