Igor L.
Igor L.

Reputation: 3475

Docker compose set container name for stacks

I am deploying a small stack onto a UCP

One of the issues I am facing is naming the container for service1.

I need to have a static name for the container, since it's utilized by mycustomimageforservice2

The container_name option is ignored when deploying a stack in swarm mode with a (version 3) Compose file.

I have to use version: 3 compose files.

version: "3"
services:

  service1:
    image: dockerhub/service1
    ports: 
      - "8080:8080"
    container_name: service1container
    networks:
      - mynet

  service2:
    image: myrepo/mycustomimageforservice2
    networks:
      - mynet
    restart: on-failure

networks:
  mynet:

What are my options?

Upvotes: 18

Views: 35295

Answers (5)

MD SHAYON
MD SHAYON

Reputation: 8063

Specify a custom container name, rather than a generated default name.

container_name: my-web-container

see this in the full docker-compose file

version: '3.9'
services:
  
  node-ecom:
    build: .
    image: "node-ecom-image:1.0.0"
    container_name: my-web-container
    ports:
      - "4000:3000"
    volumes:
      - ./:/app:ro
      - /app/node_modules
      - /config/.env
    env_file:
      - ./config/.env

know more

Upvotes: -3

SuperUser Sudo
SuperUser Sudo

Reputation: 295

container_name option is ignored when deploying a stack in swarm mode since container names need to be unique. https://docs.docker.com/compose/compose-file/#container_name

Upvotes: 1

Joris Boschmans
Joris Boschmans

Reputation: 63

If you do have to use version 3 but don't work with swarms, you can add --compatibility to your commands.

Upvotes: 0

Yor Jaggy
Yor Jaggy

Reputation: 425

You can face your problem linking services in docker-compose.yml file. Something like:

version: "3"
services:

  service1:
    image: dockerhub/service1
    ports: 
      - "8080:8080"
    networks:
      - mynet

  service2:
    image: myrepo/mycustomimageforservice2
    networks:
      - mynet
    restart: on-failure
    links:
      - service1

networks:
  mynet:

Using links arguments in your docker-compose.yml you will allow some service to access another using the container name, in this case, service2 would establish a connection to service1 thanks to the links parameter. I'm not sure why you use a network but with the links parameter would not be necessary.

Upvotes: 1

herm
herm

Reputation: 16335

You can't force a containerName in compose as its designed to allow things like scaling a service (by updating the number of replicas) and that wouldn't work with names. One service can access the other using servicename (http://serviceName:internalServicePort) instead and docker will do the rest for you (such as resolving to an actual container address, load balancing between replicas....).

This works with the default network type which is overlay

Upvotes: 12

Related Questions