登录
首页 >  Golang >  Go问答

Faktory Worker 无法与 Docker 化的应用程序建立连接

来源:stackoverflow

时间:2024-02-14 16:30:25 374浏览 收藏

一分耕耘,一分收获!既然都打开这篇《Faktory Worker 无法与 Docker 化的应用程序建立连接》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我正在尝试将我的 golang 工作应用程序包装到 docker 映像中。我已成功构建图像。但是当我运行时,结果发现我的 docker 容器无法连接到我的 faktory。这是我的 docker 文件:

# builder image
from golang:1.14.11-alpine as builder

# specific directory for build process
workdir /usr/src/build

# copying the source code 
# to the current working directory
copy . .
run apk add --no-cache git

env goprivate=gitlab.com/pinvest/backends/backend-structs
# run echo "machine gitlab.com login jais_pinhome password pinhome123" > ~/.netrc
copy .netrc /root/.netrc

# executing build process
run goos=linux go build -ldflags="-s -w" -o app

# runtime image
from golang:1.14.11-alpine as runtime

# create and use non-root user
# to increase container security 
# ref https://pythonspeed.com/articles/root-capabilities-docker-security/
run adduser myuser --disabled-password

user myuser

workdir /home/myuser

# copy the executable binary file from builder directory
# to the current working directory
copy --from=builder /usr/src/build/app .

# exposing port
expose 8080

# run the application
cmd ["./app"]

这是我的工厂日志:

jaisanas@desktop-h2gjlp8:~/work/worker$ faktory
faktory 1.4.0
copyright © 2021 contributed systems llc
licensed under the gnu public license 3.0
i 2021-01-23t01:52:15.239z initializing redis storage at /home/jaisanas/.faktory/db, socket /home/jaisanas/.faktory/db/redis.sock
i 2021-01-23t01:52:15.251z web server now listening at localhost:7420
i 2021-01-23t01:52:15.251z pid 6897 listening at localhost:7419, press ctrl-c to stop

如果我运行工作容器会返回如下错误:

jaisanas@DESKTOP-H2GJLP8:~/work/worker$ docker run --env-file docker_env.sh -p 8080:27017 --name woker worker
2021/01/23 01:48:40.502243 [0 ] unsupported protocol scheme ""
2021/01/23 01:48:40.502380 faktory_worker_go PID 1 now ready to process jobs
2021/01/23 01:48:40.681601 dial tcp 127.0.0.1:7419: connect: connection refused
2021/01/23 01:48:40.704490 dial tcp 127.0.0.1:7419: connect: connection refused
2021/01/23 01:48:40.711848 dial tcp 127.0.0.1:7419: connect: connection refused
2021/01/23 01:48:40.845921 dial tcp 127.0.0.1:7419: connect: connection refused
2021/01/23 01:48:40.876556 dial tcp 127.0.0.1:7419: connect: connection refused

这里有人熟悉将 golang faktory 应用程序打包到 docker 中并面临类似的问题吗?


解决方案


在运行工作程序的 docker 容器内,localhost 127.0.0.1 指的是容器本身,而不是您的主机(faktory 正在运行的地方)。要从 Docker 容器内部访问主机,您需要连接到特殊地址 host.docker.internal

换句话说,您应该将工作线程设置为连接到位于 host.docker.internal:7419 的 faktory,而不是 127.0.0.1:7419

请参阅 official Docker documentation 了解更多详情。

以上就是《Faktory Worker 无法与 Docker 化的应用程序建立连接》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>