Home > Blockchain >  How to use docker-compose links to access username and password details from inside another containe
How to use docker-compose links to access username and password details from inside another containe

Time:02-24

I have a docker-compose file which contains details of my container as well as rabbitmq. Here is a cut down version of my docker-compose.yml file where I am using the container_name and links keywords to access the IP address of rabbitmq from inside my container.

version: "3.2"

environment:
    &my-env
    My_TEST_VAR1: 'test_1'
    My_TEST_VAR2: 'test_2'

rabbitmq:
    container_name: rabbitmq
    image: 'rabbitmq:3.6-management-alpine'
    ports:
      - '5672:5672'
      - '15672:15672'
    environment:
      AMQP_URL: 'amqp://rabbitmq?connection_attempts=5&retry_delay=5'
      RABBITMQ_DEFAULT_USER: "guest"
      RABBITMQ_DEFAULT_PASS: "guest"

  my-service:
    tty: true
    image: my_image_name:latest
    working_dir: /opt/services/my_service/
    command: python3.8 my_script.py
    ports:
      - 9000:9000
    links:
      - rabbitmq:rabbitmq.server
    environment:
      <<: *my-env

From inside my container I can ping the rabbitmq server successfully via:

ping rabbitmq.server

Is there I way I can access the rabbitmq default username and password using this link? Or do I have to just pass them as separate environment variables? (I would like to avoid this duplication if possible)

CodePudding user response:

You should pass them using environment variables. Docker links at this point are an obsolete feature, and I'd recommend just outright deleting any links: you have left in your docker-compose.yml file. Compose sets up networking for you so that the Compose service names rabbitmq and my-server can be used as host names between the containers without any special setup; the environment variables that links provided were confusing and could unintentionally leak data.

If you want to avoid repeating things, you can use YAML anchor syntax as you already have, or write the environment variables into a separate env_file:. Unless you have a lot of settings or a lot of containers, just writing them in the docker-compose.yml file is easiest.

version: '3.8'
services:
  rabbitmq:
    image: 'rabbitmq:3.6-management-alpine'
    ports:
      - '5672:5672'
      - '15672:15672'
    environment:
      RABBITMQ_DEFAULT_USER: "guest"
      RABBITMQ_DEFAULT_PASS: "guest"
    # You may want volumes: to persist the queue.
    # As a special case for RabbitMQ only, you would need a hostname:.

  my-service:
    image: my_image_name:latest
    ports:
      - 9000:9000
    environment:
      # I'd just write these out.
      My_TEST_VAR1: 'test_1'
      My_TEST_VAR2: 'test_2'
      RABBITMQ_HOST: rabbitmq
      RABBITMQ_USER: guest
      RABBITMQ_PASSWORD: guest
    # working_dir: and command: should be in your Dockerfile as
    # WORKDIR and CMD respectively.  links: is obsolete.

In principle you can attach an anchor to any YAML node, though I'd find the syntax a little bit confusing if I was reading it. I'd tend to avoid syntax like this but it is technically an option.

services:
  rabbitmq:
    environment:
      RABBITMQ_DEFAULT_USER: &rabbitmq_user guest
  my-app:
    environment:
      RABBITMQ_USER: *rabbitmq_user

Finally, I hinted initially that the obsolete Docker links feature does republish environment variables. I wouldn't take advantage of this – it's an information leak in many ways, there's potential conflicts with the application's own environment variables, and the links feature in general is considered obsolete – but it is theoretically possible to use it

services:
  rabbitmq:
    environment:
      RABBITMQ_DEFAULT_USER: guest
  my-app:
    links: [rabbitmq]
docker-compose run my-app sh -c 'echo $RABBITMQ_DEFAULT_USER'

It'd be up to your application setup to understand the RabbitMQ image setup variables.

  • Related