Reaching host’s localhost from inside a vscode devcontainer

GoLedger
1 min readFeb 19, 2021

--

Imagine you’re migrating your application to a vscode devcontainer and it consumes some local services, i.e a database atlocalhost:27017. How do you access this local port from inside your dev container?

If you have one of those dev containers and you’re struggling to access your development database running in another container in your machine then you’ve come to the right place.

Optional: you can invoke docker commands on your host from the dev container by making the system socket available, I will link some tutorials on the end for this.

Enable host.docker.internal route

Once you’ve installed Docker into the image you’ll want to access those containers that you used to access from localhost.

At your docker-compose.yml inside your .devcontainer add these lines to your service

# your file should look like this
services:
dev:
# other definitions
extra_hosts:
- "host.docker.internal:host-gateway"

This maps your localhost from your machine host to a name host.docker.internal. Now substitute all occurrences of localhost to host.docker.internal and it's done!

So if you’ve been connecting to your database using localhost:27017 as the conn URL, you only need to change it to host.docker.internal:27071 .

Enjoy!

author: Vieira Neto

--

--

GoLedger
GoLedger

Written by GoLedger

We deploy permissioned blockchain.

No responses yet