运行 Docker 容器中的 React 应用未 运行 正确
Running React app in Docker container not running correctly
我有一个小型 React 应用程序,我想将其 运行 放在 Docker 容器中。我有以下 Docker 文件这样填写:
# base image
FROM node:9.6.1
# set working directory
RUN mkdir /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package-lock.json /app/package-lock.json
COPY package.json /app/package.json
COPY . /app
RUN npm install
# start app
CMD ["npm", "start"]
完成构建后,我能够正确地(我认为)创建图像。
>docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
notes latest e4ccada07b84 18 hours ago 846MB
但是,当我在图像上 运行 时,它会 运行 npm start
,但不在我指定的端口中,如果我转到 URL 它提供它不能连接到任何东西。
>docker run -p 3030:3030 notes:latest
You can now view notes in the browser.
Local: http://localhost:3000/
On Your Network: http://172.17.0.4:3000/
Note that the development build is not optimized.
To create a production build, use npm run build.
我缺少什么才能正确地将此 运行ning 放入容器中?
你的 React 应用程序在容器 和 的端口 3000 上设置为 运行,因为你在输出中提到你要去 URL;我假设您正在尝试访问主机的端口 3000。
将您的命令更改为以下内容;这会将您主机的端口 3000 连接到容器的端口 3000。
docker container run -p 3000:3000 notes:latest
然后前往http://localhost:3000/ - unless you're using Docker Toolbox, in that case you'll want to use http://192.168.99.100:3000/
我还建议在您的 Dockerfile
中添加一个 EXPOSE 3000
,因为这是很好的做法。
请注意 - -p
标志使用 <host>:<container>
作为格式。
我有一个小型 React 应用程序,我想将其 运行 放在 Docker 容器中。我有以下 Docker 文件这样填写:
# base image
FROM node:9.6.1
# set working directory
RUN mkdir /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package-lock.json /app/package-lock.json
COPY package.json /app/package.json
COPY . /app
RUN npm install
# start app
CMD ["npm", "start"]
完成构建后,我能够正确地(我认为)创建图像。
>docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
notes latest e4ccada07b84 18 hours ago 846MB
但是,当我在图像上 运行 时,它会 运行 npm start
,但不在我指定的端口中,如果我转到 URL 它提供它不能连接到任何东西。
>docker run -p 3030:3030 notes:latest
You can now view notes in the browser.
Local: http://localhost:3000/
On Your Network: http://172.17.0.4:3000/
Note that the development build is not optimized.
To create a production build, use npm run build.
我缺少什么才能正确地将此 运行ning 放入容器中?
你的 React 应用程序在容器 和 的端口 3000 上设置为 运行,因为你在输出中提到你要去 URL;我假设您正在尝试访问主机的端口 3000。
将您的命令更改为以下内容;这会将您主机的端口 3000 连接到容器的端口 3000。
docker container run -p 3000:3000 notes:latest
然后前往http://localhost:3000/ - unless you're using Docker Toolbox, in that case you'll want to use http://192.168.99.100:3000/
我还建议在您的 Dockerfile
中添加一个 EXPOSE 3000
,因为这是很好的做法。
请注意 - -p
标志使用 <host>:<container>
作为格式。