If you are a system administrator and manage multiple Docker hosts, then you will need to connect to each Docker host and run a command to manage Docker. This can be a time-consuming process. This is where Docker daemon comes into the picture. Docker daemon allows you to connect to a remote docker host over TCP. This way, you can manage multiple Docker hosts from your local system without having to log in to each Docker host individually.

In this post, we will show you how to configure Docker daemon to manage the Docker host over TCP.

Step 1 – Install Docker CE

First, install all required dependencies with the following command:

apt-get install git apt-transport-https ca-certificates curl software-properties-common -y

Next, add the Docker GPG key with the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add –

Next, add the Docker repository with the following command:

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Once the repository has been added, install Docker and Docker compose with the following command:

apt-get install docker-ce docker-compose -y

Once the installation is completed, verify the Docker version with the following command:

docker --version

You should see the following output:

Docker version 20.10.5, build 55c4c88

Step 2 – Configure Docker Daemon

First, you will need to create a directory to store the Docker daemon configuration file. You can create it with the following command:

mkdir -p /etc/systemd/system/docker.service.d

Next, create a new file to store the daemon options.

nano /etc/systemd/system/docker.service.d/options.conf

Add the following lines:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375

Save and close the file, then reload the systemd daemon to apply the changes:

systemctl daemon-reload

Next, restart the Docker service to apply the changes:

systemctl restart docker

At this point, the Docker daemon is configured and listening on port 2375. You can check it with the following command:

ps aux | grep dockerd

You should get the following output:

root       48453  1.2  2.4 1005080 98520 ?       Ssl  00:58   0:00 /usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
root       48775  0.0  0.0  12108   992 pts/0    S+   00:58   0:00 grep --color=auto dockerd

Step 3 – Configure Local System to Connect to the Docker Daemon

Before starting, make sure Docker and Docker compose are installed on your local system. Now, you will need to configure your local system to connect to the Docker daemon on the remote Docker host.

You can use a single one-liner to direct connect to the Docker daemon and run command on the remote Docker host. You can use the DOCKER_HOST variable to define the Docker daemon address.

For example, run the following command to run the docker info command on the remote Docker host:

DOCKER_HOST=tcp://remote-docker-host-ip:2375 docker info

Or

docker -H tcp://remote-docker-host-ip:2375 docker info

You should get the output of the docker info command as below:

Containers: 2
Images: 3
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Execution Driver: 
Kernel Version: 4.18.0-193.6.3.el8_2.x86_64
Operating System: CentOS Linux 8 (Core)
CPUs: 2
Total Memory: 3.846 GiB
Name: centos8
ID: S56P:VPIW:CMGZ:GAFN:YZNG:22CE:OBY3:SKEW:JAMT:DLD4:FG5K:QXYR
Http Proxy: 
Https Proxy: 
No Proxy: 
Labels:

You can also run the docker-compose command on the remote host as shown below:

docker-compose -H tcp://remote-docker-host-ip:2375 --version

You can also set DOCKER_HOST variable in your .bashrc file. So you don’t need to specify every time when executing docker or docker-compose command.

You can set it with the following command:

echo "export DOCKER_HOST=tcp://remote-docker-host-ip:2375" >> ~/.bashrc

Next, activate the configuration with the following command:

source ~/.bashrc

Now, you can manage remote Docker host by just running the docker and docker-compose command locally.

For example, run the following command to check the Docker version on the remote host:

docker --version

Conclusion

In the above guide, you learned how to configure Docker daemon to connect the remote Docker host over TCP and manage it from the local system. I hope this will make your day-to-day Docker tasks much easier. Start using Docker daemon today on a VPS from Atlantic.Net!