I am using a devcontainer in Visual Studio Code. The application I'm working on has a docker-compose.yml file to stand up my app and a postgresql database so that I can perform testing against a working database. What this means is that I am running docker containers from inside a docker container. I use the docker-outside-of-docker feature (https://github.com/devcontainers/features/tree/main/src/docker-outside-of-docker) to make the host's docker socket available within the devcontainer.
Here is my devcontainer.json:
{
"name": "devcontainer-dood",
"build": {
"dockerfile": "./Dockerfile"
},
"mounts": [
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
],
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
}
}
The Dockerfile used for the devcontainer is very simple
FROM mcr.microsoft.com/devcontainers/base:bullseye
RUN apt-get update && \
apt-get install -y \
postgresql-client \
&& apt-get clean
The problem I have is that when I'm running in the devcontainer I am unable to use the postgresql client, psql, to connect to the postgresql instance. If I attempt to do the same when not using the devcontainer it works fine.
I have built a simple repro at https://github.com/jamiekt/devcontainer-dood. When not running using the devcontainer I use docker compose to stand up the postgresql container and connect to it using psql:
docker compose up -d
psql -U postgres -h localhost -d demo
it works fine:
If I then open the repo inside the devcontainer and attempt the same, it fails:
My host is an intel macbook by the way. Anyone using the same hardware/OS should be able to clone the repo (git clone https://github.com/jamiekt/devcontainer-dood.git) and issue the commands above to repro the problem.
There's clearly something awry in the way I'm running docker inside the devcontainer but I'm not knowledgeable enough to know what it is. If someone could tell me why this isn't working and also how to fix it I'd be very grateful.
UPDATE!!! I've realised that if I use
instead of
Then it works. All I need to do was change my devcontainer.json to:
{
"name": "devcontainer-dood",
"build": {
"dockerfile": "./Dockerfile"
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
}
}
this screenshot demonstrates running this:
I've also discovered that when I run using docker-outside-of-docker that the psql command works fine when run from the host. So:
- when using docker-outside-of-docker the postgres container is a sibling of the devcontainer, and connection attempt fails
- when using docker-in-docker the postgres container is a child of the devcontainer, and connection attempt succeeds
Nevertheless I'd like to get this working using docker-outside-of-docker, running docker-in-docker feels too close to inception for my liking. Also I’ve always got this post: https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ by Jerome Petazzo (one of the original Docker developers) at the back of my mind, the general thrust of which is “don’t use docker-in-docker”


