1

I would like to create 2 containers with almost same settings except environment variables.

Tried severals things without success especially the .override.yml (passing multiple .yml to docker-compose overriding the previous one).

# docker-compose.yml
version: '3.7'
services:
  web:
    image: nginx
    container_name: test-web
    ports:
      - 80:80

Now the other one

# docker-compose.override.yml
version: '3.7'
services:
  web:
    container_name: dupe-web
    ports:
      - 8080:80

Doing :

$ docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
Recreating test-web ... done

Kills me the test-web container and starts the new one name dupe-web.

Is there a way to have both launched using same service?

In my case I would like to create two containers:

  • one for developers
  • one for staging (almost same settings except env as said)

Any help would be welcome.

3
  • Do you need them to be on the same port? Commented May 1, 2022 at 8:04
  • Nope, also on a different port. Edited my question to be more explicit Commented May 1, 2022 at 8:23
  • I think, what you want to do fails the purpose of docker. Docker gives each process/utility a different layer to run upon. So the services name (like web here) must be unique as far as I know. You can create two containers with different names for different usage purposes(web1 and web2). @TheFool thanks for pointing out, removed that part. Commented May 1, 2022 at 9:10

1 Answer 1

5

If you name the service the same, you override it. So your file name docker-compose.override.yml is actually very fitting.

I think that's logical and desired. How else would you be able to override, and what would be the name of the service in the second file? Compose had to choose one for you in some way. After all, it wouldn't make sense, imo.

You can use YAML anchors/overrides for your needs, but they have to be in the same file for that to work.

name: example
services:
  web: &web
    image: nginx
    container_name: test-web
    ports:
      - 80:80
  web2:
    <<: *web
    container_name: dupe-web
    ports:
      - 8080:80

I would also recommend removing the container name.

name: example
services:
  test-web: &web
    image: nginx
    ports:
      - 80:80
  dupe-web:
    <<: *web
    ports:
      - 8080:80

If you run docker compose config you can see the normalized spec:

name: example
services:
  dupe-web:
    image: nginx
    networks:
      default: null
    ports:
    - mode: ingress
      target: 80
      published: "8080"
      protocol: tcp
  test-web:
    image: nginx
    networks:
      default: null
    ports:
    - mode: ingress
      target: 80
      published: "80"
      protocol: tcp
networks:
  default:
    name: example_default
Sign up to request clarification or add additional context in comments.

2 Comments

Didn’t know you can have several times the same entry, thus overriding the previous one. Will have a try later on and keep you posted.
Learned new things also new command (docker-compose config). All clear, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.