How to use Nginx in a Dockerfile?

This is what my current Dockerfile looks:

FROM golang:1.20.5-buster AS build-stage

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o scraper

# Build the test binary
#RUN #CGO_ENABLED=0 go test -c -o dockergo.test

#Run the tests in the container
#FROM docker.io/chromedp/headless-shell:latest AS run-test-stage

#WORKDIR /app
#Copy other files that is needed to run the test (testdata?).
#COPY . .
#COPY --from=build-stage /app/scraper ./scraper
#RUN /app/scraper -test.v


# Proxy the server
FROM nginx:latest as proxy-stage
COPY nginx.conf default.conf
#COPY index.html /usr/share/nginx/html

# Deploy the application binary into a lean image
FROM docker.io/chromedp/headless-shell:latest AS build-release-stage

COPY --from=build-stage /app/scraper /scraper

EXPOSE 8080

ENTRYPOINT ["/scraper"]
CMD ["nginx", "-g", "daemon off;"]

This is what my current default looks for nginx.

http {
    resolver 8.8.8.8;
    server {
        listen 6767;
        location / {
            proxy_pass http://$http_host$request_uri;
            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;
        }
        location /health {
            return 200 '{"status":"UP"}';
            add_header Content-Type application/json;
        }
    }
}

These are the commands that I am running to build and run:
docker build --tag scraper .
docker run --name test -d -p 8080:8080 -p 6767:6767 --rm scraper

My issue is I try to run: curl http://localhost:6767/health, but I am getting curl: (56) Recv failure: Connection reset by peer. When I enter http://localhost:6767/ in the terminal, I get The connection was reset.

This is my first time using Nginx. Is there anything wrong with my Dockerfile, default.conf, or commands? What is the best way to debug this issue?

What are you trying to achieve?

Why have multiple FROM in Dockerfile? You don’t COPY anything from proxy-stage to final stage, I don’t think nginx is in the final image.

And you use ENTRYPOINT and CMD, they usually work together, you can not simply use it to run two separate processes.

In general containers are for isolation, I usually wouldn’t try to place everything in a single one.

Thank you for the help.
I am trying to run a web scraper (which is what the docker.io/chromedp/headless-shell:latest is for),and mask my IP through nginx. How would I use multiple images without using multiple FROM? What would I need to copy from the proxy-stage? Would I need to set up a WORKDIR and copy that? What is the difference between ENTRYPOINT and CMD if they do the same thing? What would you recommend I do instead of placing everything in a single one?

There are two options: I take half an hour to explain every detail to your many questions or you take an hour to search an read the documentation and some tutorials.

I prefer the second option :sweat_smile: Make some effort, enough learning material has already been created.

Here in the community forums we are happy to help when you run in problems, but we are not free private tutors.

1 Like

That’s fair.

I tried making a docker-compose file, and I am running into one particular problem. Could I ask for some help on that?

That is what this forum is for. Just make sure you stay on topic and choose as specific topic titles as you can in the right category. So open a new topic for a new question

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