Strife Cloud
Strife Cloud

Reputation: 29

How to manage environment variables in Rancher

I want manage environment variables in a stack, then the service can use it. For example: I definition a evn tracker_ip=192.168.0.101, then I want use it in service create.

create service

what should I do

Upvotes: 1

Views: 15313

Answers (3)

Bill Maxwell
Bill Maxwell

Reputation: 161

Depending on where the tracker_ip is associated, you could also create an external service as part of the stack. The external service essentially just creates a DNS entry in Rancher. So you could then just link your service to the external_tracker service in compose and refer to tracker.

version: '2'
services:
  myservice:
    ...
    link:
      - tracker_service:tracker
...

Upvotes: 0

Vincent Fiduccia
Vincent Fiduccia

Reputation: 999

There is no way to do exactly what you're asking, because that would allow editing of the variables of running containers and containers are immutable. Environment variables can be defined on services but not defined once on stacks and made available to all services.

Secrets are somewhat like this and can be shared across services, but not edited.

Upvotes: 0

Phoenix
Phoenix

Reputation: 41

There can be several answers depending on what you are trying to do and how you are deploying your stack.

Using the CLI / Rancher Compose

If you are using the command line, you can just use variable interpolation. Instructions on how to do so can be found in the official documentation:

https://docs.rancher.com/rancher/v1.5/en/cli/variable-interpolation/

Using the Rancher UI / Catalogs

If you want to do it through the Rancher UI, you can do it by creating a template in a catalog and having questions to input your environment variables. More details on how to do so here:

https://docs.rancher.com/rancher/v1.5/en/catalog/private-catalog/

You can define questions in the rancher-compose.yml file like this:

version: '2'
catalog:
  name: My Application
  version: v0.0.1
  questions:
  - variable: TRACKER_IP
    label: Tracker IP address
    required: true
    default: 192.168.0.101
    type: string

You can then push the answers to the environment section of your docker-compose.yml template for use within your image:

version: '2'
services:
  web:
    image: myimage
    ports:
    - 8000
    environment:
      TRACKER_IP: ${TRACKER_IP}

Upvotes: 4

Related Questions