-1

I created the docker image of node js 12 application using the following docker file. How can I run my image with the port I specified? How can I do port mapping? I want to use the port I mentioned above.

Docker file as given below:

FROM node:12

WORKDIR /app

COPY ./package*.json ./
RUN npm ci --production

COPY ./ ./

ENV PORT 5000
ENV HOST_URL localhost:$PORT
EXPOSE $PORT

CMD [ "npm", "run", "start" ]

I used these port and host in my node js 12 application:

export PORT=5000
export HOST_URL=https://hellosigntest.xxx

I built a image given below:

docker build -t hellosigndemo:1.0 .
meren
  • 432
  • 5
  • 19
  • Does this answer your question? [How to assign as static port to a container?](https://stackoverflow.com/questions/16958729/how-to-assign-as-static-port-to-a-container) – Arnaud Claudel Apr 09 '20 at 11:59

1 Answers1

0

When you run the image, you can specify the host ports on which it will be accessible, using either the docker run -p option or a Docker Compose ports: setting. The host port doesn't need to match the container port. Each container has its own network namespace, so multiple containers can all serve the same port without conflicting.

That means the typical setup here is to pick a fixed port number in your Dockerfile:

# The default port for Express applications
EXPOSE 3000
CMD ["npm", "run", "start"]

But when you run the container, you can pick a different port on the host:

# Map host port 5000 to container port 3000
docker run -p 5000:3000 hellosigndemo:1.0
David Maze
  • 130,717
  • 29
  • 175
  • 215