title: VS code remote containers - connect to a database either on host machine or one in a separate docker-compose file
body: <p>I have started using VS code remote containers, specifically the GO one thats generated for you via the options. It provides a <code>devcontainer.json</code> file and a <code>Dockerfile</code> which works fine.</p>
  
  <p>My problem is trying to connect to a database from within this remote container. </p>
  
  <p>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.</p>
  
  <p>However each time I try to connect either via <code>db, err := sqlx.Connect("postgres", dsn)</code> or using soda <code>soda migrate up</code> if just hangs for a while before saying its unable to connect.</p>
  
  <p>Has anyone got a solution to connecting to a DB?</p>
  
  <p>This is my dockerfile for go:</p>
  
  <pre><code>#-------------------------------------------------------------------------------------------------------------
  # 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 \
      &amp;&amp; export DEBIAN_FRONTEND=noninteractive \
      &amp;&amp; apt-get -y install --no-install-recommends apt-utils dialog 2&gt;&amp;1 \
      #
      # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
      &amp;&amp; apt-get -y install git openssh-client less iproute2 procps lsb-release \
      #
      # Build Go tools w/module support
      &amp;&amp; mkdir -p /tmp/gotools \
      &amp;&amp; cd /tmp/gotools \
      &amp;&amp; GOPATH=/tmp/gotools GO111MODULE=on go get -v golang.org/x/tools/gopls@latest 2&gt;&amp;1 \
      &amp;&amp; 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&gt;&amp;1 \
      #
      # Build Go tools w/o module support
      &amp;&amp; GOPATH=/tmp/gotools go get -v github.com/alecthomas/gometalinter 2&gt;&amp;1 \
      #
      # Build gocode-gomod
      &amp;&amp; GOPATH=/tmp/gotools go get -x -d github.com/stamblerre/gocode 2&gt;&amp;1 \
      &amp;&amp; GOPATH=/tmp/gotools go build -o gocode-gomod github.com/stamblerre/gocode \
      #
      # Install Go tools
      &amp;&amp; mv /tmp/gotools/bin/* /usr/local/bin/ \
      &amp;&amp; mv gocode-gomod /usr/local/bin/ \
      #
      # Install golangci-lint
      &amp;&amp; curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin 2&gt;&amp;1 \
      #
      # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
      &amp;&amp; groupadd --gid $USER_GID $USERNAME \
      &amp;&amp; useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
      # [Optional] Add sudo support
      &amp;&amp; apt-get install -y sudo \
      &amp;&amp; echo $USERNAME ALL=\(root\) NOPASSWD:ALL &gt; /etc/sudoers.d/$USERNAME \
      &amp;&amp; chmod 0440 /etc/sudoers.d/$USERNAME \
      #
      # Clean up
      &amp;&amp; apt-get autoremove -y \
      &amp;&amp; apt-get clean -y \
      &amp;&amp; rm -rf /var/lib/apt/lists/* /tmp/gotools
  
  # Update this to "on" or "off" as appropriate
  ENV GO111MODULE=auto
  
  </code></pre>
  
  <p>This is the devcontainer.json file:</p>
  
  <pre><code>// 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"
  }
  </code></pre>
  
  <p>This is the docker-compose file I am using for the DB</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Soda uses a .yml file to connect to the database and its as follows:</p>
  
  <pre><code>development:
    dialect: postgres
    database: my_db
    user: user
    password: pass
    host: 174.24.0.1
    port: 1234
  </code></pre>
  
  <p>Thanks in advance</p>