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: */*
2
Answers
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.
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).