How to run docker mount volumes using Docker Engine SDK and Golang

PHPz
Release: 2024-02-09 15:24:08
forward
915 people have browsed it

如何使用 Docker 引擎 SDK 和 Golang 运行 docker 安装卷

php Editor Xigua today will introduce to you how to use the Docker engine SDK and Golang to run the docker installation volume. Docker is a popular containerization platform, and the Docker Engine SDK is an API library for interacting with the Docker Engine. Golang is a powerful programming language that can be used in conjunction with the Docker Engine SDK to achieve more flexible container management. This article will introduce in detail how to use Docker engine SDK and Golang to create, manage and run docker installation volumes, allowing you to better utilize Docker for application development and deployment. let's start!

Question content

I am looking at the docker engine SDK documentation (https://docs.docker.com/engine/api/sdk/) related to running Docker with Golang I want to run a container (well documented) but I can't find how to mount a volume while running the container.

My idea is to use the Docker SDK to run the equivalent command: docker run -v $PWD:/tmp myimage But the Golang os exec library is not executed.

is it possible?

Workaround

The examples section contains most of what you need:

https://docs.docker.com/engine/api/sdk/examples/#run-a-container

It’s important to remember docker run ... It’s a bit of both

  1. Create a container
  2. Start a container

docker run -v is the abbreviation of docker run --mount type=bind,source="$(pwd)"/target,target=/app

    resp, err := cli.containercreate(ctx, &container.config{
        image: "alpine",
        cmd:   []string{"echo", "hello world",},
      },
      &container.hostconfig{
        mounts: []mount.mount{
          {
             type: mount.typebind,
             source: "/local/dir",
             target: "/app",
          },
        },
     },
     nil,
     "",
   )
Copy after login

If you only want one file

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: "alpine",
        Cmd:   []string{"echo", "hello world",},
      },
      &container.HostConfig{
        Binds: []string{
          "/local/dir/file.txt:/app/file.txt",
        },
      },
      nil,
      "",
   )
Copy after login

Related:

The above is the detailed content of How to run docker mount volumes using Docker Engine SDK and Golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template