• 使用docker-compose命令行向远程主机编排容器


    在使用Goland的时候,可以直接使用它集成的功能来向远程主机部署容器,但是如果直接使用命令行的方式该如何操作?查看help:

    $ docker-compose --help
    
    Usage:  docker compose [OPTIONS] COMMAND
    
    Docker Compose
    
    Options:
          --ansi string                Control when to print ANSI control
                                       characters ("never"|"always"|"auto")
                                       (default "auto")
          --compatibility              Run compose in backward compatibility mode
          --env-file string            Specify an alternate environment file.
      -f, --file stringArray           Compose configuration files
          --profile stringArray        Specify a profile to enable
          --project-directory string   Specify an alternate working directory
                                       (default: the path of the Compose file)
      -p, --project-name string        Project name
    
    Commands:
      build       Build or rebuild services
      convert     Converts the compose file to platform's canonical format
      cp          Copy files/folders between a service container and the local filesystem
      create      Creates containers for a service.
      down        Stop and remove containers, networks
      events      Receive real time events from containers.
      exec        Execute a command in a running container.
      images      List images used by the created containers
      kill        Force stop service containers.
      logs        View output from containers
      ls          List running compose projects
      pause       Pause services
      port        Print the public port for a port binding.
      ps          List containers
      pull        Pull service images
      push        Push service images
      restart     Restart containers
      rm          Removes stopped service containers
      run         Run a one-off command on a service.
      start       Start services
      stop        Stop services
      top         Display the running processes
      unpause     Unpause services
      up          Create and start containers
      version     Show the Docker Compose version information
    
    Run 'docker compose COMMAND --help' for more information on a command.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    没有发现设置远程主机的选项,最后一句让运行docker compose COMMAND --help获取更多信息,Windows下笔者没有安装docker命令,可以在远程Linux下查看:

    $ docker compose --help
    
    Usage:  docker [OPTIONS] COMMAND
    
    A self-sufficient runtime for containers
    
    Options:
          --config string      Location of client config files (default "/root/.docker")
      -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
      -D, --debug              Enable debug mode
      -H, --host list          Daemon socket(s) to connect to
      -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
          --tls                Use TLS; implied by --tlsverify
          --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
          --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
          --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
          --tlsverify          Use TLS and verify the remote
      -v, --version            Print version information and quit
    
    Management Commands:
      builder     Manage builds
      config      Manage Docker configs
      container   Manage containers
      context     Manage contexts
      image       Manage images
      manifest    Manage Docker image manifests and manifest lists
      network     Manage networks
      node        Manage Swarm nodes
      plugin      Manage plugins
      secret      Manage Docker secrets
      service     Manage services
      stack       Manage Docker stacks
      swarm       Manage Swarm
      system      Manage Docker
      trust       Manage trust on Docker images
      volume      Manage volumes
    
    Commands:
      attach      Attach local standard input, output, and error streams to a running container
      build       Build an image from a Dockerfile
      commit      Create a new image from a container's changes
      cp          Copy files/folders between a container and the local filesystem
      create      Create a new container
      diff        Inspect changes to files or directories on a container's filesystem
      events      Get real time events from the server
      exec        Run a command in a running container
      export      Export a container's filesystem as a tar archive
      history     Show the history of an image
      images      List images
      import      Import the contents from a tarball to create a filesystem image
      info        Display system-wide information
      inspect     Return low-level information on Docker objects
      kill        Kill one or more running containers
      load        Load an image from a tar archive or STDIN
      login       Log in to a Docker registry
      logout      Log out from a Docker registry
      logs        Fetch the logs of a container
      pause       Pause all processes within one or more containers
      port        List port mappings or a specific mapping for the container
      ps          List containers
      pull        Pull an image or a repository from a registry
      push        Push an image or a repository to a registry
      rename      Rename a container
      restart     Restart one or more containers
      rm          Remove one or more containers
      rmi         Remove one or more images
      run         Run a command in a new container
      save        Save one or more images to a tar archive (streamed to STDOUT by default)
      search      Search the Docker Hub for images
      start       Start one or more stopped containers
      stats       Display a live stream of container(s) resource usage statistics
      stop        Stop one or more running containers
      tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
      top         Display the running processes of a container
      unpause     Unpause all processes within one or more containers
      update      Update configuration of one or more containers
      version     Show the Docker version information
      wait        Block until one or more containers stop, then print their exit codes
    
    Run 'docker COMMAND --help' for more information on a command.
    
    To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    可以看到-H, --host list Daemon socket(s) to connect to选项,就它了。

    docker连接远程主机的方式有两种:

    1. 使用TCP套接字
      TCP套接字的格式为:tcp://IP:Port,比如tcp://192.168.1.8:2345
      这种方式需要先在远程主机开启容器服务,并监听指定端口,比如2345
    podman system service --time=0 tcp:0.0.0.0:2345
    
    • 1

    如果是docker需要修改docker服务文件,笔者使用的ubuntu系统,路径为:
    /lib/systemd/system/docker.service,在ExecStart的命令行中添加

    -H tcp://0.0.0.0:2345
    
    • 1

    参见笔者前面的博文Goland使用远程容器进行go开发调试Goland连接https的Docker远程服务

    1. 使用SSH
      SSH的格式为:ssh://账号@IP,比如ssh://witton@192.168.1.8,在使用SSH的方式时会要求输入密码

    一、编排上架

    命令:

    docker-compose -H "tcp://192.168.1.8:2345" -f compose.yml -p srv up --remove-orphans -d
    
    • 1

    参数解释:

    • -H "tcp://192.168.1.8:2345" 指定连接远程主机的方式、地址与端口
    • -f compose.yml 指定需要使用的编排文件
    • -p srv 指定项目名称
    • up 上架
    • --remove-orphans 移除孤立项。移除Compose文件中未定义的服务的容器。
    • -d 后台执行该服务

    编排上架还可以使用以下选项:

    • --timeout以秒为单位设置终止容器时的超时。容器首先会收到SIGTERM,然后在指定的超时后收到SIGKILL。
    • --exit-code-from返回所选服务容器的退出代码。在指定服务中的容器停止时停止所有容器。
    • --scale设置容器数。
    • --always-recreate-deps重新创建依赖的容器。
    • --renew-anon-volumes重新创建匿名卷,而不是从以前的容器中检索数据。
    • --no-start创建服务后不启动它们。如果不指定此项与--no-deps,则默认启动指定服务以及链接的服务。
    • --no-deps不启动链接的服务。如果不指定此项与--no-start,则默认启动指定服务以及链接的服务。
    • --detach在分离模式下运行。如果没有此项与--attach-dependecies则不附加到依赖的容器,而是附加到所有已启动的容器。
    • --attach-dependecies附加到所有启动的容器及依赖项。如果没有此项与--detach则不附加到依赖的容器,而是附加到所有已启动的容器。
    • --force-recreate即使容器的配置与镜像未更改,也重新创建所有容器。没有此项与--no-recreate则为如果配置或镜像更改,则替换容器。
    • --no-recreate即使配置更改,也不重新创建现有容器。没有此项与--force-recreate则为如果配置或镜像更改,则替换容器。
    • --no-build不构建镜像,如果后缺失则停止。没有此项与--build则构建不可用的镜像。
    • --build在启动容器之前构建镜像。没有此项与--no-build则构建不可用的镜像。
    • --abort-on-container-exit如果任何一个容器停止,则停止所有容器。没有此项则为手动停止容器。

    二、编排下架

    命令:

    docker-compose -H "tcp://192.168.1.8:2345" -f compose.yml -p srv down --rmi all --remove-orphans --volumes
    
    • 1

    参数解释:

    • -H "tcp://192.168.1.8:2345" 指定连接远程主机的方式、地址与端口
    • -f compose.yml 指定需要使用的编排文件
    • -p srv 指定项目名称
    • down下架
    • --rmi all移除全部镜像。如果仅移除没有自定义标记的镜像则使用--rmi local
    • --remove-orphans移除孤立项。移除Compose文件中未定义的服务的容器
    • --volumes在终止时移除所有卷

    还有一种指定主机的方式则是使用环境变量DOCKER_HOST,比如设置DOCKER_HOST=tcp://192.168.1.8:2345,然后编排可以不指定主机了。
    上架:

    docker-compose -f compose.yml -p srv up --remove-orphans -d
    
    • 1

    下架:

    docker-compose -f compose.yml -p srv down --rmi all --remove-orphans --volumes
    
    • 1

    欢迎点赞收藏!

  • 相关阅读:
    IT售前“楠“知识之这!就是售前-尚文网络xUP楠哥
    【CSDN 每日一练 ★☆☆】【双指针】删除有序数组中的重复项
    Caused by: java.lang.IllegalStateException
    产品思维训练 | 亚马逊流量7-8月网站访客流量下降,请分析原因
    《算法竞赛进阶指南》 双端队列
    聊聊spring的TransactionSynchronizationAdapter
    C++ string类介绍以及模拟实现
    #{} ${} 解析
    Vue使用for循环加Promise.all来发送多个请求,并且等到所有请求结束后处理数据
    Android MediaCodec硬件解码视频播放
  • 原文地址:https://blog.csdn.net/witton/article/details/132875793