Dockerize应用程序无法连接到工厂工人



我正试图将我的golang worker应用程序包装成docker映像。我已经成功地建立了形象。但是当我运行时,事实证明我的docker容器不能连接到我的工厂。这是我的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

如果运行worker容器返回如下错误:

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 <nil>] 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工厂应用程序到docker和面临类似的问题吗?

在运行worker的docker容器中,localhost127.0.0.1指的是容器本身,而不是运行factory的主机。要从Docker容器内部访问主机,您需要连接到特殊地址host.docker.internal

换句话说,你应该设置你的工人连接到工厂在host.docker.internal:7419而不是127.0.0.1:7419

查看Docker官方文档了解更多细节。

最新更新