VS 代码远程容器 - 连接到主机上的数据库或单独的 docker 撰写文件中的数据库



我已经开始使用 VS 代码远程容器,特别是通过选项为您生成的 GO 容器。它提供了一个devcontainer.json文件和一个工作正常的Dockerfile

我的问题是尝试从此远程容器内连接到数据库。

我尝试使用 docker-compose 启动一个单独的 postgres 数据库,并且还尝试连接到安装在我的主机上的数据库。

但是,每次我尝试通过db, err := sqlx.Connect("postgres", dsn)或使用苏打水连接时,soda migrate up如果只是挂起一段时间,然后说它无法连接。

有没有人有连接到数据库的解决方案?

这是我的 dockerfile for go:

#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
FROM golang:1
# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Configure apt, install packages and tools
RUN apt-get update 
&& export DEBIAN_FRONTEND=noninteractive 
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 
#
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
&& apt-get -y install git openssh-client less iproute2 procps lsb-release 
#
# Build Go tools w/module support
&& mkdir -p /tmp/gotools 
&& cd /tmp/gotools 
&& GOPATH=/tmp/gotools GO111MODULE=on go get -v golang.org/x/tools/gopls@latest 2>&1 
&& GOPATH=/tmp/gotools GO111MODULE=on go get -v 
honnef.co/go/tools/...@latest 
golang.org/x/tools/cmd/gorename@latest 
golang.org/x/tools/cmd/goimports@latest 
golang.org/x/tools/cmd/guru@latest 
golang.org/x/lint/golint@latest 
github.com/mdempsky/gocode@latest 
github.com/cweill/gotests/...@latest 
github.com/haya14busa/goplay/cmd/goplay@latest 
github.com/sqs/goreturns@latest 
github.com/josharian/impl@latest 
github.com/davidrjenni/reftools/cmd/fillstruct@latest 
github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest  
github.com/ramya-rao-a/go-outline@latest  
github.com/acroca/go-symbols@latest  
github.com/godoctor/godoctor@latest  
github.com/rogpeppe/godef@latest  
github.com/zmb3/gogetdoc@latest 
github.com/fatih/gomodifytags@latest  
github.com/mgechev/revive@latest  
github.com/go-delve/delve/cmd/dlv@latest 2>&1 
#
# Build Go tools w/o module support
&& GOPATH=/tmp/gotools go get -v github.com/alecthomas/gometalinter 2>&1 
#
# Build gocode-gomod
&& GOPATH=/tmp/gotools go get -x -d github.com/stamblerre/gocode 2>&1 
&& GOPATH=/tmp/gotools go build -o gocode-gomod github.com/stamblerre/gocode 
#
# Install Go tools
&& mv /tmp/gotools/bin/* /usr/local/bin/ 
&& mv gocode-gomod /usr/local/bin/ 
#
# Install golangci-lint
&& curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin 2>&1 
#
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
&& groupadd --gid $USER_GID $USERNAME 
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME 
# [Optional] Add sudo support
&& apt-get install -y sudo 
&& echo $USERNAME ALL=(root) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME 
&& chmod 0440 /etc/sudoers.d/$USERNAME 
#
# Clean up
&& apt-get autoremove -y 
&& apt-get clean -y 
&& rm -rf /var/lib/apt/lists/* /tmp/gotools
# Update this to "on" or "off" as appropriate
ENV GO111MODULE=auto

这是devcontainer.json文件:

// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/go
{
"name": "Go",
"dockerFile": "Dockerfile",
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Set *default* container specific settings.json values on container create.
"settings": { 
"terminal.integrated.shell.linux": "/bin/bash",
"go.gopath": "/go"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}

这是我用于数据库的码头工人撰写文件

version: "3.1"
services:
db:
image: postgres:10.10
environment:
POSTGRES_PASSWORD: pass
POSTGRES_USER: user
POSTGRES_DB: my_db
container_name: test_db
ports:
- 1234:5432
restart: "no"
volumes:
- ../postgres-data:/var/lib/postgresql/data

Soda 使用 .yml 文件连接到数据库,其如下所示:

development:
dialect: postgres
database: my_db
user: user
password: pass
host: 174.24.0.1
port: 1234

提前致谢

如果有人还在看,这里回答得很好:

如果数据库在主机上运行,则可以使用以下命令:

development:
dialect: postgres
database: my_db
user: user
password: pass
host: host.docker.internal
port: 1234

相关内容

最新更新