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/