我的 docker 容器和 React 应用程序 运行 在哪个端口上? Reacts 的默认 PORT 或来自 Dockerfile 的 EXPOSE?

On what PORT is my docker container with a React app running? Reacts' default PORT or EXPOSE from Dockerfile?

我是 Docker 的新手,所以如果我说的有任何错误,请纠正我。

我创建了一个 React 应用程序并在根存储库中编写了以下 Docker 文件:

# pull official base image
FROM node:latest

# A directory within the virtualized Docker environment
# Becomes more relevant when using Docker Compose later
WORKDIR /usr/src/app
 
# Copies package.json and package-lock.json to Docker environment
COPY package*.json ./
 
# Installs all node packages
RUN npm install
 
# Copies everything over to Docker environment
COPY . .
 
# Uses port which is used by the actual application
EXPOSE 8080
 
# Finally runs the application
CMD [ "npm", "start" ]

我的目标是以某种方式 运行 docker 图像,以便我可以在浏览器中(使用本地主机)打开 React 应用程序。 因为在 Docker 文件中,我将应用程序暴露给端口:8080。我想我可以 运行:

docker run -p 8080:8080 -t <name of the docker image>

但显然应用程序可以通过容器中的 3000 访问,因为当我 运行:

docker run -p 8080:3000 -t <name of the docker image>

我可以通过 localhost:8080 访问它。

当容器中的服务 运行ning 可以通过不同的端口访问时,Docker 文件中的 EXPOSE 端口有什么意义? 将 NodeJS 应用程序容器化时,我是否始终必须确保我的应用程序中的 process.env.PORT 与 Docker 文件中的 EXPOSE 相同?

EXPOSE 用于告诉 docker 应用程序内部的哪些端口 可以 公开。如果您不在内部使用这些端口(容器->主机),则没有任何意义。

EXPOSE在使用docker run -P -t <name of the docker image>-P大写P)时非常方便,让Docker自动将所有暴露的端口发布到主机上的随机端口(试一试。然后 运行 docker psdocker inspect <containerId> 并检查输出)。

因此,如果您的 Web 服务器(React 应用程序)运行 正在端口 3000(在容器内)上,您应该 EXPOSE 3000(而不是 8080)以正确地与 Docker API.

有点奇怪。 它只是某种意义上的文档。

https://docs.docker.com/engine/reference/builder/#:~:text=The%20EXPOSE%20instruction%20informs%20Docker,not%20actually%20publish%20the%20port.

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.

do I always have to make sure that process.env.PORT in my app is the same as the EXPOSE in the Dockerfile? Yes. You should.

然后你还需要确保当你使用 docker 运行 命令或在你的 docker-compose.yml 文件中,或者你计划运行宁 docker.

实际上 react 应用程序 运行 是默认端口 3000。因此您必须在 docker-compose.yml 中提及端口并公开。现在我将 3000 端口更改为 8081

  frontend: 
      container_name: frontend 
      build: 
        context: ./frontend/app
        dockerfile: ../Dockerfile  
      volumes:
        - ./frontend/app:/home/devops/frontend/app 
        - /home/devops/frontend/app/node_modules
      ports:
        - "8081:3000"
      expose:
        - 8081
      command: ["npm", "start"]
      restart: always 
      stdin_open: true 

和运行docker

$ sudo docker-compose up -d

然后检查 运行ning 容器以找到 运行ning 端口

$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
83b970baf16d        devops_frontend     "docker-entrypoint..."   31 seconds ago      Up 30 seconds       8081/tcp, 0.0.0.0:8081->3000/tcp   frontend

已解决。检查您的 public 端口

$ curl 'http://0.0.0.0:8081'