Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose nginx docker image to port other than 80?

I'm having the .Dockerfile (from the source):

# build stage
FROM node:9.11.1-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Where at the end the application is exposed to port 80. I'm then having another different .Dockerfile and for building both of them I'm using the following docker-compose.yml file:

version: "3"
services:
  service-name-one:
    image: dockerImageFromAbove
    ports:
      - "8080:80"
  service-name-two:
    image: someOtherImage
    ports:
      - "3000:3001"

And this is the example that is actually working. But I would need to change the port from nginx docker image and instead of port 80 I would need to have port 8081. By simple changing this in both files from above, it's not working and in my research I found that the only working example is when exposing to port 80 from nginx. I tried replacing the line

EXPOSE 8081

with

RUN -P 80:8081
EXPOSE 8081

but seems as -P flag is not supported here. So how can I do such a mapping, before exposing nginx to port 80?

I found this post, but I can't figure out how to use the answers in my docker files.

I also found this post (part for environment variables), but also not sure how to integrate it with my docker-compose file.

like image 385
delux Avatar asked Dec 03 '25 10:12

delux


1 Answers

The second file is not a Dockerfile but a docker-compose.yml, you have to change in the docker-compose.yml the ports and it will be ok. The option -p "hostport:containerport" expose the port when you use command docker run. Anyway i suggest you to use the supported and official image before change too much the Image in the dockerfile.

Anyway if you really need 8081 try something like this

version: "3"
services:
  service-name-one:
    image: yournginxOrSomethingelse
    ports:
      - "8080:80"
      - "8085:8081"
like image 119
Mirco Avatar answered Dec 05 '25 23:12

Mirco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!