docker机器环境

预计阅读时间:3分钟

设置环境变量以指示docker应针对特定计算机运行命令。

$ docker-machine env --help

Usage: docker-machine env [OPTIONS] [arg...]

Display the commands to set up the environment for the Docker client

Description:
   Argument is a machine name.

Options:

   --swarm	Display the Swarm config instead of the Docker daemon
   --shell 	Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh], default is sh/bash
   --unset, -u	Unset variables instead of setting them
   --no-proxy	Add machine IP to NO_PROXY environment variable

docker-machine env machinename打印出export可以在子shell中运行的命令。运行docker-machine env -u打印unset命令可以逆转此效果。

$ env | grep DOCKER
$ eval "$(docker-machine env dev)"
$ env | grep DOCKER

DOCKER_HOST=tcp://192.168.99.101:2376
DOCKER_CERT_PATH=/Users/nathanleclaire/.docker/machines/.client
DOCKER_TLS_VERIFY=1
DOCKER_MACHINE_NAME=dev
$ # If you run a docker command, now it runs against that host.
$ eval "$(docker-machine env -u)"
$ env | grep DOCKER
$ # The environment variables have been unset.

上面描述的输出用于shell bashzsh(如果不确定使用的是哪种shell,则很有可能是bash)。但是,这些并不是Docker Machine支持的唯一外壳。Docker Machine检测到您环境中可用的外壳并列出它们。码头工人的支持bashcmdpowershell,和emacs

如果您正在使用fish并且SHELL环境变量已正确设置为所fish位于的路径,则以期望docker-machine env name的格式输出值fish

set -x DOCKER_TLS_VERIFY 1;
set -x DOCKER_CERT_PATH "/Users/nathanleclaire/.docker/machine/machines/overlay";
set -x DOCKER_HOST tcp://192.168.99.102:2376;
set -x DOCKER_MACHINE_NAME overlay
# Run this command to configure your shell:
# eval "$(docker-machine env overlay)"

如果您使用的是Windows并使用PowerShell或Windows cmd.exe,则docker-machine env Docker Machine现在应该自动检测您的shell。如果自动检测无效,您仍然可以使用--shell标记覆盖它docker-machine env

对于PowerShell:

$ docker-machine.exe env --shell powershell dev

$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.101:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\captain\.docker\machine\machines\dev"
$Env:DOCKER_MACHINE_NAME = "dev"
# Run this command to configure your shell:
# docker-machine.exe env --shell=powershell dev | Invoke-Expression

对于cmd.exe

$ docker-machine.exe env --shell cmd dev

set DOCKER_TLS_VERIFY=1
set DOCKER_HOST=tcp://192.168.99.101:2376
set DOCKER_CERT_PATH=C:\Users\captain\.docker\machine\machines\dev
set DOCKER_MACHINE_NAME=dev
# Run this command to configure your shell: copy and paste the above values into your command prompt

提示:另请参阅如何在当前shell中取消设置环境变量

从代理中排除创建的计算机

env命令支持一个--no-proxy标志,该标志可确保将创建的计算机的IP地址添加到NO_PROXY/no_proxy环境变量

docker-machine与本地VM提供程序(例如virtualbox或)一起使用时vmwarefusion,在Internet访问需要HTTP代理的网络环境中使用时, 此功能很有用。

$ docker-machine env --no-proxy default

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.104:2376"
export DOCKER_CERT_PATH="/Users/databus23/.docker/machine/certs"
export DOCKER_MACHINE_NAME="default"
export NO_PROXY="192.168.99.104"
# Run this command to configure your shell:
# eval "$(docker-machine env default)"

您可能还想访问有关HTTP_PROXY使用--engine-env标志来 设置创建的守护程序的文档docker-machine create

机器env子命令