Use the docker-compose command line to orchestrate containers to remote hosts

When using Goland, you can directly use its integrated functions to deploy containers to remote hosts, but what should you do if you directly use the command line? View 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.

There is no option to set the remote host. The last sentence is to run docker compose COMMAND --help to get more information. I have not installed the docker command under Windows. You can view it under remote 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 management 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/

You can see the -H, --host list Daemon socket(s) to connect to option, that’s it.

There are two ways for docker to connect to a remote host:

  1. Using TCP sockets
    The format of TCP socket is: tcp://IP:Port, such as tcp://192.168.1.8:2345
    This method requires first opening the container service on the remote host and listening to the specified port, such as 2345
podman system service --time=0 tcp:0.0.0.0:2345

If docker needs to modify the docker service file, the ubuntu system I use, the path is:
/lib/systemd/system/docker.service, add it to the command line of ExecStart

-H tcp://0.0.0.0:2345

See the author’s previous blog posts: Goland uses remote containers for go development and debugging, and Goland connects to https Docker remote services

  1. Use SSH
    The format of SSH is: ssh://account@IP, such as ssh://[email protected]. When using SSH, you will be asked to enter a password.

1. Arrangement and listing

Order:

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

Parameter explanation:

  • -H "tcp://192.168.1.8:2345" Specifies the method, address and port to connect to the remote host
  • -f compose.yml specifies the composition file to be used
  • -p srv specifies the project name
  • up put on shelves
  • --remove-orphans Remove orphans. Remove containers for services that are not defined in the Compose file.
  • -d Execute the service in the background

The following options are also available for orchestration listings:

  • --timeoutSet the timeout in seconds when terminating the container. The container first receives SIGTERM and then SIGKILL after the specified timeout.
  • --exit-code-fromReturns the exit code of the selected service container. Stops all containers when containers in the specified service are stopped.
  • --scaleSet the number of containers.
  • --always-recreate-depsRecreate dependent containers.
  • --renew-anon-volumesRecreate anonymous volumes instead of retrieving data from the previous container.
  • --no-startDo not start services after creating them. If this item and --no-deps are not specified, the specified service and linked services will be started by default.
  • --no-depsDo not start linked services. If this item and --no-start are not specified, the specified service and linked services will be started by default.
  • --detachRun in detached mode. Without this item and --attach-dependecies, it will not be attached to the dependent container, but will be attached to all started containers.
  • --attach-dependeciesAttach to all started containers and dependencies. Without this item and --detach, it will not be attached to the dependent container, but will be attached to all started containers.
  • --force-recreateRecreate all containers even if the container’s configuration and image have not changed. Without this and --no-recreate, the container will be replaced if the configuration or image changes.
  • --no-recreateDo not recreate existing containers even if configuration changes. Without this and --force-recreate, the container will be replaced if the configuration or image changes.
  • --no-buildDo not build the image, and stop if it is missing. Without this item and --build, an unusable image will be built.
  • --buildBuild the image before starting the container. Without this item and --no-build, an unusable image will be built.
  • --abort-on-container-exitIf any container stops, stop all containers. Without this item, the container is stopped manually.

2. Arrangement for removal

Order:

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

Parameter explanation:

  • -H "tcp://192.168.1.8:2345" Specifies the method, address and port to connect to the remote host
  • -f compose.yml specifies the composition file to be used
  • -p srv specifies the project name
  • downRemoved
  • --rmi allRemove all images. To remove only images without custom tags use --rmi local
  • --remove-orphansRemove orphans. Remove containers for undefined services in Compose files
  • --volumesRemove all volumes on termination

Another way to specify the host is to use the environment variable DOCKER_HOST, for example, set DOCKER_HOST=tcp://192.168.1.8:2345, and then the host can be arranged without specifying it.
On shelves:

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

Removal:

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

Welcome to like and collect!