I have dockerize my server app with nginx and ssl configuration. Below are what I am doing
I use github actions to run my workflow: I have my Dockerfile
and docker-compose.yml
setup below
FROM node:16-alpine AS builder
WORKDIR /src/viuhealth-api
COPY package*.json ./
RUN npm ci
COPY . .
FROM node:16-alpine
WORKDIR /src/viuhealth-api
COPY --from=builder /src/viuhealth-api .
EXPOSE 3001
CMD ["npm", "start"]
compose file below
version: '3.8'
services:
app:
image: ${ECR_IMAGE_URI}
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
So during my workflow, I build the image and push it to ECR(Elastic Container Registry) with the following commands
docker-compose build
docker-compose push
I then ssh into my EC2, pull the image, create a template of a new docker-compose.yml that is going to reference the image I just pulled and setup my nginx configurations using jwilder/nginx-proxy
# Create a placeholder docker-compose.yml if it doesn't exist
echo "docker-compose.yml not found. Creating a placeholder file..."
cat <<EOF > docker-compose.yml
version: '3.8'
services:
# Nginx reverse proxy service
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80" # Expose HTTP for Let's Encrypt verification
- "443:443" # Expose HTTPS for SSL
volumes:
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- dhparam:/etc/nginx/dhparam
- certs:/etc/nginx/certs:ro # SSL certificates volume
- /usr/share/nginx/html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro # Allow proxy to discover app container
networks:
- proxy
restart: always
# Let's Encrypt companion to manage SSL certs
letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginx-proxy-le
volumes_from:
- nginx-proxy
volumes:
- certs:/etc/nginx/certs:rw # Write access to certificates
- /var/run/docker.sock:/var/run/docker.sock:ro # Needed for Let's Encrypt
restart: always
# Your app container
app:
image: *******.dkr.ecr.****.amazonaws.com/*****:latest
container_name: viuhealth-api
environment:
- VIRTUAL_HOST=****.com # Replace with your actual domain
- VIRTUAL_PORT=3001 # Port where your app listens
- VIRTUAL_PROTO=https # Use HTTPS
- LETSENCRYPT_HOST=****.com # Domain for Let's Encrypt SSL
- LETSENCRYPT_EMAIL=<my-email-here> # Your email for SSL notifications
env_file: .env
networks:
- proxy
restart: always
volumes:
conf:
vhost:
dhparam:
certs:
networks:
proxy:
external:
name: nginx-proxy
EOF
# Pull latest images and recreate containers
sudo docker-compose pull
sudo docker network create nginx-proxy
docker-compose up -d --force-recreate
my workflow works and all the containers runs on my ec2 but whenever I try to access any andpoint, it gives me 502 Bad Request
Please can anyone help me.
1