gpt4 book ai didi

node.js - Dockerfile 中的 Nodejs 端口更改

转载 作者:行者123 更新时间:2023-12-02 18:18:03 24 4
gpt4 key购买 nike

docker 容器中有一个 Node js 应用程序,它在端口 3149 上工作,但我需要容器在端口 3000 上运行,如何更改端口并将其注册到 Dockerfile 而不更改应用程序代码中的任何内容?

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" ] ```

最佳答案

要让容器在端口 3000 上运行,您必须在使用 --port 运行容器后指定此项。或 -p选项/标志,并注意 EXPOSE 不发布端口:

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.



所以你必须用 -p 运行容器终端选项:
docker run -p 3000:3149 ...

关于node.js - Dockerfile 中的 Nodejs 端口更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61831289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com