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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With