Home  >  Article  >  Backend Development  >  How to run docker mount volumes using Docker Engine SDK and Golang

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

PHPz
PHPzforward
2024-02-09 15:24:08854browse

如何使用 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,
     "",
   )

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,
      "",
   )

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete