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

version: '3'
        image: client:latest
          - test_server
        command: bash -c "run_test.sh -p 80"
        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?


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)

container_name: nginx-certbot
restart: unless-stopped
image: staticfloat/nginx-certbot
  - local
  - ""
  - ""
  CERTBOT_EMAIL: <insert_email>
  - ./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 https://jenkins.hauntedmansion.io;

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


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.