Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Connect to another host with your docker client, without modifying your local Docker installation

Run commands on remote Docker host

This is how to connect to another host with your docker client, without modifying your local Docker installation or when you don't have a local Docker installation.

Enable Docker Remote API

First be sure to enable the Docker Remote API on the remote host.

This can easily be done with a container.
For HTTP connection use jarkt/docker-remote-api.
For HTTPS connection use kekru/docker-remote-api-tls.

You can also configure the Docker engine to expose the remote API. Read Enable Docker Remote API with TLS client verification for more information.

Download docker client

If you don't have a local Docker installation, you need to download the docker client (= docker cli), which is a simple executable.
And then add it to your PATH variable.

Here are some ways how to get the executable.
You only need one of the steps for you OS, not all:

HTTPS connection configuration

Docker's Remote API client authentication works with certificates.
See Protect the Docker daemon socket or my Enable Docker Remote API with TLS client verification on how to create server and client certificates.

For the following examples copy ca.pem (CA certificate), cert.pem (client certificate) and key.pem (client's private key) in /home/me/docker-tls/ or C:\users\me\docker-tls\.

Connect to remote api

Now we will see some ways on how to connect to a docker remote api.

Set alias (Linux and Mac)

For HTTP connection set the following alias:

alias dockerx="docker -H=your-remote-server.org:2375"

For HTTPS connection set the following alias:

alias dockerx="docker \
  --tlsverify \
  -H=your-remote-server.org:2376 \
  --tlscacert=/home/me/docker-tls/ca.pem \
  --tlscert=/home/me/docker-tls/cert.pem \
  --tlskey=/home/me/docker-tls/key.pem"

Now you can run commands on the remote machine with dockerx instead of docker.

Example:

dockerx ps

Create .bat file (Windows)

Create a file dockerx.bat.
For HTTP connection the content of the bat file should be:

docker -H=your-remote-server.org:2375 %*

For HTTPS connection the content of the bat file should be:

docker ^
  --tlsverify ^
  -H=your-remote-server.org:2376 ^
  --tlscacert=C:\users\me\docker-tls\ca.pem ^
  --tlscert=C:\users\me\docker-tls\cert.pem ^
  --tlskey=C:\users\me\docker-tls\key.pem %*

(If this does not work remove the carets (^) and the line breaks)

Now you can run commands on the remote machine with dockerx.bat instead of docker.

Example:

dockerx.bat ps

Set env var (Linux, Mac, Windows)

You can set environment vars to define the docker remote api that should be connected to.

For HTTP connection

# Linux/Mac
export DOCKER_HOST="tcp://your-remote-server.org:2375"

# Windows Powershell
$env:DOCKER_HOST="tcp://your-remote-server.org:2375"

For HTTPS connection

# Linux/Mac
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://your-remote-server.org:2376"
export DOCKER_CERT_PATH="/home/me/docker-tls"

# Windows Powershell
$env:DOCKER_TLS_VERIFY="1"
$env:DOCKER_HOST="tcp://your-remote-server.org:2376"
$env:DOCKER_CERT_PATH="C:\users\me\docker-tls"

Be sure that your DOCKER_CERT_PATH directory contains the following files:

  • ca.pem (CA certificate)
  • cert.pem (client certificate)
  • key.pem (client's private key)

Now any docker command will run against the remote api

docker ps

Do switch back to local docker, unset the env vars:

# Linux/Mac
unset DOCKER_HOST
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH

# Windows Powershell
Remove-Item env:DOCKER_HOST
Remove-Item env:DOCKER_TLS_VERIFY
Remove-Item env:DOCKER_CERT_PATH

Reuse SSH connection (Linux, Mac, Windows(?))

If you already added an SSH public key to your remote server, then you can use this ssh credentials for your docker connection, too. You don't need to configure the remote api on the server for this approach.
(Should work on Windows, but I did only test on Linux yet)

Set the env var to a ssh address:

# Linux/Mac
export DOCKER_HOST="ssh://username@your-remote-server.org"

# Windows Powershell
$env:DOCKER_HOST="ssh://username@your-remote-server.org"

Now any docker command will run against the remote api

docker ps

Do switch back to local docker, unset the env vars:

# Linux/Mac
unset DOCKER_HOST

# Windows Powershell
Remove-Item env:DOCKER_HOST

Docker Context (Linux, Mac, Windows)

Since Docker 19.03 there is the docker context command. You can define multiple remote servers and switch between them.

Create a context for HTTPS
(Change paths for Windows)

docker context create example-server \
  --description "connection to example server" \
  --docker "host=tcp://your-remote-server.org:2376, \
     ca=/home/me/docker-tls/ca.pem, \
     cert=/home/me/docker-tls/cert.pem, \
     key=/home/me/docker-tls/key.pem"

(For HTTP connection remove ca, cert and key and switch port to 2375. For SSH connection use ssh address)

Now you can call the remote server with:

docker --context example-server ps

Or choose the context and then all following command will call the remote server

docker context use example-server
docker ps
@BartRobeyns

This comment has been minimized.

Copy link

BartRobeyns commented Feb 18, 2018

The Install Docker from binaries link no longer provides info for installing the Windows Docker Client by itself. Instead, you can go to https://download.docker.com/win/static/stable/x86_64/, and grab the latest zip-file that contains the Docker Client, docker.exe.

@kekru

This comment has been minimized.

Copy link
Owner Author

kekru commented Feb 22, 2018

Thanks @BartRobeyns, I updated the text

@solvingj

This comment has been minimized.

Copy link

solvingj commented Mar 9, 2018

This is what happens when i run docker.bat ps:

U:\>docker.bat ps
U:\>docker -H=TEMP-HYPERV:2375 ps
U:\>docker -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 ps
U:\>docker -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 ps
U:\>docker -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 -H=TEMP-HYPERV:2375 ps

and it goes on to infinity until i cancel.

Basically, it happens if you name the script docker.bat specifically, AND run it in the folder where it exists. Name it something else, or run it from another directory.

@kekru

This comment has been minimized.

Copy link
Owner Author

kekru commented Mar 19, 2018

Haha thats crazy. Thanks @solvingj, I updated the text to use dockerx.bat instead

@wxgeorge

This comment has been minimized.

Copy link

wxgeorge commented Mar 27, 2018

You also could set DOCKER_HOST in your session ...

@ghost

This comment has been minimized.

Copy link

ghost commented Jan 16, 2019

root@dfmix-easycards01-pltf:/etc/gitlab-runner# docker run -p 80:2375 -v /var/run/docker.sock:/var/run/docker.sock jarkt/docker-remote-api

Hangs and no port is opened

@kekru

This comment has been minimized.

Copy link
Owner Author

kekru commented Jan 17, 2019

@fusionfai
Try run with the -d option to make it run in background:
docker run -d -p 80:2375 -v /var/run/docker.sock:/var/run/docker.sock jarkt/docker-remote-api
And make sure, you're connecting to port 80 instead of 2375 by changing the port in -H, when running a docker command

@koganzjo

This comment has been minimized.

Copy link

koganzjo commented May 14, 2019

@fusionfai
if you set port 80 instead of 2375 in the dockerx.bat I think that all works

@omidkrad

This comment has been minimized.

Copy link

omidkrad commented Jul 28, 2019

Thanks! This worked great, I only needed to use port 2375 instead of 80.
docker run -d --name docker-remote-api -p 2375:2375 -v /var/run/docker.sock:/var/run/docker.sock jarkt/docker-remote-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.