Docker – Running nginx as a proxy for Redmine

I’d like to Dockerize the setup for an nginx to redmine (and other services) reverse proxy, but when forwarding requests from nginx to redmine, I’m seeing a 501 bad gateway request on browsers and a "failed (111 Connection refused)" on the nginx output.

The nginx configuration works with regular nginx server (non-dockerized) forwarding requests to a dockerized redmine server, which leads me to believe I’ve done something screwy with the network. I can also access the the redmine server directly via port 3000.

  • Docker: Layering on top of shared volume
  • Pull Image with Docker on Windows 10 : “failed to register layer …Denied Access”
  • Docker TLS error on Mac
  • How to download file from docker container?
  • Keepalived health check can't connect to 127.0.0.1
  • How to compile from a remote machine ?
  • Here’s a snippet of my setup:

    nginx.conf

    http {
        server {
            listen 80;
    
            # Seeing a 501 Bad Gateway on the browser
            location /redmine {
                proxy_pass http://127.0.0.1:3000;
            }
        }
    }
    

    docker-compose.yaml

    version: '2'
    services:
        nginx:
            image: nginx
            ports:
                - "80:80"
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            networks:
                - shared_internal
        redmine:
            image: redmine
            ports:
                - "3000:3000"
            networks:
                - shared_internal
    networks:
        shared_internal:
    

  • Nodejs dev environnement with docker volumes
  • Commit data in a mysql container
  • Docker Container's PublicPort via Remote API
  • Docker container can reach DNS but not resolve hosts
  • What's the best way to synchronize a python script with a docker database container?
  • Issue connecting elasticsearch cluster (i.e. non dockerized) from elasticsearch client running in docker container
  • 2 Solutions collect form web for “Docker – Running nginx as a proxy for Redmine”

    Note: I haven’t looked at Bukharov Sergey’s answer yet, so that may be more elegant.


    Here’s what I’ve come up with. I’ve found 2 methods for achieving the network sharing capability and 1 hack for a Redmine issue that appears when using said methods.

    Method 1 (Preferred because it’s shorter, but maybe not because it’s deprecated?): Container Linking

    docker-compose.yaml

    version: '2'
    services:
        nginx:
            image: nginx
            ports:
                - "80:80"
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            # Method 1: Linking
            links:
                - redmine
        redmine:
            image: redmine
            # Method 1: Exposing port to linked containers
            expose:
                - "3000"
    

    nginx.conf

    http {
        server {
            listen 80;
    
    # Method 1: Access via alias from link
            location /redmine/ {
                proxy_pass http://redmine:3000/;
            }
    }
    

    Method 2: Defining a Network

    docker-compose.yaml

    version: '2'
    services:
        nginx:
            image: nginx
            ports:
                - "80:80"
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            # Method 2
            networks:
                shared_net:
                    ipv4_address: 172.22.0.4
        redmine:
            image: redmine
            # Method 2
            networks:
                shared_net:
                    ipv4_address: 172.22.0.5
    # Method number 2: Via networks
    networks:
        shared_net:
            driver: bridge
            ipam:
                config:
                    - subnet: 172.22.0.0/16
                      gateway: 172.22.0.1
    

    nginx.conf

    http {
        server {
            listen 80;
    
    # Method 2: Access via ip address in shared network
            location /redmine_networked/ {
                proxy_pass http://172.22.0.5:3000/;
            }
        }
    }
    

    Redmine Hack: Accessing redmine via a suburl

    The above solutions allow for access to Redmine’s home page. All the Redmine URLs, though, will point to root (e.g. ‘/’ for home, instead of ‘/redmine’ or ‘/redmine_networked’). So none of those links will work. If nginx was setup to redirect all ‘/’ urls to Redmine, this would be a non-issue. The following hack assumes that is not the case.

    To get Redmine to point to the URLs configured, one will need to edit the config/environment.rb file.

    Here’s the hack:

     > docker exec -it <redmine_container> bash
     redmine> cd config
    # We're going to install vim (this is a hack)
     redmine> apt-get update 
     redmine> apt-get install vim
     redmine> vim environment.rb
    

    Change the following lines at the bottom of your config/environment.rb

    Initialize the Rails application Rails.application.initialize!
    

    to

    RedmineApp::Application.routes.default_scope = "/redmine" 
    Initialize the Rails application Rails.application.initialize!
    
    redmine> exit
    > docker restart <redmine> (or just kill other docker process and run docker up again)
    

    You can use nginx proxy container
    nginx-proxy sets up a container running nginx and docker-gen. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.

    it is more convenient then manual configuration nginx

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.