how to permanently set environment variable for bo

2019-07-16 10:56发布

问题:

I have tried to put my environment variable at /var/lib/boot2docker/profile file at guest machine, and restart it

export http_proxy=http://proxy:port

then i open shell from my host machine (Windows 7) by using

docker-machine ssh default

I can't find 'http_proxy' from my environment variable by using

env

回答1:

The docker daemon sources /var/lib/boot2docker/profile before starting. The HTTP_PROXY variable will be available in the docker daemons environment. Users logging in via ssh will not see this variable.

Any /etc/profile.d/*.sh files will be loaded into a users profile at login but as you pointed out, this is reset back to the base image after every reboot.

The /var/lib/boot2docker/ directory contains the files that are persisted over reboots. The bootlocal.sh will be run at the end of startup. bootsync.sh file will be run before docker.

Edit /var/lib/boot2docker/bootsync.sh to include

echo 'export HTTP_PROXY="http://whatever"' > /etc/profile.d/proxy.sh

Then the variable will be available for anything that logs in after docker has started for the first time.

○ → docker-machine restart default-docker
...
○ → docker-machine ssh default-docker
...
docker@default-docker:~$ echo $HTTP_PROXY
http://whatever


回答2:

When PHPdocker.io generates a docker-compose.yml file, it also puts in a section for the environment variables.

tie-mysql:
  image: mysql:5.7
  container_name: tie-mysql
  environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_DATABASE=db_name
    - MYSQL_USER=db_user
    - MYSQL_PASSWORD=db_password

It starts running Docker-compose from the Vagrantfile:

config.vm.provision "shell", run: "always", 
      inline: "cd /home/docker/tie/phpdocker && docker-compose up -d 1>&2"

and if you aren't using docker-compose you could also arrange to put an appropriate file (like @Matt's /etc/profile.d/proxy.sh) into the virtual machine as it is being provisioned (with the config.vm.provision "shell"), rather than manually adding it later.



回答3:

You can create a custom virtual machine with proxy configuration like this

docker-machine create -d virtualbox --engine-env HTTP_PROXY=http://10.x.y.z:4951  --engine-env HTTPS_PROXY=https://10.x.y.z:4951 testbox

It will create a virtual machine with permanent proxy configuration.