Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote - Containers configure multiple services in devcontainer.json #254

Closed
kan opened this issue May 9, 2019 · 6 comments
Closed

Remote - Containers configure multiple services in devcontainer.json #254

kan opened this issue May 9, 2019 · 6 comments
Labels
containers Issue in vscode-remote containers feature-request Request for new features or functionality

Comments

@kan
Copy link

kan commented May 9, 2019

I am developing using docker-compose with containers including go and node containers.

I would like to develop vscode attached separately to go container and node container respectively, but it seems that only one service can be specified in the current devcontainer.json specification.

Please let me know if there is a way to attach to multiple containers.

@egamma egamma added feature-request Request for new features or functionality containers Issue in vscode-remote containers labels May 9, 2019
@Chuxel
Copy link
Member

Chuxel commented May 9, 2019

@kan We don't support this from a single VS Code window right now, but you can do this using multiple VS Code windows using the extension's "attach" functionality. Here's how:

  1. Open your folder in a container
  2. After everything is up, File > New Window
  3. F1 > Remote-Containers: Attach to Running Container...
  4. Select your other container from the list

Each Window should now be connected to a different container. You can also add "shutdownAction": "none" to devcontainer.json to keep the containers running when you shut down VS Code.

@kan
Copy link
Author

kan commented May 10, 2019

@Chuxel thank you. I understand that I can attach to multiple containers in the way you indicate.

In that case, for example, what will be the handling of the extension executed in the container? I would like to use the eslint extension in the container of node, but how should I describe the case of using vscode-go in the container of go in devcontainer.json ?

@Chuxel
Copy link
Member

Chuxel commented May 10, 2019

@kan There's two ways you can do this -- and there's also a way to do this without attaching and just opening a folder in a container twice.

If you attach:

  1. Once you've connected to the container, you can install any extension via the extensions panel and they'll install inside the container.
  2. You could also drop a .vscode/extensions.json file in the folder for the other container with recommended extensions so you'll be prompted to install them.

The other way is to use two devcontainer.json files from different folders that point to a common docker-compose.yml. VS Code will actually attach to running containers if they are already up when you open a folder in a container -- which is why this works. I'm actually curious if this alternative would work for you given your source code setup.

FYI @chrmarti @egamma - Wonder if we should doc this.

Here's a super simplified view of this alternate setup. Say you've got your folders setup like this:

📁container1-src
     📄.devcontainer.json
     📄hello.go
📁container2-src
     📄.devcontainer.json
     📄hello.js
📄docker-compose.yml

Next, assume this is your docker-compose.yml in the root:

version: '3'
services:
  container-1:
    image: ubuntu:bionic
    volumes:
      - ./container-1-src:/workspace
      - ~/.gitconfig:/root/.gitconfig
    command: sleep infinity
    links:
      - container-2
 
  container-2:
    image: ubuntu:bionic
    volumes:
      - ./container-2-src:/workspace
      - ~/.gitconfig:/root/.gitconfig
    command: sleep infinity 

Your container1-src/.devcontainer.json looks like this:

{
	"name": "Container 1",
	"dockerComposeFile": ["../docker-compose.yml"],
	"service": "container-1",
	"workspaceFolder": "/workspace",
	"extensions": ["ms-vscode.Go"],
	"shutdownAction": "none"
}

And container2-src/.devcontainer.json looks like this:

{
	"name": "Container 2",
	"dockerComposeFile": ["../docker-compose.yml"],
	"service": "container-2",
	"workspaceFolder": "/workspace",
	"extensions": ["dbaeumer.vscode-eslint"],
	"shutdownAction": "none"
}

To connect to both:

  1. F1 > Open Folder in Container... > container1-src folder
  2. This will start up both containers and connect this window to service container-1 and install the Go extension.
  3. File > New Window
  4. In the new Window, F1 > Open Folder in Container... > container2-src folder
  5. This will connect to the already running container-2 and install ESLint.

The "shutdownAction":"none" in the devcontainer.json files will leave the containers running even if you close the two VS Code windows -- which prevents you from accidentally shutting down both containers (and thus both windows stop working) by closing one. This part is optional.

Here's the test project: multi-container.zip

@kan
Copy link
Author

kan commented May 10, 2019

@Chuxel Thank you for the explanation. I will try to find out how to prepare devcontainer.json for each container.

@kan kan closed this as completed May 10, 2019
@chrmarti
Copy link
Contributor

@Chuxel Sounds good to me. Maybe we can have this as a separate page under 'advanced' topics. It will be good to collect more feedback from developers as we haven't used this setup ourselves much.

@egamma
Copy link
Member

egamma commented May 10, 2019

👍 on documenting this as an advanced setup.

Chuxel added a commit to microsoft/vscode-docs that referenced this issue May 15, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Jun 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
containers Issue in vscode-remote containers feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants