How to set up a Docker Host
the simple way
Let
client = Docker client
server = Docker Host
So that
client is your computer (Mac, Linux, *BSD or other) where you have docker application installed, and you do your software development. Server is a Linux computer/server where you have dockerd and containerd and sshd installed and running.
However, if you do not have a docker installed on the client or server, please follow the next installation example. Otherwise, please skip to the configuration session.
Client install example (macOS)
brew install docker
Server install example (Arch Linux)
sudo su -
pacman -Syu
pacman -S docker, openssh
# to start services automatically:
systemctl enable sshd
systemctl enable docker
# start sshd
systemctl start sshd
exit
Additional docker.service configuration can be found here
Configuration
The Client computer has to connect to the Docker server via TCP. We’ll use SSH protocol to establish a connection between client and server, so that the client can manage the Docker host remotely and use its resources.
## server
Docker needs to listen to the network, so we have to change docker service configuration.
vim /lib/systemd/system/docker.service
Changing the ExecStart configuration
ExecStart=/usr/bin/dockerd -H tcp://192.168.1.10 -H fd://
Then start docker service.
sudo su -
# reload docker configuration
systemctl daemon-reload
# start docker
systemctl start docker.service
exit
Client
The client has to be able to connect to the server via ssh. To better manage ssh sessions, (optionally) configure ssh key login.
ssh
To configure ssh key on the client-side:
ssh-keygen
then send the ssh public key to the server, by copying the public key from client to server.
ssh-copy-id <user>@192.168.1.10
without mention the -i parameter in the ssh-copy-id command, it will (by default) assume -i ~/.ssh/id_rsa.pub
docker
Configure docker to use remote HOST resources, by setting DOCKER_HOST
environment variable:
export DOCKER_HOST=ssh://192.168.1.10
To make it persistent:
echo 'export DOCKER_HOST=ssh://192.168.1.10' >> ~/.zshrc
## or, case you use bash
echo 'export DOCKER_HOST=ssh://192.168.1.10' >> ~/.bashrc
Run docker on client to check
docker ps
Note that every deployment to this docker instance will be hosted and its ports published on the remote host.
So, when you run
docker run -it -p 8080:8080 app app
take in account that, port 8080 is published on 192.168.1.10 (in this example)
That’s it!