1

I have a docker file that runs some integration tests between two containers. It looks a bit like this:

version: '3'
services:
    test_client:
        image: client:latest
        depends_on:
          - test_server
        command: bash -c "run_test.sh -p 80"
    test_server:
        image: server:latest

test_server launches a web server on port 80, but that same port is already exposed within test_client (I don't have control over the part of the client:latest image where the port is exposed. I tried using the port: 8080:80 to forward port 80 to 8080 in the server, but that only changes the port exposed on the host, not within the docker environment.

How can I connect test_client to test_server in such a scenario?

1

You can use a proxy to forward traffic between the two services, I typically do something like this which includes automated SSL with certbot. In the example below I have two services listening on port 80/443 (Sonarqube and Jenkins). You can also see I can reference container hosts by name (https://jenkins:8080)

nginx:
container_name: nginx-certbot
restart: unless-stopped
image: staticfloat/nginx-certbot
networks:
  - local
ports:
  - "0.0.0.0:80:80"
  - "0.0.0.0:443:443"
environment:
  CERTBOT_EMAIL: <insert_email>
volumes:
  - ./conf.d:/etc/nginx/user.conf.d:ro
  - letsencrypt:/etc/letsencrypt

And this is what my nginx config looks like

server {
listen              443 ssl;
server_name jenkins.hauntedmansion.io;
ssl_certificate     /etc/letsencrypt/live/jenkins.hauntedmansion.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jenkins.hauntedmansion.io/privkey.pem;

client_max_body_size 20M;

  location / {
    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-Proto $scheme;
    proxy_pass         http://jenkins:8080;
    proxy_read_timeout 90;
    proxy_redirect     http://127.0.0.1:8080 https://jenkins.hauntedmansion.io;
  }
}

If you want to see the full docker compose example check it out here:

https://gist.github.com/cvega/66e0e5a2815e9b923da2c616f74dea7e

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.