Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 935
Yser Avatar asked Nov 26 '14 16:11

Yser


People also ask

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

Does Docker use .ENV file?

The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both $VARIABLE and ${VARIABLE} syntax are supported.

Can you use Vagrant with Docker?

Vagrant comes with support out of the box for using Docker as a provider. This allows for your development environments to be backed by Docker containers rather than virtual machines. Additionally, it provides for a good workflow for developing Dockerfiles.


1 Answers

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.

like image 75
Mykola Gurov Avatar answered Sep 20 '22 07:09

Mykola Gurov