0xSHA1001
0xSHA1001

Reputation: 69

Nginx is not working with docker-compose for react app

I have created nginx config, DockerFile, Docker-compose file for the same.

nginx/nginx.conf

 server {
  listen 80;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
}

DockerFile

FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --silent
RUN npm install [email protected] -g --silent
COPY . ./
RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

version: "3.7"
services:
  client:
    container_name: client
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3001:3000

Now after doing docker-compose up --build

I get the logs as

client    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
client    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
client    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
client    | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
client    | 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packages version
client    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
client    | /docker-entrypoint.sh: Configuration complete; ready for start up

I am not sure, if the problem is due to different packages version or something else but when tried to visit the url it says this site can’t be reached.

Upvotes: 2

Views: 2148

Answers (2)

user21364935
user21364935

Reputation:

Dockerfile

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf

should be

COPY nginx/nginx.conf /etc/nginx/nginx.conf

nginx.conf

    listen 80;

should be

    listen 80;
    server_name localhost;

Upvotes: 0

allan
allan

Reputation: 969

You didn't specify which url you are using, but it looks like your nginx is exposed on port 80. You need to expose that port in docker compose. Include under the ports:

- 80:80

Upvotes: 1

Related Questions