Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Apache + Nginx Reverse Proxy Example in Docker Compose
  1. download these two files to the same directory
  2. docker-compose up
  3. http://localhost should show you "it works" which is apache being reverse-proxied through nginx
  4. docker sets up DNS for web based on compose service name so the nginx front-end can find http://web
  5. proxy is set to listen on public port 80 on host so it'll receive incoming traffic, then push to httpd
version: '3'
services:
proxy:
image: nginx:1.11 # this will use the latest version of 1.11.x
ports:
- '80:80' # expose 80 on host and sent to 80 in container
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
web:
image: httpd # this will use httpd:latest
server {
listen 80;
location / {
proxy_pass http://web;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
@pankajred

This comment has been minimized.

Copy link

commented Sep 13, 2017

sir i want to use nginx as a load balancer and proxy server . so can i do this tasks with docker-compose..???
i'm just asking this because after running container with docker-compose they don't get and ip.!!

@pankajred

This comment has been minimized.

Copy link

commented Sep 13, 2017

and sir how can combine docker-swarm with docker-compose

@sarashmishra

This comment has been minimized.

Copy link

commented Jun 7, 2018

Hi Bret, I am getting the below error:
ERROR: for proxy Cannot start service proxy: OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused "rootfs_linux.go:58: mounting \"/root/tmp/proxy/nginx.conf\" to rootfs \"/var/lib/docker/aufs/mnt/f6e22c98d09f14d4ed858153e1672d2de5b5e6ea09d46a092bb971cef41a6308\" at \"/var/lib/docker/aufs/mnt/f6e22c98d09f14d4ed858153e1672d2de5b5e6ea09d46a092bb971cef41a6308/etc/nginx/conf.d/default.conf\" caused \"not a directory\""": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

@jchowdhary

This comment has been minimized.

Copy link

commented Sep 13, 2018

@sarashmishra

Hi Bret, I am getting the below error:
ERROR: for proxy Cannot start service proxy: OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused "rootfs_linux.go:58: mounting "/root/tmp/proxy/nginx.conf" to rootfs "/var/lib/docker/aufs/mnt/f6e22c98d09f14d4ed858153e1672d2de5b5e6ea09d46a092bb971cef41a6308" at "/var/lib/docker/aufs/mnt/f6e22c98d09f14d4ed858153e1672d2de5b5e6ea09d46a092bb971cef41a6308/etc/nginx/conf.d/default.conf" caused "not a directory""": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

This is because , maybe you are using a docker toolbox in windows. The following docker-compose.yml file contains the volumes.. which will only work with docker toolbox in windows ...only if you put the docker-compose.yml and nginx.conf inside the C:/Users// directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.