无法使用cURL访问Docker容器。

huangapple go评论82阅读模式
英文:

Cannot curl to the docker container

问题

以下是翻译好的部分:

我构建了Docker镜像

  1. ...
  2. ENV PORT=8080
  3. ENV ADDRESS=0.0.0.0
  4. EXPOSE 8080
  5. CMD ["node", "/usr/app/src"]

我可以通过docker run来运行它

  1. [HttpFastifyServer]: 在地址0.0.0.0:8080上初始化服务器
  2. [HttpFastifyServer]: └── (空根节点)
  3. ├── / (POST)
  4. └── / (POST)
  5. └── * (OPTIONS)
  6. [Process]: 进程已初始化

但我无法使用curl访问它。

  1. $ curl -X POST http://0.0.0.0:8080/
  2. curl: (7) 连接到0.0.0.0端口8080失败:连接被拒绝
  3. $ curl -X POST http://127.0.0.1:8080/
  4. curl: (7) 连接到127.0.0.1端口8080失败:连接被拒绝
  5. $ curl -X POST http://localhost:8080/
  6. curl: (7) 连接到localhost端口8080失败:连接被拒绝

我看不到任何IP地址

  1. $ docker inspect myImage:1.0.0 | grep "IPAddress"
  2. $

有什么想法吗?

英文:

I build the docker image

  1. ...
  2. ENV PORT=8080
  3. ENV ADDRESS=0.0.0.0
  4. EXPOSE 8080
  5. CMD ["node", "/usr/app/src"]

I can run it by docker run

  1. [HttpFastifyServer]: Initializing server on address 0.0.0.0:8080
  2. [HttpFastifyServer]: └── (empty root node)
  3. ├── / (POST)
  4. └── / (POST)
  5. └── * (OPTIONS)
  6. [Process]: Process initialized

But I cannot curl it.

  1. $ curl -X POST http://0.0.0.0:8080/
  2. curl: (7) Failed to connect to 0.0.0.0 port 8080: Connection rejected
  3. $ curl -X POST http://127.0.0.1:8080/
  4. curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection rejected
  5. $ curl -X POST http://localhost:8080/
  6. curl: (7) Failed to connect to localhost port 8080: Connection rejected

I don't see any ip address

  1. $ docker inspect myImage:1.0.0 | grep "IPAddress"
  2. $

Any ideas?

答案1

得分: 1

如果您在容器中暴露了应用程序的端口8080,并且希望从外部通过端口8080访问它,您需要将-p 8080:8080添加到您的docker run命令中。

英文:

If you expose port 8080 of your app in the container and you want to access it from the outside on port 8080 as well, you need to add -p 8080:8080 to your docker run command.

huangapple
  • 本文由 发表于 2023年5月6日 20:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定