1

i'm new to docker networking and nginx stuff, but try to "dockerize" everything on a local devserver. for tests a docker image with nginx which should redirect another container (gogs) from port 3000 to a specific url with port 80. And i want to have the reverse proxy configs and the docker images "separated", for each "app" an own docker-compose file.

so i should reach with http://app.test.local the gog installation. BUT: i reach with http://app.test.local only a bad gateway of nginx and with http://app.test.local:3000 i reach the gog installation...

i tried many tutorials, but somehwere there have to be an error, thats slips in every time

so what i did:

$ docker network create testserver-network

created docker-compose for nginx:

version: '3'

services:
  proxy:
    container_name: proxy
    hostname: proxy
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /docker/proxy/config:/etc/nginx
      - /docker/proxy/certs:/etc/ssl/private
    networks:
      - testserver-network
networks:
  testserver-network:

and one for gogs:

version: '3'

services:
  gogs:
    container_name: gogs
    hostname: gogs
    image: gogs/gogs
    ports:
      - 3000:3000
      - "10022:22"
    volumes:
      - /docker/gogs/data:/var/gogs/data
    networks:
      - testserver-network
networks:
  testserver-network:

(mapped directories work)

configured default.conf of nginx:

# upstream gogs {
#   server      0.0.0.0:10880;
# }

server {
  listen        80;
  server_name   app.test.local;

  location / {
    proxy_pass  http://localhost:3000;
  }
}  

and added to hosts file on client app.test.local <server ip>

docker exec proxy nginx -t and docker exec proxy nginx -s reload say everythings fine...

Cybeaer
  • 43
  • 9
  • You should look into using traefik as front service. Using labels for services takes off burden of managing reverse proxy and allows easy configuration for new dockerized services. – DevilaN Nov 16 '21 at 20:34
  • thanks DevilaN, allready looked into traefik but it wont work for, or better say have it's own problems, so i try to keep it basic and low, and more self controlled, even if it means a bit more work. – Cybeaer Nov 17 '21 at 07:51
  • If you develop only one service traefik is probably overkill. But with more than one there is not-so-much-fun with ports and injecting them to dependent services to communicate. If you want to spend 10 minutes with it to try it out you might check out my local-traefik setup on github: https://github.com/DevilaN/local-traefik – DevilaN Nov 17 '21 at 08:30
  • thanks i will give it a try, maybe if the container count grow up or some other project :) – Cybeaer Nov 17 '21 at 21:24

1 Answers1

1

Answer

You should connect both containers to the same docker network and then proxy to http://gogs:3000 instead. You also shouldn't need to expose port 3000 on your localhost unless you want http://app.test.local:3000 to work. I think ideally you should remove that, so http://app.test.local should proxy to your gogs server, and http://app.test.local:3000 should error out.

Explanation

gogs is exposed on port 3000 inside its container, which is then further exposed on port 3000 on your host machine. The nginx container does not have access to port 3000 on your host, so when it tries to proxy to http://localhost:3000 it is proxying to port 3000 inside the nginx container (which is hosting nothing).

After you have joined the containers to the same network, you should be able to reference the gogs container from the nginx container by its hostname (which you've set to gogs). Now nginx will proxy through the docker network. So you should be able to perform the proxy without needing to expose 3000 on your local machine.

kthompso
  • 1,823
  • 6
  • 15
  • changed it to that but now it tells me when i reload nginx config ```host not found in upstream "gogs" in /etc/nginx/conf.d/gogs.conf```could it be that two different docker-compose files "can't" share the same network? – Cybeaer Nov 17 '21 at 07:46
  • it creates it's own network... docker network ls shows gogs_testserver-network the moment when the gogs container starts – Cybeaer Nov 17 '21 at 08:05
  • 1
    thanks with your answer of gogs:3000 and the network config of https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects it works! – Cybeaer Nov 17 '21 at 08:11
  • Ah yea, you're right I did not read carefully enough. Another option is to create a docker network separate from either stack with [docker network create](https://docs.docker.com/engine/reference/commandline/network_create/) and then join both containers to it using `external: true` in both. That's typically what I would do so that the second stack is not so dependent on the details of the first one. Nice work! – kthompso Nov 18 '21 at 16:36