繁体   English   中英

我正在尝试对我制作的一个简单项目进行 dockerize 处理,但遇到了一些问题

[英]im trying to dockerize a simple project i made and had some issues

https://github.com/itay1989/PP.git

以上是针对我的问题的 git 存储库。 我尝试对我制作的现有项目进行 dockerize 处理,但在服务器(api)连接到数据库(postgres)时遇到了一些问题。 在 repo 中,我 cli 服务器容器获取 ip 并尝试将 ./src/components... 文件中的所有按钮路由到服务器,但它仍然没有 function。

这就是我得到的警告。 客户端仍然有效,但我没有任何功能。 任何帮助,将不胜感激。

client_1    | Compiled with warnings.
client_1    |
client_1    | ./src/components/InputTodo.js
client_1    |   Line 10:13:  'response' is assigned a value but never used  no-unused-vars
client_1    |
client_1    | ./src/components/ListTodos.js
client_1    |   Line 12:13:  'deleteTodo' is assigned a value but never used  no-unused-vars
client_1    |
client_1    | ./src/components/EditTodo.js
client_1    |   Line 12:13:  'response' is assigned a value but never used  no-unused-vars
client_1    |
client_1    | Search for the keywords to learn more about each warning.
client_1    | To ignore, add // eslint-disable-next-line to the line before.

您应该使用不同的方法从前端访问后端。 例如,在https://github.com/itay1989/PP/blob/master/client/src/components/EditTodo.js#L13

      const response = await fetch(
        `http://172.22.0.4:5000/todos/${todo.todo_id}`, // <- BAD!
        {
          method: "PUT",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(body)
        }
      );

这是错误的,因为容器 IP 可能会从一次执行更改为另一次执行。 它肯定会从一种环境改变到另一种环境。

解决方案

CRA 允许在您的代码中使用环境变量

      const response = await fetch(
        `${process.env.REACT_APP_BACK_URL}/todos/${todo.todo_id}`, // <- GOOD!
        {
          method: "PUT",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(body)
        }
      );

然后,将其添加到您的docker-compose.yml

  client:
    # ...
    environment:
      - CHOKIDAR_USEPOLLING=true 
      - REACT_APP_BACK_URL=http://api:5000

在这里,我使用api服务中定义的名称,该名称将在所有容器的/etc/hosts中定义。

笔记:

在没有完整日志和测试环境的情况下分析完整 repo 的错误,迫使我们将其安装在我们的机器上。 在接下来的问题中,请在询问之前考虑进行更多调试,并将其减少到包含错误的最窄范围。

在这里,我只是稍微查看一下代码并发现一个错误(我没有安装它)。 可能有更多的错误。

暂无
暂无

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

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