英文:
Why docker container does not respond to my requests on loalhost (my laptop)?
问题
我已经将给定链接https://github.com/ashahbazi65/go-docker.git中的一个简单的golang项目进行了Docker化。然而,在我的笔记本电脑上,请求无法从应用程序获取任何响应。以下是curl的响应。有人可以帮助我理解原因吗?
curl -v localhost:3000
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
英文:
I have dockerized a simple golang project given in the link https://github.com/ashahbazi65/go-docker.git.
However, on my laptop the requests can't get any response from the app. Below is the curl response. Can anybody help me understand why?
curl -v localhost:3000
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
答案1
得分: 3
根据你的 GitHub 仓库,你的应用程序不在 3000 端口上运行,而是在 8081 端口上运行。
因此,当使用 go run main.go
命令时,你的应用程序将在 8081 端口上运行。
为什么在使用 Docker 时它会在 3000 端口上运行呢?这是因为你在 Docker 中暴露了 3000 端口,并使用 docker-compose yaml 文件将这些请求转发到容器内的 8081 端口,从而到达你的应用程序。
英文:
Based on your GitHub repo, your application doesn't run on port 3000 but runs on port 8081.
So when using go run main.go, your app will run on port 8081.
Why does it run on port 3000 when using Docker? It's because you expose port 3000 and then forward those requests to port 8081 inside the container using the docker-compose yaml file, which will reach your application.
答案2
得分: -1
尽管@not1insight提供的答案在技术上是正确的,但问题是“...请求无法从应用程序获得任何响应。下面是curl的响应。有人可以帮我理解为什么吗?”
根据您的存储库,有主要3种运行应用程序的方式。
-
假设您使用'go run main.go'运行它,那么应用程序将在端口8081上公开,如您的go代码中的http.ListenAndServe(":8081", router)所述。
-
假设您使用docker-compose使用"docker-compose up"运行它,那么您的应用程序将在您的笔记本电脑上的端口3000上公开,因为您在docker-compose文件中将Docker容器(您的应用程序)的端口8081主动映射到您的笔记本电脑(主机)的端口3000。
-
还有另一种选项,可以使用Dockerfile直接使用docker命令运行应用程序。通过这种方式,您可以在docker run CLI命令中指定应用程序公开的端口号。参考-https://docs.docker.com/engine/reference/commandline/run/#publish
根据您运行应用程序的方式,检查相应的端口。
正如您提到的,您正在将应用程序容器化,这意味着添加一个Dockerfile以便在容器中运行它。
Docker run、docker-compose、Kubernetes等都是用于运行容器化应用程序的不同运行时。
您的端口规则取决于该运行时配置映射到您代码的端口(在本例中为8081)。
英文:
Although the answer provided by @not1insight is technically correct - The question asks "... requests can't get any response from the app. Below is the curl response. Can anybody help me understand why?"
Looking at your repo, there are mainly 3 ways to run your app.
-
Lets say you ran it using 'go run main.go' then the app gets exposed on port 8081 as mentioned in your go code at http.ListenAndServe(":8081", router).
-
Lets say you ran it using docker-compose using "docker-compose up" then your app gets exposed on port 3000 on your laptop as you are actively mapping Docker container (your app's) port 8081 to your laptop's (host) port 3000 in your docker-compose file.
-
There is also another option to directly run your app using docker command with the help of Dockerfile. In this way, you can specify the port number on the docker run CLI command on which your app gets exposed. Reference - https://docs.docker.com/engine/reference/commandline/run/#publish
Depending on how you are running the app, check appropriate port.
As you've mentioned you are dockerizing your app, it means adding a Dockerfile so that it can be run in a container.
Docker run, docker-compose , Kubernetes, etc. are all different runtimes for running containerized apps.
Your port rules depend on that runtime configuration mapping to your code's port (8081 in this case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论