gpt4 book ai didi

reactjs - 将docker-compose环境变量的值转换为服务ip地址

转载 作者:行者123 更新时间:2023-12-02 18:19:56 25 4
gpt4 key购买 nike

我想将环境变量的值(它是同一docker-compose.yml文件中的服务)转换为相应容器的ip,而我无法实现。

docker-compose.yml

version: '3'
services:
server:
restart: always
build:
dockerfile: Dockerfile
context: ./server
ports:
- '3001:3001'
volumes:
- /app/node_modules
- ./server:/app
client:
restart: always
build:
dockerfile: Dockerfile
context: ./client
ports:
- '3000:3000'
volumes:
- /app/node_modules
- ./client:/app
environment:
- REACT_APP_API_HOST=server #I WANT TO TRANSFORM THIS TO THE SERVICE IP
- REACT_APP_API_PORT=3001

当我console.log(process.env.REACT_APP_API_HOST)时,我在控制台中看到“服务器”,我希望它成为服务器的IP地址(这是一个api)

谢谢。

最佳答案

尝试改用服务或容器名称。它们将由docker守护程序解决。

version: '3'
services:
server:
container_name: myserver
...

client:
container_name: myclient
...
environment:
- REACT_APP_API_HOST=myserver
- REACT_APP_API_PORT=3001

关于reactjs - 将docker-compose环境变量的值转换为服务ip地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56068754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com