2 min read

Resolve Docker Folder Name Conflict

This happened to me recently: I have a project that needs to be deployed both for staging and production on the same machine. I know this isn’t best practice, but the client insists.

Let’s say my repository name is my-app and it needs to be deployed into these folders respectively:

/code/production/my-app
/code/staging/my-app

As you can see, the final folder has the same name as the repository, even though the parent is different.

Now, when I run this command for staging:

/code/staging/my-app$ docker compose -f docker-compose.staging.yml up -d

Everything works fine until I try to bring up the production code:

/code/production/my-app$ docker compose -f docker-compose.production.yml up -d

Production comes up, but when I checked, my staging was down. It seems like the staging containers were replaced by production containers. What’s happening?

After some research, I found out that even though the folder paths are different, Docker always uses the current folder name to build container names. Since both processes have the same name my-app, the later process replaces the existing one with the same name.

The solution is to use the --project-name [UNIQUE-NAME] argument with the docker compose command.

To bring up both projects, just run these commands:

/code/staging/my-app$ docker compose -f docker-compose.staging.yml --project-name staging up -d
/code/production/my-app$ docker compose -f docker-compose.production.yml --project-name production up -d

For more information, check out their documentation.