vscode-remote-release icon indicating copy to clipboard operation
vscode-remote-release copied to clipboard

Add ability to specify container name in `devcontainer.json`

Open lugehorsam opened this issue 5 years ago • 19 comments

It would be great to be able to specify a default container name rather than the autogenerated name provided via VSCode.

I could rename the container afterwards but without knowing what the name of the container will be when first generated, it's non-trivial to automate this.

My use case is knowing the name of my development container so I can use volumes-from to pass its volumes to any new containers it creates.

lugehorsam avatar Mar 05 '20 20:03 lugehorsam

In the docker-compose.yml I set the container_name: XXX and my container is named XXX in such case.

I just curious now, why VS Code auto-generates the group for containers automatically, like {ProjectFolderName}_devcontainer and how to specify my custom name for the Docker container group.

1st avatar Jan 04 '21 11:01 1st

Container group name is metadata. Container metadata is passed to the Docker via labels. labels can be passed to the Docker engine using runArgs[]. The Container name is not metadata - this is DNS name of the container on the Corresponding Docker network and meaningful display value for the individual container.

PavelSosin-320 avatar Jan 04 '21 16:01 PavelSosin-320

Is there a way to leverage the docker compose project-name (now called top-level name) as for the devcontainer group name?

# docker-compose.yml
name: sql-server
services:
  az-sql:
      container_name: azure-sql
      image: mcr.microsoft.com/azure-sql-edge:latest
  
      ports:
        - 1433:1433
      
      volumes:
        - sql-data:/data

volumes:
  sql-data:
    name: sql-data

The name: sql-server is now used as the container group name with the docker compose v2.3.3. Previously in docker, this is the folder name. So the question is how do I leverage this when using .devcontainer.json instead of passing via runArgs: []?

image

kosperera avatar Mar 16 '22 09:03 kosperera

Container group name is metadata. Container metadata is passed to the Docker via labels. labels can be passed to the Docker engine using runArgs[]. The Container name is not metadata - this is DNS name of the container on the Corresponding Docker network and meaningful display value for the individual container.

Put a row like this one in your .devcontainer.json: "runArgs": ["--name", "${localEnv:USER}_devcontainer"]

rentom avatar Jun 15 '22 11:06 rentom

Hello, is there a possibility to change the dev container image repository, for now it is vsc-${BASE_IMAGE}-${HASH} ?

mazikiou avatar Jul 21 '22 09:07 mazikiou

I was expecting that name would be reflected as the container name or that there'd be some setting for it. It'd be nice if I got to choose the name - easier for me to look at the container names than the images to see which one's which.

black-snow avatar Oct 01 '22 17:10 black-snow

To set the docker project and container names I currently use:

  • an .env file in the project's root folder with COMPOSE_PROJECT_NAME=my-project (see documentation)
  • optional: set container_name: my-container in the docker-compose.yml below the service

So I can have several devcontainer projects with the same folder name running at the same time.

bkarstaedt avatar Oct 12 '22 08:10 bkarstaedt

Hello, is there a possibility to change the dev container image repository, for now it is vsc-${BASE_IMAGE}-${HASH} ?

The Dev Container extension auto clone the image we used and set the name vsc-${BASE_IMAGE}-${HASH}, then create the container from it. I cannot understand why it do that? Please fix it in current versions, if the extension want to add features, let do it directly in container and don't create new image! Finally I rollback to version 0.238.2 and don't get this issue.

quycao avatar Nov 06 '22 03:11 quycao

Put a row like this one in your .devcontainer.json: "runArgs": ["--name", "${localEnv:USER}_devcontainer"]

It would be good to have this as a comment in the generated .devcontainer.json

jkristia avatar Feb 11 '23 16:02 jkristia

these seems to address container name, but what about image name....an image name like this is very unsexy and unpleasing to the eye when viewing docker processes

vsc-myapp-69e5361f2a0ac432981344b94b2fb69bab00834cd7db4c4b402e7138c6b65c25-uid

dss010101 avatar Apr 02 '23 22:04 dss010101

is there any work on this request ?

armando-fandango avatar May 17 '23 21:05 armando-fandango

This would be good

shoutsid avatar Aug 05 '23 23:08 shoutsid

Container group name is metadata. Container metadata is passed to the Docker via labels. labels can be passed to the Docker engine using runArgs[]. The Container name is not metadata - this is DNS name of the container on the Corresponding Docker network and meaningful display value for the individual container.

Put a row like this one in your .devcontainer.json: "runArgs": ["--name", "${localEnv:USER}_devcontainer"]

my goodness...so glad i dont have to explain what their weird names are during demos anymore. now if we can ust get rid of the vsc-fkldlsjsfjajfajdf;kjas;kfljakldjf;alsjdf;lkajsf;lajsdflkjdkljfldjlfjldjflsjdlsdjfjldjflsdjlfjsdl

dss010101 avatar Sep 17 '23 22:09 dss010101

+1

gerritdeen avatar Oct 12 '23 07:10 gerritdeen

Container group name is metadata. Container metadata is passed to the Docker via labels. labels can be passed to the Docker engine using runArgs[]. The Container name is not metadata - this is DNS name of the container on the Corresponding Docker network and meaningful display value for the individual container.

Put a row like this one in your .devcontainer.json: "runArgs": ["--name", "${localEnv:USER}_devcontainer"]

The 1st quote is for post graduates.

The 2nd quote is for undergraduates.

This one is for you:

"runArgs": ["--name", "hello-world"],

ZYinMD avatar Nov 08 '23 20:11 ZYinMD

I got stuck adding multiple parameters and figured out.

	"runArgs": [
        "--network=host", 
		"--name", "hello-world"
    ],

sidespin-admin avatar Dec 12 '23 20:12 sidespin-admin

Do you have any link regarding the runArgs field? I've only found vague references to it but no actual documentation

eric-burel avatar Mar 21 '24 16:03 eric-burel

these seems to address container name, but what about image name....an image name like this is very unsexy and unpleasing to the eye when viewing docker processes

vsc-myapp-69e5361f2a0ac432981344b94b2fb69bab00834cd7db4c4b402e7138c6b65c25-uid

What I do here, is manually pull the image from the .devcontainer.json that was generated by vscode. Before I open it up vscode as a container.

glennsaint-86 avatar Mar 22 '24 23:03 glennsaint-86

To set the docker project and container names I currently use:

  • an .env file in the project's root folder with COMPOSE_PROJECT_NAME=my-project (see documentation)
  • optional: set container_name: my-container in the docker-compose.yml below the service

So I can have several devcontainer projects with the same folder name running at the same time.

Thank you. It also works with several users on the same server. You helped me a lot.

dmitrymailk avatar Jun 19 '24 23:06 dmitrymailk

Regarding volumes: it would be great if the project name provided in .devcontainer/devcontainer.json or .devcontainer/compose.yaml could also be used in the volume labels:

docker compose -f .devcontainer/compose.yaml up -d app
WARN[0000] volume "devcontainer_enhancements_data" already exists but was created for project "devcontainer-enhancements_devcontainer" (expected "devcontainer_enhancements_devcontainer"). Use `external: true` to use an existing volume

It seems like the volume itself was created with the name devcontainer_enhancements_data, but the label was auto-generated by Dev Containers as devcontainer-enhancements_devcontainer.

viktorianer avatar Oct 01 '24 15:10 viktorianer

Container group name is metadata. Container metadata is passed to the Docker via labels. labels can be passed to the Docker engine using runArgs[]. The Container name is not metadata - this is DNS name of the container on the Corresponding Docker network and meaningful display value for the individual container.

Put a row like this one in your .devcontainer.json: "runArgs": ["--name", "${localEnv:USER}_devcontainer"]

Thank you for this, @rentom

bhaaratkapur avatar Apr 09 '25 13:04 bhaaratkapur

Since we ran into the same container-name collisions with Docker Compose, we built a tiny init script to generate COMPOSE_PROJECT_NAME from your local path - so every clone gets a deterministic, collision-free name:

  1. Sanitizes your local path into COMPOSE_PROJECT_NAME
  2. Writes it into .devcontainer/.env before Compose runs

Minimal example here: https://github.com/reinoxl/unique-devcontainer-name-with-docker-compose.git

Maybe this could be helpful for someone.

reinochsl avatar Jun 17 '25 15:06 reinochsl