Skip to content

Instantly share code, notes, and snippets.

@petrofcikmatus
Last active June 16, 2023 08:08
Show Gist options
  • Save petrofcikmatus/2c0ee82ee0853cf83cd9ac51dfd37db9 to your computer and use it in GitHub Desktop.
Save petrofcikmatus/2c0ee82ee0853cf83cd9ac51dfd37db9 to your computer and use it in GitHub Desktop.
Automated nginx proxy for Docker containers

Automated nginx proxy for Docker containers

Run it

1. Start nginx-proxy container

$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

or using docker-compose up -d

# projects/nginx-proxy/docker-compose.yml
version: '3.3'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

2. Start my app container

Then start any containers with an env var VIRTUAL_HOST=subdomain.youdomain.com, so -e VIRTUAL_HOST=foo.bar.com

$ docker run -e VIRTUAL_HOST=foo.bar.com  ...

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.

Example time! Behold at my docker-compose.yml file:

# projects/dictionary/docker-compose.yml
version: '3.3'
services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  mysql:
    image: mysql:5.7
    restart: always
    volumes:
      - ./docker/data:/var/lib/mysql
    ports:
      - 3306:3306 # Expose of port, so I can access database from Phpstorm
    environment:
      MYSQL_ROOT_PASSWORD: root
      VIRTUAL_HOST: mysql.dictionary.lh

  apache:
    build:
      context: .
      dockerfile: docker/apache/Dockerfile
    restart: always
    volumes:
      - .:/var/www
    depends_on:
      - mysql
    environment:
      VIRTUAL_HOST: dictionary.lh,*.dictionary.lh
      VIRTUAL_PORT: 80

  adminer:
    image: adminer
    restart: always
    depends_on:
      - mysql
    environment:
      VIRTUAL_HOST: adminer.dictionary.lh
      VIRTUAL_PORT: 80

Then run docker-compose up -d

arkona:dictionary petrofcikmatus$ docker-compose up -d
Creating dictionary_nginx-proxy_1 ... done
Creating dictionary_mysql_1       ... done
Creating dictionary_adminer_1     ... done
Creating dictionary_apache_1      ... done

Then run docker ps

arkona:dictionary petrofcikmatus$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                    NAMES
8a737bad80fe        adminer               "entrypoint.sh docke…"   About a minute ago   Up About a minute   8080/tcp                 dictionary_adminer_1
c9f19c39a9bd        dictionary_apache     "docker-php-entrypoi…"   About a minute ago   Up About a minute   80/tcp                   dictionary_apache_1
bd4a50acdf14        mysql:5.7             "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   dictionary_mysql_1
5bc06baf90a2        jwilder/nginx-proxy   "/app/docker-entrypo…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp       dictionary_nginx-proxy_1

And of course I added my custom hosts into /etc/hosts file (or use dnsmasq).

# /etc/hosts
127.0.0.1 dictionary.lh www.dictionary.lh adminer.dictionary.lh mysql.dictionary.lh mysql

Interesting features

Default host ✓

Add -e DEFAULT_HOST=foo.bar.com

$ docker run -d -p 80:80 -e DEFAULT_HOST=foo.bar.com -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

IPv6 support ✓

Add -e ENABLE_IPV6=true

$ docker run -d -p 80:80 -e ENABLE_IPV6=true -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

SSL? ✓

Yes, but I didn't try this...

FROM php:7.1-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends git zlib1g-dev libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl gettext zip pdo pdo_mysql \
&& a2enmod rewrite \
&& sed -i 's!/var/www/html!/var/www/web!g' /etc/apache2/sites-available/000-default.conf \
&& mv /var/www/html /var/www/web \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& apt-get install -y --no-install-recommends nodejs
WORKDIR /var/www
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment