You can add the following line to your .devcontainer.json and it will set it up for you:
// Allow the devcontainer to run host docker commands, see https://github.com/devcontainers/templates/tree/main/src/docker-outside-of-docker
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
}
This uses "Devcontainer Features", which add common language/tool/CLI to a development container. You will have the docker CLI installed, and it will point to the existing docker daemon on your host.
Answer from Jethro on Stack OverflowVideos
You can add the following line to your .devcontainer.json and it will set it up for you:
// Allow the devcontainer to run host docker commands, see https://github.com/devcontainers/templates/tree/main/src/docker-outside-of-docker
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
}
This uses "Devcontainer Features", which add common language/tool/CLI to a development container. You will have the docker CLI installed, and it will point to the existing docker daemon on your host.
If you prefer to remain in the docker-from-docker approach, different from the alternative solution called docker-in-docker proposed by wanheda, you can connect the dev container to your host ports.
Assuming that you preserve your host's docker socket mount:
"mounts": ["source=/var/run/docker.sock,target=/var/run/docker-host.sock,type=bind"]
you can add this line to your .devcontainer/devcontainer.json file:
"runArgs": ["--add-host=host.docker.internal:host-gateway"]
In this way, you give the dev container a way to reach your host's address by just using the host name host.docker.internal
I suggest you to read this article, because docker-from-docker has the advantage of lower overhead but it has also some bind mounting limitations.
Use Docker or Kubernetes from a container
Hi everyone,
I want to build a devcontainer so that we have a standard development environment for our team.
In our project, we are using an Nx monorepo that contains two apps: a NextJS frontend and a NestJS backend. We are also intending on using a Postgres Database.
We intend to have everything be deployed as containers in the end, by using Docker and Docker-Compose to help set up our services. But we would like to be able to use Docker while we are developing, for example, we want to test with dummy data from our Postgres DB that is in a container, and also we want to have the sense of what the production environment is like while we are in development.
I would also need to set up a script that once I run Docker Compose that I would have Nx watching for any changes, and then trigger a rebuild of the respective Docker container so that the containers have the latest changes or something, or perhaps I would use a shared volume.
I was going over the given templates in VS Code, and I noticed two options: "Docker in Docker" and "Docker outside of Docker."
I get the idea of what each of them mean, but I want to ask which one is better for my use case? And I also want to ask if the approach I am using for hot-reloading my Docker containers is fine or what does the community recommend?
Thanks! :)