0

I wanted to use docker-compose to spin up new instances of my containers, but with slightly different parameters, so I essentially copied the entire project folder, made changes to my Docker compose file, and did docker-compose up --build but no matter which project folder I run that in, it only recreates the containers rather than spinning up new ones.

Below is my compose file. In one project folder it's this, and in the other, I changed container-name to app-test-client and app-test-api as well as changing the ports (e.g. 8080:80), so why does it recreate instead of spinning up new containers? I want to see both app-client and app-test-client running.

version: '3.2'
services:
  client:
    build:
      context: ./client
    container_name: app-client
    ports:
      - '80:80'
      - '5432:5432'
      - '443:443'
    links:
      - api
  api:
    build:
      context: ./api
    container_name: app-api
    volumes:
      - ~/.ssh:/root/.ssh
    environment:
      # read from ./.env file if it exists
      - EDR_ENVIRONMENT=${EDR_ENV}
      - SAS_ENVIRONMENT=${SAS_ENV}
    command: ['node', '.']
redOctober13
  • 3,662
  • 6
  • 34
  • 61
  • Are the two copies of the file in directories with the same name; `old-project/docker/docker-compose.yml` and `new-project/docker/docker-compose.yml`, say? See _e.g._ [Docker is not creating new container but recreates running one](https://stackoverflow.com/a/43127846). – David Maze Aug 23 '21 at 18:26
  • yes, the directories that contain the `docker-compose.yml` file are named the same, because I just copied the entire project directory and then changed the compose file. – redOctober13 Aug 23 '21 at 18:48

1 Answers1

2

The name is based on the service name, not the container name.

version: '3.2'
services:
  client-test:
    ...
    links:
      - api
  api-test:
     ...

You can also pass the parameter p to change the project name

Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
  • 1
    ah, interesting. I would never have guessed that; I would have thought only the container name mattered. I tried the -p option without changing the service name, but that didn't do anything for me; in fact it wouldn't even build. But changing the service names worked. Is this the recommended way to do this? Copy the project folder and change the service names, or is there a way to spin up duplicate containers with say different ports with only one project and docker-compose file? – redOctober13 Aug 23 '21 at 18:42
  • 2
    I found the `-p` option only works if it's right after `docker-compose` but not after `docker-compose up` which is where I was putting it – redOctober13 Aug 23 '21 at 18:48