在使用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.
没有发现设置远程主机的选项,最后一句让运行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/
可以看到-H, --host list Daemon socket(s) to connect to
选项,就它了。
docker连接远程主机的方式有两种:
tcp://IP:Port
,比如tcp://192.168.1.8:2345
2345
podman system service --time=0 tcp:0.0.0.0:2345
如果是docker需要修改docker服务文件,笔者使用的ubuntu系统,路径为:
/lib/systemd/system/docker.service
,在ExecStart的命令行中添加
-H tcp://0.0.0.0:2345
参见笔者前面的博文Goland使用远程容器进行go开发调试、Goland连接https的Docker远程服务
ssh://账号@IP
,比如ssh://[email protected]
,在使用SSH的方式时会要求输入密码命令:
docker-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
如果任何一个容器停止,则停止所有容器。没有此项则为手动停止容器。命令:
docker-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
,然后编排可以不指定主机了。
上架:
docker-compose -f compose.yml -p srv up --remove-orphans -d
下架:
docker-compose -f compose.yml -p srv down --rmi all --remove-orphans --volumes
欢迎点赞收藏!