本文整理匯總了Golang中github.com/fsouza/go-dockerclient.NewClientFromEnv函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClientFromEnv函數的具體用法?Golang NewClientFromEnv怎麽用?Golang NewClientFromEnv使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClientFromEnv函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: dockerClients
// dockerClients creates two *docker.Client, one for long running operations and
// the other for shorter operations. In test / dev mode we can use ENV vars to
// connect to the docker daemon. In production mode we will read docker.endpoint
// from the config file.
func (d *DockerDriver) dockerClients() (*docker.Client, *docker.Client, error) {
if client != nil && waitClient != nil {
return client, waitClient, nil
}
var err error
var merr multierror.Error
createClients.Do(func() {
if err = shelpers.Init(); err != nil {
d.logger.Printf("[FATAL] driver.docker: unable to initialize stats: %v", err)
return
}
// Default to using whatever is configured in docker.endpoint. If this is
// not specified we'll fall back on NewClientFromEnv which reads config from
// the DOCKER_* environment variables DOCKER_HOST, DOCKER_TLS_VERIFY, and
// DOCKER_CERT_PATH. This allows us to lock down the config in production
// but also accept the standard ENV configs for dev and test.
dockerEndpoint := d.config.Read("docker.endpoint")
if dockerEndpoint != "" {
cert := d.config.Read("docker.tls.cert")
key := d.config.Read("docker.tls.key")
ca := d.config.Read("docker.tls.ca")
if cert+key+ca != "" {
d.logger.Printf("[DEBUG] driver.docker: using TLS client connection to %s", dockerEndpoint)
client, err = docker.NewTLSClient(dockerEndpoint, cert, key, ca)
} else {
d.logger.Printf("[DEBUG] driver.docker: using standard client connection to %s", dockerEndpoint)
client, err = docker.NewClient(dockerEndpoint)
}
client.HTTPClient.Timeout = dockerTimeout
return
}
d.logger.Println("[DEBUG] driver.docker: using client connection initialized from environment")
client, err = docker.NewClientFromEnv()
if err != nil {
merr.Errors = append(merr.Errors, err)
}
client.HTTPClient.Timeout = dockerTimeout
waitClient, err = docker.NewClientFromEnv()
if err != nil {
merr.Errors = append(merr.Errors, err)
}
})
return client, waitClient, merr.ErrorOrNil()
}
示例2: dockerClient
// dockerClient creates *docker.Client. In test / dev mode we can use ENV vars
// to connect to the docker daemon. In production mode we will read
// docker.endpoint from the config file.
func (d *DockerDriver) dockerClient() (*docker.Client, error) {
if client != nil {
return client, nil
}
var err error
createClient.Do(func() {
// Default to using whatever is configured in docker.endpoint. If this is
// not specified we'll fall back on NewClientFromEnv which reads config from
// the DOCKER_* environment variables DOCKER_HOST, DOCKER_TLS_VERIFY, and
// DOCKER_CERT_PATH. This allows us to lock down the config in production
// but also accept the standard ENV configs for dev and test.
dockerEndpoint := d.config.Read("docker.endpoint")
if dockerEndpoint != "" {
cert := d.config.Read("docker.tls.cert")
key := d.config.Read("docker.tls.key")
ca := d.config.Read("docker.tls.ca")
if cert+key+ca != "" {
d.logger.Printf("[DEBUG] driver.docker: using TLS client connection to %s", dockerEndpoint)
client, err = docker.NewTLSClient(dockerEndpoint, cert, key, ca)
} else {
d.logger.Printf("[DEBUG] driver.docker: using standard client connection to %s", dockerEndpoint)
client, err = docker.NewClient(dockerEndpoint)
}
return
}
d.logger.Println("[DEBUG] driver.docker: using client connection initialized from environment")
client, err = docker.NewClientFromEnv()
})
return client, err
}
示例3: init
func init() {
var err error
Client, err = docker.NewClientFromEnv()
if err != nil {
panic(err)
}
}
示例4: Complete
func (o *DockerbuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
paths, envArgs, ok := cmdutil.SplitEnvironmentFromResources(args)
if !ok {
return kcmdutil.UsageError(cmd, "context directory must be specified before environment changes: %s", strings.Join(args, " "))
}
if len(paths) != 2 {
return kcmdutil.UsageError(cmd, "the directory to build and tag must be specified")
}
o.Arguments, _, _ = cmdutil.ParseEnvironmentArguments(envArgs)
o.Directory = paths[0]
o.Tag = paths[1]
if len(o.DockerfilePath) == 0 {
o.DockerfilePath = filepath.Join(o.Directory, "Dockerfile")
}
var mounts []dockerbuilder.Mount
for _, s := range o.MountSpecs {
segments := strings.Split(s, ":")
if len(segments) != 2 {
return kcmdutil.UsageError(cmd, "--mount must be of the form SOURCE:DEST")
}
mounts = append(mounts, dockerbuilder.Mount{SourcePath: segments[0], DestinationPath: segments[1]})
}
o.Mounts = mounts
client, err := docker.NewClientFromEnv()
if err != nil {
return err
}
o.Client = client
o.Keyring = credentialprovider.NewDockerKeyring()
return nil
}
示例5: dockerSetup
// dockerSetup does all of the basic setup you need to get a running docker
// process up and running for testing. Use like:
//
// task := taskTemplate()
// // do custom task configuration
// client, handle, cleanup := dockerSetup(t, task)
// defer cleanup()
// // do test stuff
//
// If there is a problem during setup this function will abort or skip the test
// and indicate the reason.
func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle, func()) {
if !testutil.DockerIsConnected(t) {
t.SkipNow()
}
client, err := docker.NewClientFromEnv()
if err != nil {
t.Fatalf("Failed to initialize client: %s\nStack\n%s", err, debug.Stack())
}
driverCtx, execCtx := testDriverContexts(task)
driver := NewDockerDriver(driverCtx)
handle, err := driver.Start(execCtx, task)
if err != nil {
execCtx.AllocDir.Destroy()
t.Fatalf("Failed to start driver: %s\nStack\n%s", err, debug.Stack())
}
if handle == nil {
execCtx.AllocDir.Destroy()
t.Fatalf("handle is nil\nStack\n%s", debug.Stack())
}
cleanup := func() {
handle.Kill()
execCtx.AllocDir.Destroy()
}
return client, handle, cleanup
}
示例6: getDockerClient
// Get a *docker.Client, either using the endpoint passed in, or using
// DOCKER_HOST, DOCKER_TLS_VERIFY, and DOCKER_CERT path per their spec
func getDockerClient(dockerEndpoint string) (*docker.Client, error) {
if len(dockerEndpoint) > 0 {
glog.Infof("Connecting to docker on %s", dockerEndpoint)
return docker.NewClient(dockerEndpoint)
}
return docker.NewClientFromEnv()
}
示例7: checkDockerVersion
func checkDockerVersion() error {
startCheck("Docker up to date")
dockerVersionTest, err := docker.NewClientFromEnv()
if err != nil {
return err
}
minDockerVersion, err := docker.NewAPIVersion("1.9")
e, err := dockerVersionTest.Version()
if err != nil {
return err
}
currentVersionParts := strings.Split(e.Get("Version"), ".")
currentVersion, err := docker.NewAPIVersion(fmt.Sprintf("%s.%s", currentVersionParts[0], currentVersionParts[1]))
if err != nil {
return err
}
if !(currentVersion.GreaterThanOrEqualTo(minDockerVersion)) {
diagnose(Diagnosis{
Title: "Docker up to date",
Description: "<fail>Docker engine is out of date (min: 1.9)</fail>",
DocsLink: "https://docs.docker.com/engine/installation/",
Kind: "fail",
})
} else {
diagnose(Diagnosis{
Title: "Docker up to date",
Kind: "success",
})
}
return nil
}
示例8: TestConformanceInternal
// TestConformance* compares the result of running the direct build against a
// sequential docker build. A dockerfile and git repo is loaded, then each step
// in the file is run sequentially, committing after each step. The generated
// image.Config and the resulting filesystems are compared. The next step reuses
// the previously generated layer and performs an incremental diff. This ensures
// that each step is functionally equivalent.
//
// Deviations:
// * Builds run at different times
// * Modification timestamps are ignored on files
// * Some processes (gem install) result in files created in the image that
// have different content because of that (timestamps in files). We treat
// a file that is identical except for size within 10 bytes and neither old
// or new is zero bytes to be identical.
// * Docker container commit with ENV FOO=BAR and a Docker build with line
// ENV FOO=BAR will generate an image with FOO=BAR in different positions
// (commit places the variable first, build: last). We try to align the
// generated environment variable to ensure they are equal.
// * The parent image ID is ignored.
//
// TODO: .dockerignore
// TODO: check context dir
// TODO: ONBUILD
// TODO: ensure that the final built image has the right UIDs
//
func TestConformanceInternal(t *testing.T) {
testCases := []conformanceTest{
{
ContextDir: "fixtures/dir",
},
// TODO: Fix this test
// {
// ContextDir: "fixtures/ignore",
// },
{
Dockerfile: "fixtures/Dockerfile.env",
},
{
Dockerfile: "fixtures/Dockerfile.edgecases",
},
{
Dockerfile: "fixtures/Dockerfile.exposedefault",
},
{
Dockerfile: "fixtures/Dockerfile.add",
},
}
c, err := docker.NewClientFromEnv()
if err != nil {
t.Fatal(err)
}
for i, test := range testCases {
conformanceTester(t, c, test, i, *compareLayers)
}
}
示例9: Ping
// Ping implements the DockerInterface Ping method.
func (c *KubeDocker) Ping() error {
client, err := docker.NewClientFromEnv()
if err != nil {
return err
}
return client.Ping()
}
示例10: worker
func worker(requests int, image string, completeCh chan time.Duration) {
client, err := docker.NewClientFromEnv()
if err != nil {
panic(err)
}
for i := 0; i < requests; i++ {
start := time.Now()
container, err := client.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: image,
}})
if err != nil {
panic(err)
}
err = client.StartContainer(container.ID, nil)
if err != nil {
panic(err)
}
completeCh <- time.Since(start)
}
}
示例11: DockerClient
// DockerClient creates a docker client from environment
func DockerClient() *dockerclient.Client {
client, err := dockerclient.NewClientFromEnv()
if err != nil {
log.Fatalf("Unabled to create a Docker Client: Is Docker Machine installed and running?")
}
client.SkipServerVersionCheck = true
return client
}
示例12: ClientOrDie
// ClientOrDie creates a new Docker client. If one couldn't be created, logs and error and exits with status code 1
func ClientOrDie() *docker.Client {
cl, err := docker.NewClientFromEnv()
if err != nil {
log.Err("creating new docker client (%s)", err)
os.Exit(1)
}
return cl
}
示例13: NewControllerFromEnv
// NewControllerFromEnv creates a new Docker client using environment clues.
func NewControllerFromEnv(out io.Writer) (*Controller, error) {
client, err := docker.NewClientFromEnv()
if err != nil {
return nil, fmt.Errorf("could not create Docker client: %v", err)
}
return NewController(client, out)
}
示例14: NewDockerClient
func NewDockerClient() (*docker.Client, error) {
host := os.Getenv("DOCKER_HOST")
hostIsLocal := host == "" || strings.HasPrefix(host, "unix://")
if !hostIsLocal {
log.Warnf("Detected DOCKER_HOST %s. This should not be remote.",
host)
}
return docker.NewClientFromEnv()
}
示例15: GetClient
// GetClient returns a valid Docker client, the address of the client, or an error
// if the client couldn't be created.
func (_ *Helper) GetClient() (client *docker.Client, endpoint string, err error) {
client, err = docker.NewClientFromEnv()
if len(os.Getenv("DOCKER_HOST")) > 0 {
endpoint = os.Getenv("DOCKER_HOST")
} else {
endpoint = "unix:///var/run/docker.sock"
}
return
}