2

I have started using VS code remote containers, specifically the GO one thats generated for you via the options. It provides a devcontainer.json file and a Dockerfile which works fine.

My problem is trying to connect to a database from within this remote container.

I have tried spinning up a separate postgres database using docker-compose and have also tried connecting to a database installed on my base machine.

However each time I try to connect either via db, err := sqlx.Connect("postgres", dsn) or using soda soda migrate up if just hangs for a while before saying its unable to connect.

Has anyone got a solution to connecting to a DB?

This is my 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

This is the devcontainer.json file:

// 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"
}

This is the docker-compose file I am using for the DB

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 uses a .yml file to connect to the database and its as follows:

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

Thanks in advance

davidjh
  • 387
  • 1
  • 7
  • 13
  • what is the database connection string you are using, can you please update your dockerfile and docker-compose – Dupinder Singh Jun 11 '20 at 15:33
  • 1
    @DupinderSingh I have updated the post to include the dockerfile and docker-compose I am using. Soda uses a .yml file to connect and its in the post above – davidjh Jun 11 '20 at 15:50
  • Are you just want to connect to a database from inside the docker container ? – Dupinder Singh Jun 11 '20 at 16:32
  • Yep. From within the container. I have a database on my local machine as well as one in a separate container. I know how to connect, but it just hangs then times out. If they were both in one place it would work. But i suspect because the go code is in a container it cant access the databsse on either my machine or a different container – davidjh Jun 11 '20 at 16:41

1 Answers1

2

Great answer here if anyone is still looking:

If your database is running on host machine, you can use this:

development:
  dialect: postgres
  database: my_db
  user: user
  password: pass
  host: host.docker.internal
  port: 1234
tonysta
  • 421
  • 1
  • 4
  • 14