简体   繁体   English

如何访问在 docker 容器中运行的 create-react-app?

[英]How to access create-react-app run in a docker container?

I followed the steps under https://mherman.org/blog/dockerizing-a-react-app/我按照https://mherman.org/blog/dockerizing-a-react-app/下的步骤进行操作

My setup:我的设置:

How to reproduce: Follow the steps from the first link:如何重现:按照第一个链接中的步骤操作:

install create-react-app globally:全局安装 create-react-app:

npm install -g create-react-app@3.4.1

Generate new app:生成新应用:

$ npm init react-app sample --use-npm
$ cd sample

Create Dockerfile in the root of directory:在目录根目录下创建 Dockerfile:

# pull official base image
FROM node:13.12.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent

# add app
COPY . ./

# start app
CMD ["npm", "start"]

Add.dockerignore:添加.dockerignore:

node_modules
build
.dockerignore
Dockerfile
Dockerfile.prod

Build and tag the dockerimage:构建并标记 dockerimage:

$ docker build -t sample:dev .

Spin up the container:启动容器:

$ docker run \
    -it \
    --rm \
    -v ${PWD}:/app \
    -v /app/node_modules \
    -p 3001:3000 \
    -e CHOKIDAR_USEPOLLING=true \
    sample:dev

This is what I see in the Docker Quickstart Terminal:这是我在 Docker 快速入门终端中看到的: 在此处输入图像描述

And this is my project structure:这是我的项目结构: 在此处输入图像描述

However, when I go to localhost:3001 as described in the post, I see但是,当我 go 到 localhost:3001 如帖子中所述时,我看到在此处输入图像描述

Any idea where I'm missing something?知道我在哪里遗漏了什么吗?

When you run the container, specify the port like this: 3000:80 (use:80)当你运行容器时,像这样指定端口: 3000:80 (use:80)

$ docker run -it --rm -p 3000:80 sample:prod

You can then navigate to http://localhost:3000/ to see the CRA app.然后,您可以导航到http://localhost:3000/以查看 CRA 应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM