I have a golang application which starts a TCP server on port 8080. Every thing works fine when I run the app native.
However when I run it as a container, I am unable to even telnet to the port from within the container itself.
docker ps
9bb08785b728 dp_local "/bin/dragonpit-linux" 8 seconds ago Up 7 seconds 8080-8081/tcp youthful_villani
docker exec -it youthful_villani sh
/ # telnet localhost 8080
telnet: can't connect to remote host (127.0.0.1): Connection refused
Note: used 0.0.0.0 as well as 127.0.0.1 in place of localhost
TCP Server starting code
var err error
var lc net.ListenConfig
th.listener, err = lc.Listen(ctx, "tcp", "0.0.0.0:8080")
if err != nil {
return err
}
clog.Info(ctx, "tcp protocol listening", "listenAddr", th.addr)
I hard-coded the address to see whats the issue.
My Dockerfile
FROM golang:1.18.0 as builder
RUN mkdir -p /build/
ADD . /build
WORKDIR /build
RUN CGO_ENABLED=0 GOOS=linux go build -o tcp_server
FROM alpine:latest
EXPOSE 8080 8081
RUN mkdir -p /server/config
ADD config /server/config/
ENV SMC_PATH /
COPY --from=builder /build/tcp_server /bin/
RUN apk update
RUN apk add busybox-extras
ENTRYPOINT ["/bin/tcp_server"]
Output of docker-inspect
"NetworkSettings": {
"Bridge": "",
"SandboxID": "a70f476a8a7376b1e5a935b67170145f2222e059c5b2a1a63da50519a491babf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": null,
"8081/tcp": null
},
"SandboxKey": "/var/run/docker/netns/a70f476a8a73",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7b6b9d0d5f83b8136919ac0f765167f6d380a8d836f460a0243bedeb3489a013",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "16563fdda1d5059bb6e2800455f2e87ac8d02e040386eae595a215692e849d76",
"EndpointID": "7b6b9d0d5f83b8136919ac0f765167f6d380a8d836f460a0243bedeb3489a013",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
Docker running command and starting logs
docker run dp_local
25T14:59:46.367075600Z","caller":"build/main.go:24","msg":"Starting listening","listenAddr":"0.0.0.0:8080"
Edit:
I just printed out the Addr().String() of TCP Listener, I got this
{"level":"info","ts":"2022-04-25T15:44:28.952095700Z","caller":"server/tcp.go:65","msg":"[::]:8080"}
Answering my question here. It was actually a TCP parameter which was set incorrectly and caused issues. The culprit was MTU. For reference: https://www.baeldung.com/cs/tcp-max-packet-size#:~:text=The%20maximum%20size%20of%20a,size%20should%20never%20exceed%20MTU.
Two Assumptions based on your question
You want to expose ports 8080 and 8081
EXPOSE 8080 8081
2. You are running the docker image and the client on same machine
In that case you need to bind the Docker container ports to ports on an interface on the local machine. Let's use localhost. You docker run command should look like
docker run -p 127.0.0.1:8080:8080/tcp -p 127.0.0.1:8081:8081/tcp dp_local
This will bind container ports 8080 to 8080 and 8081 to 8081 on localhost of local machine.
Read more here
Related
I have 'docker desktop' installed on my windows 10.
so, I pulled in Jenkins image from docker hub and then run the commands:
docker pull jenkins/jenkins
docker volume create jenkins_home
docker run --name jenkins -p 8080:8000 -p 50000:50000 -v C:/Users/myusername/Documents/jenkins_home:/var/jenkins_home jenkins/jenkins
On console, everything was ok, and get the password for the admin user.
So after that I run
docker start jenkins
and then
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36683f39dd31 jenkins/jenkins "/sbin/tini -- /usr/…" 6 minutes ago Up 2 minutes 8080/tcp, 0.0.0.0:50000->50000/tcp, 0.0.0.0:8080->8000/tcp jenkins
Now, I need to log into http://localhost:8080/ port to view the Jenkins web app. But did not bring up Jenkins app. I also tried using docker inspect to get the container's ip, but port 8080 didn't work on those ips as well.
"NetworkSettings": {
"Bridge": "",
"SandboxID": "19504b4695ca4dfc9c4af41c9a7ef90115004af07d0099ffed33f4838155cddb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"50000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "50000"
}
],
"8000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
],
"8080/tcp": null
},
"SandboxKey": "/var/run/docker/netns/19504b4695ca",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f1938dad37524b58d3146f8bfffa4964687d1946ab97b3ae7f0105ced3f64ddf",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"Aliases": null,
"NetworkID": "b285258a51bc4877fa9ff8a1928eced061f61a4f92f54a5aa756a118e0f1cb7b",
"EndpointID": "f1938dad37524b58d3146f8bfffa4964687d1946ab97b3ae7f0105ced3f64ddf",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
NOTE: Before running jenkins, I made sure the port wasn't busy.
netstat -aon | findstr 8080
After kenkins running, the command shows me
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 25076
TCP [::]:8080 [::]:0 LISTENING 25076
If I find the process to use this port, I got this
tasklist | findstr 25076
com.docker.backend.exe 25076 Console 2 22.664 K
so, Which ip address do I use to see the Jenkins app that is running in the container? and why the localhost on port 8080 doen't work ?
Om Mac I'm using docker for my local web and I want to access mysql with MySQL-workbench but after I did install MySQL-workbench and try to connect to database I get this error message
Can't connect to MySQL server on '127.0.0.1' (61)
I tried to find solution for docker but I didn't find any.
docker inspect pkbook_mysql_1
"NetworkSettings": {
"Bridge": "",
"SandboxID": "cb984bedd303958298621c1571482c7c06a2a326df021902c15a7955b8c26b2c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": null
},
"SandboxKey": "/var/run/docker/netns/cb984bedd303",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"publiquip_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"mysql",
"ee6b2dd46fb8"
],
"NetworkID": "8809726a588595aea20989df3608ae6646f2e64aa928974469df34b52febe7a4",
"EndpointID": "f41cf7f7e42f4921393e13c7db9ce046c784c159ad0ff3c74d45ed788323d4b1",
"Gateway": "172.20.0.1",
"IPAddress": "172.20.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:14:00:04",
"DriverOpts": null
}
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e34b4dbad1e3 pkbook_joomla "/entrypoint.sh apac…" 4 days ago Up 5 hours 0.0.0.0:80->80/tcp pkbook_joomla_1
ee6b2dd46fb8 mysql:5.6 "docker-entrypoint.s…" 6 days ago Up 5 hours 3306/tcp pkbook_mysql_1
0ccd811b7955 pkbook_gulp "docker-entrypoint.s…" 6 days ago Up 5 hours pkbook_gulp_1
You need to publish the 3306 port with the -p opt option.
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql
Instead of host 127.0.0.1 try to type container name and port.
If it doesn't work.
In terminal type docker ps. (If You didn't gave the name during container creation, docker will name it by random name). If container is running You should see it on the list.
Then check IP and PORT:
docker inspect [CONTAINER-NAME]
and find under:
"NetworkSettings":
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "33082"
}
],
"33060/tcp": null
},
So, in this situation it's 0.0.0.0:33082
You need to set the allow login from any ip with MYSQL_ROOT_HOST=%
Full docker command line as follows:
>docker run --name mysql-server -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql/mysql-server
--name means that it's container name
-d means that it's mysql docker image
When docker status is Up 3 minutes (healthy) you can easy connect to DB via mysql Workbench
I have a simple web app container running on docker engine for mac (v.1.12.5) using the following:
docker run --rm -p 80:8089 test-app
I've checked my container's IP under Networks > bridge from the following:
docker inspect $(docker ps -l --format "{{.ID}}")
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f53f1b93aa0f2fda186498d30e7f6e5b97ba952d1b6fe442663ac6025fd74ce3",
"EndpointID": "178937cf211c2360d9f9c594891985637d1d82a334a40b1b46d3acb2ea8aaf20",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2", // <- use this?
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
As far as I understand, I am running my web app container on my docker engine directly on my laptop (not via docker-machine). At this point, I'm not so much concerned with making it work rather than understanding.
My container has an assigned IP 172.17.0.2 which I've pasted above and I've mapped my web app container (with an EXPOSE 80) to port 8089 via the docker run -p flag.
I'm under the impression that I should be able to reach my web app at http:// 172.17.0.2:8089 but I just get no response. Why?
If the process in the container listens on port 80, your -p flag should be the other way round -p 8089:80. The service can then be reached at localhost:8089.
I have a container
ps -a
de3ff650fbef tasklist-img "/bin/sh -c 'java -ja" 2 minutes ago Up 2 minutes 8080/tcp, 0.0.0.0:8080->8081/tcp tasklist-img-01
And could not access like localhost:8080/task-list
When I inspect I got json with ip address 172.17.0.2, it is also unreachable
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "82398f58a56f6685223df13f0e053c1ad388505ff595ec270eb4e9a445109641",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": null,
"8081/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/82398f58a56f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "24ef0b8e2aea470b5a0c4e48b96e5b203ea35b291689fe6d73b71b870c86f659",
"EndpointID": "e8d9d3e209e858d6e881295899949df457fd8ca28cb059105bebceb6015e1e7d",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
My docker machine run under ip 192.168.99.100
i run container with command
docker run -d -p 8080:8081 --name tasklist-img-01 tasklist-img
I can access http://192.168.99.100:8080/ and it is return only dropwizard metrics, http://192.168.99.100:8080/task-list/ unreachable
I have also log
INFO [2016-11-14 19:32:29,324] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened application#522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:29,402] org.eclipse.jetty.setuid.SetUIDListener: Opened admin#35390ee3{HTTP/1.1}{0.0.0.0:8081}
INFO [2016-11-14 19:32:29,406] org.eclipse.jetty.server.Server: jetty-9.2.z-SNAPSHOT
INFO [2016-11-14 19:32:30,428] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET /task-list (com.bjedrzejewski.tasklistservice.TaskListResource)
GET /task-list/big (com.bjedrzejewski.tasklistservice.TaskListResource)
INFO [2016-11-14 19:32:30,435] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler#378bd86d{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,450] io.dropwizard.setup.AdminEnvironment: tasks =
POST /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
WARN [2016-11-14 19:32:30,451] io.dropwizard.setup.AdminEnvironment:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW !
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE !
! LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR !
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2016-11-14 19:32:30,456] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler#7b60c3e{/,null,AVAILABLE}
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application#522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin#35390ee3{HTTP/1.1}{0.0.0.0:8081}
So I cant understand where is my micro service?:)))
Looks like you mapped port 8081 of the container to 8080 of the host
docker run -d -p 8080:8081
And there are ports listening on 8080 and 8081
INFO [2016-11-14 19:32:30,486] org.eclipse.jetty.server.ServerConnector: Started application#522a32b1{HTTP/1.1}{0.0.0.0:8080}
INFO [2016-11-14 19:32:30,487] org.eclipse.jetty.server.ServerConnector: Started admin#35390ee3{HTTP/1.1}{0.0.0.0:8081}
You can access it, sure, but this is not the true 8080 of the container.
You probably need to publish the range of ports
docker run -d -p 8080-8081:8080-8081
I have installed docker-toolbox v1.12 locally on a Windows 8.1 laptop.
As suggested here I have run docker run -d --name exhibitor netflixoss/exhibitor:1.5.2. This has pulled all the images in my Docker VM "home". docker inspect <<container-id>> (container id obtained from docker ps command) reveals my Network Settings as below.
"NetworkSettings": {
"Bridge": "",
"SandboxID": "c1a16c1704f76b2e2a35b2ae6a18780aaedac078cceb005b419cafb405b1e3b2",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"2181/tcp": null,
"2888/tcp": null,
"3888/tcp": null,
"8080/tcp": null
},
"SandboxKey": "/var/run/docker/netns/c1a16c1704f7",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3e19d6c6eeb2e7c9dbb9b357ac4ecc9515bdf391df6688f17420bb9443ce3d22",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "85b5af82cb891978b6d3272f622c747fa68546d0401a24e85c9d42f7644ea2d4",
"EndpointID": "3e19d6c6eeb2e7c9dbb9b357ac4ecc9515bdf391df6688f17420bb9443ce3d22",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
I'm trying to invoke the running Docker container from Windows host using "http://172.17.0.2:8080/exhibitor/v1/ui/index.html". But that doesn't seem to work. docker ps suggests the container is up
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
1a849df14320 netflixoss/exhibitor:1.5.2 "java -jar exhibitor-" About an hour ago Up About an hour 2181/tcp, 2888/tcp, 3888/tcp, 8080/t
cp exhibitor
Also I can connect to the container using docker attach 1a849df14320 and see the following log entries time-to-time
INFO com.netflix.exhibitor.core.activity.ActivityLog Cleanup task completed [pool-2-thread-93]
I'm a complete newbie with Docker.
You have not exposed the port to the host.
docker ps -a should give output similar to:
0.0.0.0:1234->9876/tcp, 0.0.0.0:4321->7890/tcp
This shows port 1234 on the host is mapped to 9876 in the container.
Your docker ps output has no port mappings. You should do:
docker run .......... -p 8080:8080
You can always use the -P flag too (if you expose the port in the Dockerfile) but the above solution is the quickest.