Nodejs port change in Dockerfile

Viewed 1289

There is a Node js application in the docker container, it works on port 3149, but I need the container to run on port 3000, how can I change the port and register it in the Dockerfile without changing anything in the application code?

dokerfile


COPY package*.json /

ADD id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan bitbucket.org > /root/.ssh/known_hosts && \
    apt update -qqy && \
    apt -qqy install \
    ruby \
    ruby-dev \
    yarn \
    locales \
    autoconf automake gdb git libffi-dev zlib1g-dev libssl-dev \
    build-essential

RUN gem install compass

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en

COPY . .

RUN npm ci && \
    node ./node_modules/gulp/bin/gulp.js client && \
    rm -rf /app/id_rsa \
    rm -rf /root/.ssh/

EXPOSE 3000

CMD [ "node", "server.js" ] ```
2 Answers

To have the container running on port 3000 you have specify this once you run the container using --port or -p options/flags, and note that EXPOSE does not publish the port :

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

so you have to run the container with -p option from the terminal:

docker run -p 3000:3149 ...

when you run the container map port 3000 on the host to port 3149 in the container e.g.

docker run -p 3000:3149 image
Related