本文整理汇总了Golang中github.com/docker/engine-api/types/container.HostConfig类的典型用法代码示例。如果您正苦于以下问题:Golang HostConfig类的具体用法?Golang HostConfig怎么用?Golang HostConfig使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HostConfig类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Process
func (m *UpdateServiceSnapshotByContainer) Process() error {
_, imageName := m.info.GetName()
cached, err := utils.IsImageCached(imageName)
if err != nil {
fmt.Println(err)
return err
}
if !cached {
//TODO: remove this image to save server disk
err := utils.PullImage(imageName)
if err != nil {
return err
}
}
var config container.Config
config.Image = imageName
config.Cmd = []string{snapshotProcess, m.info.CallbackID, m.info.Host, m.info.DataProto}
var hostConfig container.HostConfig
hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s", m.info.DataURL, snapshotMountDir))
containerName := "scan-" + m.info.CallbackID
err = utils.StartContainer(config, hostConfig, containerName)
return err
}
示例2: TestUtilsContainer
func TestUtilsContainer(t *testing.T) {
//TODO: dockyard dev team should provide small testing containers.
imageName := "google/nodejs"
containerName := ""
cached, err := utils.IsImageCached(imageName)
if err == utils.ErrorsNoDockerClient {
fmt.Println("Please start a docker daemon to continue the container operation test")
return
}
assert.Nil(t, err, "Fail to load Image")
if !cached {
err := utils.PullImage(imageName)
assert.Nil(t, err, "Fail to pull image")
}
tmpFile, err := ioutil.TempFile("/tmp", "dockyard-test-container-oper")
assert.Nil(t, err, "System err, fail to create temp file")
defer os.Remove(tmpFile.Name())
var config container.Config
config.Image = imageName
config.Cmd = []string{"touch", tmpFile.Name()}
var hostConfig container.HostConfig
hostConfig.Binds = append(hostConfig.Binds, "/tmp:/tmp")
utils.StartContainer(config, hostConfig, containerName)
//TODO: stop, remove the container process
assert.Equal(t, true, utils.IsFileExist(tmpFile.Name()), "Fail to touch file by using StartContainer")
}
示例3: adaptContainerSettings
// adaptContainerSettings is called during container creation to modify any
// settings necessary in the HostConfig structure.
func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConfig, adjustCPUShares bool) error {
if adjustCPUShares && hostConfig.CPUShares > 0 {
// Handle unsupported CPUShares
if hostConfig.CPUShares < linuxMinCPUShares {
logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
hostConfig.CPUShares = linuxMinCPUShares
} else if hostConfig.CPUShares > linuxMaxCPUShares {
logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
hostConfig.CPUShares = linuxMaxCPUShares
}
}
if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
// By default, MemorySwap is set to twice the size of Memory.
hostConfig.MemorySwap = hostConfig.Memory * 2
}
if hostConfig.ShmSize == 0 {
hostConfig.ShmSize = container.DefaultSHMSize
}
var err error
if hostConfig.SecurityOpt == nil {
hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
if err != nil {
return err
}
}
if hostConfig.MemorySwappiness == nil {
defaultSwappiness := int64(-1)
hostConfig.MemorySwappiness = &defaultSwappiness
}
return nil
}
示例4: setSandboxResources
func setSandboxResources(hc *dockercontainer.HostConfig) {
hc.Resources = dockercontainer.Resources{
MemorySwap: -1, // Always disable memory swap.
CPUShares: defaultSandboxCPUshares,
// Use docker's default cpu quota/period.
}
hc.OomScoreAdj = defaultSandboxOOMScore
}
示例5: modifyHostNetworkOptionForSandbox
// modifyHostNetworkOptionForSandbox applies NetworkMode/UTSMode to sandbox's dockercontainer.HostConfig.
func modifyHostNetworkOptionForSandbox(hostNetwork bool, hc *dockercontainer.HostConfig) {
if hostNetwork {
hc.NetworkMode = namespaceModeHost
} else {
// Assume kubelet uses either the cni or the kubenet plugin.
// TODO: support docker networking.
hc.NetworkMode = "none"
}
}
示例6: setSandboxResources
func setSandboxResources(hc *dockercontainer.HostConfig) {
hc.Resources = dockercontainer.Resources{
MemorySwap: -1, // Always disable memory swap.
CPUShares: defaultSandboxCPUshares,
// Use docker's default cpu quota/period.
}
// TODO: Get rid of the dependency on kubelet internal package.
hc.OomScoreAdj = qos.PodInfraOOMAdj
}
示例7: updateHostConfig
// These two functions are OS specific (for now at least)
func updateHostConfig(hc *dockercontainer.HostConfig, opts *kubecontainer.RunContainerOptions) {
// There is no /etc/resolv.conf in Windows, DNS and DNSSearch options would have to be passed to Docker runtime instead
hc.DNS = opts.DNS
hc.DNSSearch = opts.DNSSearch
// MemorySwap == -1 is not currently supported in Docker 1.14 on Windows
// https://github.com/docker/docker/blob/master/daemon/daemon_windows.go#L175
hc.Resources.MemorySwap = 0
}
示例8: modifyCommonNamespaceOptions
// modifyCommonNamespaceOptions apply common namespace options for sandbox and container
func modifyCommonNamespaceOptions(nsOpts *runtimeapi.NamespaceOption, hostConfig *dockercontainer.HostConfig) {
if nsOpts != nil {
if nsOpts.GetHostPid() {
hostConfig.PidMode = namespaceModeHost
}
if nsOpts.GetHostIpc() {
hostConfig.IpcMode = namespaceModeHost
}
}
}
示例9: setSandboxResources
func setSandboxResources(_ *runtimeApi.PodSandboxResources, hc *dockercontainer.HostConfig) {
// Ignore the resource requests and limits for now and just use the docker
// defaults.
// TODO: apply resource limits based on the configuration.
hc.Resources = dockercontainer.Resources{
MemorySwap: -1, // Always disable memory swap.
CPUShares: defaultSandboxCPUshares,
// Use docker's default cpu quota/period.
}
hc.OomScoreAdj = defaultSandboxOOMScore
}
示例10: modifyHostNetworkOptionForContainer
// modifyHostNetworkOptionForContainer applies NetworkMode/UTSMode to container's dockercontainer.HostConfig.
func modifyHostNetworkOptionForContainer(hostNetwork bool, sandboxID string, hc *dockercontainer.HostConfig) {
sandboxNSMode := fmt.Sprintf("container:%v", sandboxID)
hc.NetworkMode = dockercontainer.NetworkMode(sandboxNSMode)
hc.IpcMode = dockercontainer.IpcMode(sandboxNSMode)
hc.UTSMode = ""
hc.PidMode = ""
if hostNetwork {
hc.UTSMode = namespaceModeHost
}
}
示例11: createRoach
func (l *LocalCluster) createRoach(node *testNode, vols *Container, cmd ...string) {
l.panicOnStop()
hostConfig := container.HostConfig{
PublishAllPorts: true,
NetworkMode: container.NetworkMode(l.networkID),
}
if vols != nil {
hostConfig.VolumesFrom = append(hostConfig.VolumesFrom, vols.id)
}
var hostname string
if node.index >= 0 {
hostname = fmt.Sprintf("roach%d", node.index)
}
var entrypoint []string
if *cockroachImage == builderImageFull {
entrypoint = append(entrypoint, "/"+filepath.Base(*cockroachBinary))
} else if *cockroachEntry != "" {
entrypoint = append(entrypoint, *cockroachEntry)
}
var err error
node.Container, err = createContainer(
l,
container.Config{
Hostname: hostname,
Image: *cockroachImage,
ExposedPorts: map[nat.Port]struct{}{
defaultTCP: {},
},
Entrypoint: entrypoint,
// TODO(pmattis): Figure out why the Go DNS resolver is misbehaving.
Env: []string{"GODEBUG=netdns=cgo"},
Cmd: cmd,
Labels: map[string]string{
// Allow for `docker ps --filter label=Hostname=roach0` or `--filter label=Roach`.
"Hostname": hostname,
"Roach": "",
},
},
hostConfig,
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
l.networkID: {
Aliases: []string{hostname},
},
},
},
node.nodeStr,
)
maybePanic(err)
}
示例12: createRoach
func (l *LocalCluster) createRoach(i int, dns, vols *Container, cmd ...string) *Container {
l.panicOnStop()
hostConfig := container.HostConfig{
PublishAllPorts: true,
}
if dns != nil {
ci, err := dns.Inspect()
maybePanic(err)
hostConfig.DNS = append(hostConfig.DNS, ci.NetworkSettings.IPAddress)
}
if vols != nil {
hostConfig.VolumesFrom = append(hostConfig.VolumesFrom, vols.id)
}
var hostname string
if i >= 0 {
hostname = fmt.Sprintf("roach%d", i)
}
var entrypoint []string
if *cockroachImage == builderImage {
entrypoint = append(entrypoint, "/"+filepath.Base(*cockroachBinary))
} else if *cockroachEntry != "" {
entrypoint = append(entrypoint, *cockroachEntry)
}
c, err := createContainer(
l,
container.Config{
Hostname: hostname,
Domainname: domain,
Image: *cockroachImage,
ExposedPorts: map[nat.Port]struct{}{
cockroachTCP: {},
pgTCP: {},
},
Entrypoint: strslice.New(entrypoint...),
Cmd: strslice.New(cmd...),
Labels: map[string]string{
// Allow for `docker ps --filter label=Hostname=roach0` or `--filter label=Roach`.
"Hostname": hostname,
"Roach": "",
},
},
hostConfig,
nodeStr(i),
)
maybePanic(err)
return c
}
示例13: adaptContainerSettings
// adaptContainerSettings is called during container creation to modify any
// settings necessary in the HostConfig structure.
func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConfig, adjustCPUShares bool) error {
if hostConfig == nil {
return nil
}
if hostConfig.CPUShares < 0 {
logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, windowsMinCPUShares)
hostConfig.CPUShares = windowsMinCPUShares
} else if hostConfig.CPUShares > windowsMaxCPUShares {
logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares)
hostConfig.CPUShares = windowsMaxCPUShares
}
return nil
}
示例14: createContainer
// createContainer creates a new container using the specified
// options. Per the docker API, the created container is not running
// and must be started explicitly. Note that the passed-in hostConfig
// will be augmented with the necessary settings to use the network
// defined by l.createNetwork().
func createContainer(l *LocalCluster, containerConfig container.Config, hostConfig container.HostConfig, containerName string) (*Container, error) {
hostConfig.NetworkMode = container.NetworkMode(l.networkID)
// Disable DNS search under the host machine's domain. This can
// catch upstream wildcard DNS matching and result in odd behavior.
hostConfig.DNSSearch = []string{"."}
resp, err := l.client.ContainerCreate(&containerConfig, &hostConfig, nil, containerName)
if err != nil {
return nil, err
}
return &Container{
id: resp.ID,
name: containerName,
cluster: l,
}, nil
}
示例15: OneShot
// OneShot runs a container, expecting it to successfully run to completion
// and die, after which it is removed. Not goroutine safe: only one OneShot
// can be running at once.
// Adds the same binds as the cluster containers (certs, binary, etc).
func (l *LocalCluster) OneShot(
ref string,
ipo types.ImagePullOptions,
containerConfig container.Config,
hostConfig container.HostConfig,
name string,
) error {
if err := pullImage(l, ref, ipo); err != nil {
return err
}
hostConfig.VolumesFrom = []string{l.vols.id}
container, err := createContainer(l, containerConfig, hostConfig, name)
if err != nil {
return err
}
l.oneshot = container
defer func() {
if err := l.oneshot.Remove(); err != nil {
log.Errorf("ContainerRemove: %s", err)
}
l.oneshot = nil
}()
if err := l.oneshot.Start(); err != nil {
return err
}
return l.oneshot.Wait()
}