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

Adjustable project-name flag for docker-compose #512

Closed
Tracked by #9044
lando-cal opened this issue May 29, 2019 · 43 comments
Closed
Tracked by #9044

Adjustable project-name flag for docker-compose #512

lando-cal opened this issue May 29, 2019 · 43 comments
Assignees
Labels
containers Issue in vscode-remote containers feature-request Request for new features or functionality verified Verification succeeded
Milestone

Comments

@lando-cal
Copy link

Currently, the docker-compose command seems to base --project-name off of the parent/project folder.

Launching containers from two sub-folders of a root project, seems to namespace them in separate areas. I've yet to find a way to override this. While this may work when both containers are inheriting from a top level docker-compose file, this would mean major refactoring of our repositories to match our IDE, rather than just specifying a flag to launch related projects under the same namespace.

As an example:

  • Project1/
    • devcontainer1/
      • devcontainer.json
      • docker-compose.dev.yml
    • Project1.componentX/
      • devcontainer2/
        • devcontainer.json
        • docker-compose.dev.yml
      • Dockerfile
      • docker-compose.yml
    • docker-compose.yml
    • Dockerfile

devcontainer1/devcontainer.json expands Project1/docker-compose.yml , creating Docker networks/assets prepended with Project1_.

devcontainer2/devcontainer.json expands Project1.componentX/docker-compose.yml, which then either fails, as it's referencing Docker networks/assets with no prefix, or creates assets with the now incorrect prefix of Project1.componentX if assets are created explicitly.

@Chuxel Chuxel transferred this issue from microsoft/vscode-dev-containers May 29, 2019
@kieferrm kieferrm added the containers Issue in vscode-remote containers label May 31, 2019
@chrmarti
Copy link
Contributor

Related: #222

@chrmarti chrmarti added the feature-request Request for new features or functionality label Oct 21, 2019
@chrmarti chrmarti self-assigned this Oct 21, 2019
@chrmarti
Copy link
Contributor

We should also check for COMPOSE_PROJECT_NAME in .env or ENV.

@oben59
Copy link

oben59 commented Dec 29, 2019

I'm really interested in this feature, to be consistent with how docker-compose works.
I'd like to be able to clone a project with a docker-compose container declaration, just change the project name parameter and be able to launch my dev container regardless of the directory I cloned it in without any naming collision.

@ihormaslov
Copy link

I have the same issue.
Seems like COMPOSE_PROJECT_NAME is ignored in .env

@chrmarti chrmarti added this to the Backlog milestone Jan 9, 2020
@szymon-nowak
Copy link

Maybe custom (remote) project name could also be defined in the devcontainer.json file?

@zhijian720
Copy link

zhijian720 commented Jan 17, 2020

I got the same issue, it will be very helpful if we can check COMPOSE_PROJECT_NAME in .env

-----temporary solution------
looks like vscode get the project name(folder name) from the first compose file provided to docker-compose, if create a dummy init compose file to the folder with expected name, vscode will use expected project name, the additional work is add this dummy init file to devcontainer.json

directory structure as below:

  • expected_project_name
    • project1
      • .devcontainer
        • devcontainer.json
      • docker-compose.yml
  • docker-compose-init.yml

devcontainer.json as below:
{
"context": "..",
"dockerComposeFile": [
"../../docker-compose-init.yml",
"../docker-compose.yml"
]
}

vscode will get:
docker-compose --project-name expected_project_name -f ....\expected_project_name\docker-compose-init.yml -f ....\expected_project_name\project1\docker-compose.yml

@LiNuXaDDiKt
Copy link

I'm also quite interested. managing directory naming is a pain when multiple developers are working on the same server. Managing this from either the .env file or devcontainer.json would be ideal I think.

@TsuyoshiUshio
Copy link

+1 I also want to override the project_name. I have directories that has a different name but almost the same docker compose. I need to remove container when I switch the directory.

@reaper8055
Copy link

I was wondering if there's a fix other than what @zhijian720 mentioned?

@mrmachine
Copy link

mrmachine commented Sep 24, 2020

#979 (closed as a dupe of this issue) and #2645 (incorrectly closed with "working as described") give the best explanation for this issue.

The official docs still say:

VS Code will respect the value of the COMPOSE_PROJECT_NAME environment variable if set for the VS Code process or in a .env file in the root of the project.

And yet, VS Code will in fact always bring up containers with --project-name BASENAME_OF_PROJECT_DIR

[3787 ms] Start: Run: docker-compose --project-name FOO -f /Users/tailee/IC/Projects/FOO/docker-compose.yml -f /Users/tailee/IC/Projects/FOO/.devcontainer/docker-compose.dev.yml up -d --build

This CLI arg seems to override COMPOSE_PROJECT_NAME exported before launching VS Code from CLI or in .env in the project root directory.

@chrmarti
Copy link
Contributor

@mrmachine Using the COMPOSE_PROJECT_NAME environment variable should work, could you open a bug report for this?

@mrmachine
Copy link

@chrmarti Is that not what this issue is all about, already? There are several comments about it in this issue, even from you saying that:

We should also check for COMPOSE_PROJECT_NAME in .env or ENV.

And #979 which perfectly describe the problem was already closed as a duplicate of this issue.

@chrmarti
Copy link
Contributor

I made the environment variable work a while back. You seem to describe a regression and I would appreciate a separate issue so I can follow up.

This issue is tracking the addition of a property to the devcontainer.json for the project name - the way I interpret it.

@rubensa
Copy link

rubensa commented Oct 5, 2020

Yes, that's the point of this feature-request, to allow specifying project-name (for docker-compose) in .devcontainer.json so you don't have to create a .env file in the root of your project and set the COMPOSE_PROJECT_NAME variable there.

@odannyc
Copy link

odannyc commented Mar 9, 2021

@chrmarti

Are there any updates? I would also like to set the project-name via the devcontainer.json file.

@chrmarti
Copy link
Contributor

chrmarti commented Mar 9, 2021

@odannyc Not at this moment. It is best to 👍 the issue's description to add your vote.

@farums
Copy link

farums commented Apr 17, 2021

а если еще внутри docker-compose топ-то docker in docker COMPOSE_PROJECT_NAME в .env не могу задать потому что он воссияет на внутренний докер

@gwax
Copy link

gwax commented May 4, 2021

I would like to add an additional note as to the benefit of configuration in devcontainer.json rather than .env

There are a lot of tools, which structure themselves around managing environment via a .env file, which can clash with VSCode, including docker-compose itself. Being unconfigurable outside of a shared file makes the solution unusable by developers that share their repo with other non-VSCode developers or use tools that collide with .env (e.x. dotenv and and its variants).

@crhan
Copy link

crhan commented Apr 17, 2022

I have met this problem when I was preparing a dev environments for the whole team on a shared server. The docker compose project name will always be collised when two dev members open the same code project. Think of the "Clone Repository in Container Volume" scene. And every dir name and structure will be the same.

Finally I found a solution. Use devcontainer.json initializeCommand to generate a unique project name for docker-compose. Refer to this method: https://gist.github.com/crhan/4d45c28015f2046704cedb67a80d3cec

@mmenzyns
Copy link

mmenzyns commented Oct 20, 2022

I used @crhan solution to my problem. I needed devcontainer to not collide with standard docker-compose usage, I think @gwax might have had the same problem. What I did is create .env right before creating devcontainer, and deleting it right after devcontainer is created. It's definitely ugly solution, but I see no other choice. I put something similar to this in devcontainer.json:

"initializeCommand": "echo COMPOSE_PROJECT_NAME=debug > .env",
"postCreateCommand": "rm -f .env",

@marcindulak
Copy link

The above "initializeCommand" trick creating .env seems not working anymore #7377

@jmdfm
Copy link

jmdfm commented Sep 14, 2023

@chrmarti It's a very confusing choice to have the compose project name come from a .env file and not from configuration in the devcontainer.json file. Is there a specific reason reading a project_name key in decvontainer.json was not chosen? Is there any reason that shouldn't be done? Would a PR be accepted that allowed that option?

@sliekens
Copy link

I might be missing the point of having the extension pass --project-name $dirname at all.

Doesn't Docker Compose already use the directory name if you omit the option?

It should also be possible to set the project name in the docker-compose.yml

version: "3.9"
name: my-project-name # <----- used as prefix for volumes etc.
services:
   app: # ...

volumes:
  app-data: # <-----  my_project_name__app-data

Currently the extension overrides the name from the docker-compose.yaml by passing a different --project-name. I'm not sure why it's doing that?

@sliekens
Copy link

sliekens commented Sep 24, 2023

Here is some piece of code if someone wants and have time to contribute: https://github.com/devcontainers/cli/blob/413c5f790f3a4972bf6f05b71d71ac946c87bc7c/src/spec-node/dockerCompose.ts#L45

As far as I can tell, getProjectName can be greatly simplified by just asking docker-compose for the project name.

Here's how you would do it in bash, but can be easily modified to work in Node.js:

$ docker-compose -f docker-compose.yml config --format json | jq .name
"my-project-name"

This reuses the logic built in to Docker Compose to determine the project name:

  • From the COMPOSE_PROJECT_NAME environment variable
  • From the top level name: variable from the config file (or the last name: from a series of config files specified using -f)
  • The basename of the project directory containing the config file (or containing the first config file specified using -f)

This can be implemented in a fully backwards-compatible way. Using a .env file to set COMPOSE_PROJECT_NAME will continue to work. Otherwise the YAML is checked for a name: property, which is currently not supported. If neither are set, the base name of the directory containing the (first (*)) config file will be used.

(*) might be problematic if the only config file is the one in .devcontainer/ which now that I think about it is probably why the current implementation exists.

cc @chrmarti

@jmdfm
Copy link

jmdfm commented Sep 24, 2023

@sliekens Great find, and yes an even better approach to just go with docker defaults. All of my Docker related files are in a subdir .docker so I don't like it using the defualt directory name, so passing a name in some form is a must for me.

@zsteinkamp
Copy link

The lack of support for using name: from the docker-compose.yml file surprised me. I would have assumed this would be evaluated prior to checking for .env or env vars.

@protection6 protection6 mentioned this issue Sep 29, 2023
@sliekens
Copy link

sliekens commented Oct 21, 2023

I would have assumed this would be evaluated prior to checking for .env or env vars.

Env vars are checked first to enable multiple instances of a compose project.

$ COMPOSE_PROJECT_NAME=main docker-compose up -d
[+] Running 4/4
 ✔ Network main_default           Created                                                                                                                                                                                                                                          0.1s
 ✔ Volume "main_postgres-data"    Created                                                                                                                                                                                                                                          0.0s
 ✔ Container main-db-1            Started                                                                                                                                                                                                                                          0.1s
 ✔ Container main-devcontainer-1  Started                                                                                                                                                                                                                                          0.1s
$ COMPOSE_PROJECT_NAME=alt docker-compose up -d
[+] Running 4/4
 ✔ Network alt_default           Created                                                                                                                                                                                                                                           0.1s
 ✔ Volume "alt_postgres-data"    Created                                                                                                                                                                                                                                           0.0s
 ✔ Container alt-db-1            Started                                                                                                                                                                                                                                           0.1s
 ✔ Container alt-devcontainer-1  Started

For my projects, I put a static .env file with nothing but COMPOSE_PROJECT_NAME in source control. It would be nice if name: in docker-compose.yml was supported so I can delete the file again.

@DavidTWynn
Copy link

I am running into this issue too where I would like to use the top level name: in my docker compose file to set the compose project name. .env is not enough because I would like it to be changed by another environment variable. I can do this is a compose file but not .env.

@Superskyyy
Copy link

Superskyyy commented Apr 24, 2024

In a shared machine, a .devcontainer is git checked-in and therefore shared by multiple developers. The issue is that if you don't offer a convenient way to change the project name people's different variants of the devcontainer (due to changing dockerfile a bit to suit personal needs) are going to collide with each other and cause issues. Container name is another thing that should be able to overriden in devcontainer.json

@Superskyyy
Copy link

The above "initializeCommand" trick creating .env seems not working anymore #7377

#7377 (comment) according to this comment it should be back to working again.

@DavidTWynn
Copy link

Yes I was able to use the initilizeCommand and postCreateCommand to do what I need as a work around. The difference for me was I already had a .env so needed to append to it, then remove just the line that was changed.

Hoping this will be fixed in the future by respecting the top-level name: in a docker compose file.

@Superskyyy
Copy link

I didn't use initializeCommand because our use case demand each user to fully isolate with other user's containers, so I wrote up a similar script like the previous suggested code, the idea is to replace the container_name and generate a .env file (project_name) with make devcontainer. One small inconvenience is that we need to dynamically assemble and gitignore the docker compose file, otherwise container name still will be accidentally checked into git if not careful. Can also solve with a pre-commit hook.

@Superskyyy
Copy link

I'm surprised that it will not use the docker compose .env file in the .devcontainer folder, instead it has to be at the root of the project. So I need to programatically create two .envs.. which is not a good looking solution.

@chrmarti
Copy link
Contributor

chrmarti commented May 3, 2024

I'm surprised that it will not use the docker compose .env file in the .devcontainer folder, instead it has to be at the root of the project. So I need to programatically create two .envs.. which is not a good looking solution.

It needs to be next to the docker-compose.yml. This is given by Docker Compose: https://docs.docker.com/compose/environment-variables/set-environment-variables/

@chrmarti
Copy link
Contributor

chrmarti commented May 3, 2024

Fixing by adding support for the name attribute in the compose file.

@chrmarti chrmarti closed this as completed May 3, 2024
@zsteinkamp
Copy link

Thank you @chrmarti! I will feel the benefit of this improvement many times over!

@chrmarti
Copy link
Contributor

chrmarti commented May 7, 2024

This is now available with Dev Containers 0.365.0-pre-release. Let us know if it works (or doesn't!) for you, thanks!

@DavidTWynn
Copy link

DavidTWynn commented May 7, 2024

Yes that worked for me. I tried it out yesterday and it didn’t but now it does. Making sure there is no environment variable, I see the “docker compose ls” project name was changed with the name value in docker compose.

I updated my dev container extension to the newest release, removed the initializeCommand and postCreateCommand from devcontainer.json that setup the old environment variable, I then added the top level name attribute to docker-compose.yml, deleted the old compose instance and container then with vscode rebuild the container and compose environment.

One thing to mention is I was using the username of the person who ran it to make the compose name unique. This worked with bash because I would make the username lowercase. Now using “name: project_${USER}” doesn’t work because docker wants it to be lower case. Now just looking into a way to keep it unique and valid.

@sliekens
Copy link

sliekens commented Jun 11, 2024

It works for me.

PS: don't forget to update https://code.visualstudio.com/remote/advancedcontainers/set-docker-compose-project-name.

PS (2): this only works with docker compose V2, i.e. the plugin for docker that is written in Go and invoked with docker compose, not the standalone tool that is written in Python and invoked with docker-compose. The devcontainer CLI uses the standalone tool by default.

In Azure Pipelines, these versions are installed:

$ docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

$ docker compose version
Docker Compose version v2.23.3

In this scenario, it is necessary to pass --docker-compose-path="docker compose" to devcontainer build commands. @chrmarti

I suspect it would be better if the devcontainer CLI would detect if docker compose V2 is available and then prefer it instead of docker-compose. Another option could be to bundle a version of Compose V2 with the CLI, so the CLI is less reliant on system environment.

@chrmarti
Copy link
Contributor

@sliekens Thanks, updating the docs here: microsoft/vscode-docs#7377

We automatically use docker compose when there is no docker-compose and with Docker Desktop docker-compose just forwards to docker compose. Azure Pipelines seems to be in the process of removing v1: actions/runner-images#9557.

@microsoft microsoft locked and limited conversation to collaborators Jun 17, 2024
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 verified Verification succeeded
Projects
None yet
Development

No branches or pull requests