我正在研究 golang 项目,最近我阅读了有关 docker 的文章,并尝试在我的应用程序中使用 docker。我正在使用 mongoDB 作为数据库。现在的问题是,我正在创建 Dockerfile 来安装所有软件包并编译和运行 go 项目。我在本地运行 mongo 数据,如果我在没有 docker 的情况下运行 go 程序,它会给我输出,但是如果我在同一个项目中使用 docker(只是安装依赖项并运行项目),它会成功编译但不提供任何输出,有错误::
CreateSession: no reachable servers
Run Code Online (Sandbox Code Playgroud)
我的 Dockerfile::
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang
WORKDIR $GOPATH/src/myapp
# Copy the local package files to the container's workspace.
ADD . /go/src/myapp
#Install dependencies
RUN go get ./...
# Build the installation command inside the container.
RUN go install myapp
# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/myapp
# Document that the service listens on port 8080.
EXPOSE 8080
EXPOSE 27017
Run Code Online (Sandbox Code Playgroud)
当你在 Docker 中运行你的应用程序时,它运行在一个虚拟环境中;它就像另一台计算机,但一切都是虚拟的,包括网络。
为了将您的容器连接到主机,Docker 为其提供了一个特殊的 ip 地址,并为该 ip 提供了一个值为 的 url host.docker.internal。
因此,假设 mongo 正在主机上的每个接口上绑定运行,从容器中可以使用连接字符串访问它:
mongodb://host.docker.internal:21017/database
简化, Just use host.docker.internal as your mongodb hostname.
| 归档时间: |
|
| 查看次数: |
5964 次 |
| 最近记录: |