Yser
Yser

Reputation: 2106

How to use env attribute of Vagrant Docker provider?

I want to change an environment-variable of a docker container with the Vagrant Docker provisioner. How can I do this?

Example Vagrantfile:

config.vm.define 'container' do |ws|
    ws.vm.hostname = 'container'
    ws.ssh.port = 23
    ws.ssh.guest_port = 23
    ws.vm.provider "docker" do |d|
      d.image = "name/image"
      d.env = {
        "SSH_PORT" => 23
      }
      d.vagrant_machine = "host"
      d.vagrant_vagrantfile = "../Vagrantfile"
      d.force_host_vm = true
      d.has_ssh = true
    end
end

Example Dockerfile:

FROM centos:centos7
ENV PORT 22
#...
RUN echo "Port $PORT" >>  /somefile.txt
#...
EXPOSE $PORT

It's always ending up with PORT=22 instead of 23. A possible workaround with d.create_args = ["-e", "PORT=23"] failed too.

Sources: Vagrant Docker Docker environment-vars

Upvotes: 6

Views: 3122

Answers (2)

Clayton SIlva
Clayton SIlva

Reputation: 45

Try to change the d.env part:

d.env = {
        "SSH_PORT":"23"
}

Upvotes: 0

Mykola Gurov
Mykola Gurov

Reputation: 8715

When you define ENV PORT 22 in your Dockerfile, it will be defined as such and not inherited from the build environment. If you wish to override it, you could do that while running the container: docker run --rm -it -e PORT=23 <your image> env | grep PORT.

Upvotes: 1

Related Questions