working with devcontainer, docker compose and volumes

已回答

Hello,

i am testing devcontainer together with full featured docker-compose.yml (for easy on boarding process and regardless if Windows or Linux user).

But since i've had no luck to create a working devcontainer directly from Git (Remote Development → Create Dev Container → From VCS Project), I'm taking a little detour now:

  1. File → New Project from Version Control (enter Git remote URL and local storage path) → Clone
  2. Context Menu devcontainer.json → Dev Container →Create Dev Container and Clone Sources → Build Container and Continue
    (PhpStrom create a container now and run a new instance; otherwise go to Services → Dev Containers → Open Project)

This way is almost bullet proof, but only until nobody decide to remove the local Project again because he don't needed.

The Problem: Mount sources are mixed now but shouldn't. The project file local.ini is still linked to local project while any other project files are cloned to a new volume “jb_devcontainer_sources_…”

docker inspect foobar-php-1
….
        "Mounts": [
           {
               "Type": "bind",
               "Source": "/home/paul/Projekte/foobar/.devcontainer/php/local.ini",
               "Destination": "/usr/local/etc/php/conf.d/local.ini",
               "Mode": "rw",
               "RW": true,
               "Propagation": "rprivate"
           },
           {
               "Type": "volume",
               "Name": "jb_devcontainers_shared_volume",
               "Source": "/var/lib/docker/volumes/jb_devcontainers_shared_volume/_data",
               "Destination": "/.jbdevcontainer/JetBrains/RemoteDev/dist",
               "Driver": "local",
               "Mode": "rw",
               "RW": true,
               "Propagation": ""
           },
           {
               "Type": "volume",
               "Name": "jb_devcontainer_sources_c8a35fa6520a3353bef726d95be9f55c",
               "Source": "/var/lib/docker/volumes/jb_devcontainer_sources_c8a35fa6520a3353bef726d95be9f55c/_data",
               "Destination": "/workspace/foobar",
               "Driver": "local",
               "Mode": "rw",
               "RW": true,
               "Propagation": ""
           }
       ],

 

Example devcontainer config:

devcontainer.json
{
  "dockerComposeFile": "docker-compose.yml",
  "service": "php",
  "workspaceFolder": "/workspace/foobar"
}

docker-compose.yml
name: foobar
services:
  php:
    image: php:8.4.14-fpm-trixie
    volumes:
      - ./local.ini:/usr/local/etc/php/conf.d/local.ini

local.ini
memory_limit=1G

 

Could somebody help me please?

0

Hello, Paul.

The problem you described is related to the relative paths being specified in your Docker Compose configuration:

      - ./local.ini:/usr/local/etc/php/conf.d/local.ini

Due to the implementation of the build from VCS project (explained in a bit more detail here in our documentation), the relative paths in the configuration are not supported

The Problem: Mount sources are mixed now but shouldn't. The project file local.ini is still linked to local project while any other project files are cloned to a new volume “jb_devcontainer_sources_…”

In the scenario you use now, it is an expected outcome because the Create Dev Container and Clone Sources option is not the same as the option to build from the VCS project. 
The difference is following: when you build the dev container completely from the VCS project (the option that doesn't work for you), the dev container configuration and the build context are picked up from your VCS repository; on contrary, when you open the devcontainer.json file and select the Create Dev Container and Clone Sources action in the gutter, the whole build context (including the mounted local.ini file) is picked up from your local project, but the project sources themselves are cloned into the container.

 

If you don't want to keep the repository locally, the only possible solution I can suggest is to remove the relative mount path from the Docker Compose configuration. By the way, do I understand correctly that the local.ini file is supposed to be mounted from your local PC to the dev container? If it is already present inside the VCS repository, and you just need to move it to the specific folder inside the container, it could technically be done via some lifecycle script (like postCreateCommand) executed on an already up and running container. 

0

请先登录再写评论。