EC2 Docker Compose connection refused

1. The problem I’m having:

I am trying to use caddy as a reverse proxy for a api and frontend usage i have in EC2 and cloudflare, so far i gave up for now on cloudflare just need to get https working for testing. But essencially i have a fastapi in python and caddy and docker composer running. I think seems right now but now i get connection refused not sure why.

2. Error messages and/or full log output:


{"level":"error","ts":1708450821.914592,"logger":"http.log.error","msg":"dial tcp 172.27.0.3:80: connect: connection refused","request":{"remote_ip":"172.27.0.1","remote_port":"41404","client_ip":"172.27.0.1","proto":"HTTP/2.0","method":"GET","host":"chain.e-love.app","uri":"/chain/playground","headers":{"Sec-Ch-Ua-Platform":["\"Linux\""],"Purpose":["prefetch"],"Sec-Fetch-Site":["none"],"Sec-Fetch-Mode":["navigate"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"],"Sec-Purpose":["prefetch;prerender"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Dest":["document"],"Sec-Ch-Ua":["\"Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121\""],"Dnt":["1"],"Sec-Gpc":["1"],"Sec-Fetch-User":["?1"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["pt-BR,pt;q=0.9,ja-JP;q=0.8,ja;q=0.7,en-US;q=0.6,en;q=0.5"],"Sec-Ch-Ua-Mobile":["?0"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"chain.e-love.app"}},"duration":0.004004358,"status":502,"err_id":"4js9h2kcm","err_trace":"reverseproxy.statusError (reverseproxy.go:1267)"}

3. Caddy version:

2.7.6 caddy-cloudflare docker file

4. How I installed and ran Caddy:

Basically dockerfile and dockercompose

b. Command:

make deploy to start docker compose up build

c. Service/unit/compose file:

version: '3.8'
services:
  langchain:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      REDIS_HOST: ${REDIS_HOST}
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    expose:
      - 80
    ports:
      - 8000:80
    depends_on:
      - redis
    networks:
      - my-network
    links:
      - redis  
    restart: always
    extra_hosts:
      - "host.docker.internal:host-gateway"
  caddy:
    container_name: caddy
    restart: unless-stopped
    networks:
      - my-network
    build:
      context: ./caddy
      dockerfile: ./Dockerfile
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./data/caddy_data:/data
      - ./data/caddy_config:/config
    depends_on:
      - langchain
    environment:
      PROXY_BACKEND: langchain
      PROXY_PORT: ${PROXY_PORT}
      DOMAIN: ${DOMAIN}
      CF_API_TOKEN: ${CF_API_TOKEN}

  redis:
    image: redis
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    networks:
      - my-network
    volumes: 
      - ./data/redis:/data
volumes:
  caddy_data:
  caddy_config:
  redis:
    driver: local
networks:
  my-network:
    driver: bridge

d. My complete Caddy config:

{$DOMAIN} {
    debug
	reverse_proxy http://{$PROXY_BACKEND}:${PROXY_PORT}
}

FROM iarekylew00t/caddy-cloudflare:latest

RUN mkdir /app
COPY start.sh /app/start.sh

COPY Caddyfile /etc/caddy/Caddyfile

CMD ["sh", "/app/start.sh"]

5. Links to relevant resources:

My api docker

# Use an official Python runtime as the base image
FROM python:3.12.1

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file to the working directory
COPY requirements.txt .
RUN true
# Install dependencies
RUN pip install -r requirements.txt
RUN true
# Copy requirements
COPY requirements.txt .
RUN true
# Copy app code
COPY app/ .
RUN true

COPY female.txt .
RUN true
# Export necessary port
EXPOSE 8000 

ENV OPENAI_API_KEY=${OPENAI_API_KEY}
ENV REDIS_HOST=${REDIS_HOST}

# Define the command to run on container start
CMD [ "python", "server.py" ]

Are you sure your app is listening on port 80?

Looks like your Dockerfile suggests it would be port 8000.

Since you’re writing your own Dockerfile anyway, I recommend you build yourself with Build from source — Caddy Documentation. That way you don’t depend on some unknown user’s Docker image which may or may not get updated in the future, and might sneakily have some vulnerabilities added if it gets compromised.

I didn’t noticed that but in fact the problem was i haven’t set the env variable for the 8000, ty.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.