【问题标题】:deploying docker, nginx and react部署 docker、nginx 和 react
【发布时间】:2019-10-28 13:43:16
【问题描述】:

我用 React 完成了申请。 我想在 docker 容器上运行它并将其呈现给用户。 我输入http://localhost:8000时可以访问react,但是输入http://localhost时无法访问react,即如何访问默认的react应用我打字 http://localhost

我希望默认的 React 应用程序能够查看用户何时键入 http://localhost 你能帮我解决这个问题吗?

Dockerfile

# Stage 1
FROM node:8 as react-build
WORKDIR /app
COPY . ./
RUN yarn
RUN yarn build

# Stage 2 - the production environment
FROM nginx:alpine
COPY nginx-custom.conf /etc/nginx/conf.d/default.conf
COPY --from=react-build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx-custom.conf

server {
  listen       80;
  server_name  localhost;
  #charset koi8-r;
  #access_log  /var/log/nginx/host.access.log  main;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri /index.html;
  }

  #error_page  404              /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #    root           html;
  #    fastcgi_pass   127.0.0.1:9000;
  #    fastcgi_index  index.php;
  #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  #    include        fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
}

【问题讨论】:

  • 请问您使用的是什么资源?我正在努力学习这一点。

标签: reactjs docker nginx containers


【解决方案1】:

我尝试了一个与您的完全匹配的配置。我已经使用了您提供的两个 docker 文件,并且还使用了一个 react 应用程序。运行命令时发布端口 80 对我有用。这个命令:

docker container run -p 80:80 nginx-react:latest

在这种情况下,在构建 nginx 映像时,我将其命名为 nginx-react 并标记为 latest。

如果该命令对您不起作用。您能否分享您正在使用的机器的已知主机列表?如果您不确定在哪里可以找到您机器上的主机列表,请查看this 链接。

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2020-08-17
    • 2023-02-08
    • 1970-01-01
    • 2020-07-23
    • 2020-01-21
    • 1970-01-01
    • 2017-07-14
    • 2019-11-19
    相关资源
    最近更新 更多