使用docker-compose命令行向远程主机编排容器
在使用Goland的时候,可以直接使用它集成的功能来向远程主机部署容器,但是如果直接使用命令行的方式该如何操作?查看help:
1$ docker-compose --help
2
3Usage: docker compose [OPTIONS] COMMAND
4
5Docker Compose
6
7Options:
8 --ansi string Control when to print ANSI control
9 characters ("never"|"always"|"auto")
10 (default "auto")
11 --compatibility Run compose in backward compatibility mode
12 --env-file string Specify an alternate environment file.
13 -f, --file stringArray Compose configuration files
14 --profile stringArray Specify a profile to enable
15 --project-directory string Specify an alternate working directory
16 (default: the path of the Compose file)
17 -p, --project-name string Project name
18
19Commands:
20 build Build or rebuild services
21 convert Converts the compose file to platform's canonical format
22 cp Copy files/folders between a service container and the local filesystem
23 create Creates containers for a service.
24 down Stop and remove containers, networks
25 events Receive real time events from containers.
26 exec Execute a command in a running container.
27 images List images used by the created containers
28 kill Force stop service containers.
29 logs View output from containers
30 ls List running compose projects
31 pause Pause services
32 port Print the public port for a port binding.
33 ps List containers
34 pull Pull service images
35 push Push service images
36 restart Restart containers
37 rm Removes stopped service containers
38 run Run a one-off command on a service.
39 start Start services
40 stop Stop services
41 top Display the running processes
42 unpause Unpause services
43 up Create and start containers
44 version Show the Docker Compose version information
45
46Run 'docker compose COMMAND --help' for more information on a command.
没有发现设置远程主机的选项,最后一句让运行docker compose COMMAND --help
获取更多信息,Windows下笔者没有安装docker命令,可以在远程Linux下查看:
1$ docker compose --help
2
3Usage: docker [OPTIONS] COMMAND
4
5A self-sufficient runtime for containers
6
7Options:
8 --config string Location of client config files (default "/root/.docker")
9 -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")
10 -D, --debug Enable debug mode
11 -H, --host list Daemon socket(s) to connect to
12 -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
13 --tls Use TLS; implied by --tlsverify
14 --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
15 --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
16 --tlskey string Path to TLS key file (default "/root/.docker/key.pem")
17 --tlsverify Use TLS and verify the remote
18 -v, --version Print version information and quit
19
20Management Commands:
21 builder Manage builds
22 config Manage Docker configs
23 container Manage containers
24 context Manage contexts
25 image Manage images
26 manifest Manage Docker image manifests and manifest lists
27 network Manage networks
28 node Manage Swarm nodes
29 plugin Manage plugins
30 secret Manage Docker secrets
31 service Manage services
32 stack Manage Docker stacks
33 swarm Manage Swarm
34 system Manage Docker
35 trust Manage trust on Docker images
36 volume Manage volumes
37
38Commands:
39 attach Attach local standard input, output, and error streams to a running container
40 build Build an image from a Dockerfile
41 commit Create a new image from a container's changes
42 cp Copy files/folders between a container and the local filesystem
43 create Create a new container
44 diff Inspect changes to files or directories on a container's filesystem
45 events Get real time events from the server
46 exec Run a command in a running container
47 export Export a container's filesystem as a tar archive
48 history Show the history of an image
49 images List images
50 import Import the contents from a tarball to create a filesystem image
51 info Display system-wide information
52 inspect Return low-level information on Docker objects
53 kill Kill one or more running containers
54 load Load an image from a tar archive or STDIN
55 login Log in to a Docker registry
56 logout Log out from a Docker registry
57 logs Fetch the logs of a container
58 pause Pause all processes within one or more containers
59 port List port mappings or a specific mapping for the container
60 ps List containers
61 pull Pull an image or a repository from a registry
62 push Push an image or a repository to a registry
63 rename Rename a container
64 restart Restart one or more containers
65 rm Remove one or more containers
66 rmi Remove one or more images
67 run Run a command in a new container
68 save Save one or more images to a tar archive (streamed to STDOUT by default)
69 search Search the Docker Hub for images
70 start Start one or more stopped containers
71 stats Display a live stream of container(s) resource usage statistics
72 stop Stop one or more running containers
73 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
74 top Display the running processes of a container
75 unpause Unpause all processes within one or more containers
76 update Update configuration of one or more containers
77 version Show the Docker version information
78 wait Block until one or more containers stop, then print their exit codes
79
80Run 'docker COMMAND --help' for more information on a command.
81
82To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
可以看到-H, --host list Daemon socket(s) to connect to
选项,就它了。
docker连接远程主机的方式有两种:
- 使用TCP套接字 TCP套接字的格式为:
tcp://IP:Port
,比如tcp://192.168.1.8:2345
这种方式需要先在远程主机开启容器服务,并监听指定端口,比如2345
1podman system service --time=0 tcp:0.0.0.0:2345
如果是docker需要修改docker服务文件,笔者使用的ubuntu系统,路径为:
/lib/systemd/system/docker.service
,在ExecStart的命令行中添加
1-H tcp://0.0.0.0:2345
参见笔者前面的博文 Goland使用远程容器进行go开发调试、 Goland连接https的Docker远程服务
- 使用SSH SSH的格式为:
ssh://账号@IP
,比如ssh://witton@192.168.1.8
,在使用SSH的方式时会要求输入密码
一、编排上架
命令:
1docker-compose -H "tcp://192.168.1.8:2345" -f compose.yml -p srv up --remove-orphans -d
参数解释:
-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
如果任何一个容器停止,则停止所有容器。没有此项则为手动停止容器。
二、编排下架
命令:
1docker-compose -H "tcp://192.168.1.8:2345" -f compose.yml -p srv down --rmi all --remove-orphans --volumes
参数解释:
-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
,然后编排可以不指定主机了。
上架:
1docker-compose -f compose.yml -p srv up --remove-orphans -d
下架:
1docker-compose -f compose.yml -p srv down --rmi all --remove-orphans --volumes
欢迎点赞收藏!
- 原文作者:Witton
- 原文链接:https://wittonbell.github.io/posts/2023/2023-09-14-使用docker-compose命令行向远程主机编排容器/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。