Run nginx in docker with custom vhost

14,495

Incorrect location for nginx.conf

You're using an incorrect location for the configuration file. As described on the readme for the official nginx image, the location for the configuration file is /etc/nginx/nginx.conf, so you should use;

-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro

For the configuration file.

Given that you'd be serving only a single website in your container, you don't even have to specify the domainname in your virtual host (any request that arrives at the nginx container should be for that domain).

Serving multiple domains

In general, I'd recommend using a dedicated container for each domain/website you want to run. To serve multiple websites (thus, multiple containers) on a single docker machine, you can either;

Use port-mapping; each container is published on a different port in the host (-p 8080:80 for website "A", -p 8081:80 for website "B")

Use a proxy container. This is probably a nicer approach; You run your website containers as usual, but don't publish their ports. Those containers are thus not publicly reachable. Instead of publishing the ports directly, you run a proxy-server that forwards traffic to the right container(s), based on the domain-name.

Domainname-based forwarding requires some configuration. However, there's a convenient way to do this fully automatic. jwilder/nginx-proxy is a nginx proxy-server that's designed to run in a Docker container; all you need to do, is run that container, and give it access to the docker API.

Subsequently, start your webserver containers, but set an environment variable on them that specifies which domains they serve (for example -e VIRTUAL_HOST=www.example.com,example.com).

The nginx-proxy container listens for "docker events", and automatically generates the configuration to forward traffic to the right container.

If your DNS (or /etc/hosts) is correctly set up, you can now simply visit http://www.example.com, and view that website.

Share:
14,495
Sergey Terehin
Author by

Sergey Terehin

Updated on June 04, 2022

Comments

  • Sergey Terehin
    Sergey Terehin almost 2 years

    I want to run basic nginx container on OSX with docker machine.

    I run the following code:

    docker pull nginx
    docker run -d -i -t \
        -p 8080:80 \
        -v $(pwd)/vhost.conf:/etc/nginx/sites-enabled/vhost.conf \
        -v $(pwd)/docker.dev:/var/www/docker.dev nginx
    

    In current directory I have vhost.conf file:

    server {
        listen 80;
        index index.html;
        server_name docker.dev;
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log
        root /var/www/docker.dev
    }
    

    and docker.dev folder with index.html file in it.

    Also in my /etc/hosts on my host OSX system I have:

    192.168.99.100 docker.dev
    

    But when I visit http://192.168.99.100:8080 in browser I get default nginx page, not my index.html

    I can achieve the desired result by placing index.html file in /usr/share/nginx/html folder. But I want to figure out how can I adjust my own location for web root.