Nick Ginanto
Nick Ginanto

Reputation: 32130

Restart docker with different environment variable

I have a docker-compose that has

myimage:
  image: myimage:latest
  environment:
    MY_VAR: "something"

the container uses MY_VAR internally, and sometimes in automated testing I'd like to restart that specific container with a different MY_VAR (to simulate a process restart with different environment variable settings)

Is there a way to do this while keeping the rest of the docker-compose container up?

Upvotes: 15

Views: 24014

Answers (2)

Lucas dos Santos Abreu
Lucas dos Santos Abreu

Reputation: 596

Like @robin.thoni said, would need to recreate the container to change the MY_VAR value, but you could make the process simpler by using a environment var in the compose YML, like this:

    myimage:
      image: myimage:latest
      environment:
        - MY_VAR: ${A_ENV_VAR}

And running the container like this:


A_ENV_VAR=test docker-compose run myimage

Upvotes: 14

Robin Thoni
Robin Thoni

Reputation: 1721

You can't do that, you have to recreate a container, as environment variables are given in the run command (even with a docker-compose).

Upvotes: 3

Related Questions