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

标签 node.js docker dockerfile

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/

相关文章:

node.js - 如何在 NodeJS 中模拟嵌套依赖关系

javascript - winston 超过 maxsize 后我的日志文件不旋转

ruby-on-rails - 127.0.0.1:11211已关闭party_manager | DalliError:没有可用的服务器

docker - 如何将 Capistrano 与 Docker 集成进行部署?

docker 拉 - 标准错误 : layers from manifest don't match image configuration

node.js - 将音频文件从 Google Drive 流式传输到 Discord 语音 channel

javascript - 如何像google Drive一样在网页中查看office文件

Dockerfile - 中间容器到底在做什么?

java - Docker 容器与 2 个应用程序交互

docker - 错误:服务 'api-gateway'无法建立:添加失败:stat/var/lib/docker/tmp/docker-builder931060141/api-gateway:无此类文件或目录