How to make docker listening to unix and TCP socket under centos with systemd

Viewed 7876

I have installed docker (v17.06.2-ce) under CentOS using systemd. Docker works fine and listens to the unix socket. Now i would like to have docker to listen to the Unix socket and to the TCP socket 2375. Since this is an in-house development machine security is no issue.

I scanned the internet found several articles but still got some questions.

My understanding is that I have systemd file (docker.service) which starts the dockerd without any parameters. ExecStart=/usr/bin/dockerd

Then there is the file demon.json here I can list what I will listen to. My question is what to enter here. It could be.

{
   "hosts": [  "unix:///var/run/docker.sock",
                "tcp://0.0.0.0:2375"
            ]
}

Or is it something like this for socket activation?

{
       "hosts": [  "unix:///var/run/docker.sock",
                    "fd://"
                ]
    }

Then the second thing I found out is to prepare systemd socket by providing a file docker.sockst like this

[Unit] Description=Docker Socket for the API
PartOf=docker.service 

[Socket] 
ListenStream=tcp://0.0.0.0:2375 
SocketMode=0660 
SocketUser=root 
SocketGroup=docker 

[Install] 
WantedBy=sockets.target

But this will define only one TCP socket. According to one article then docker will respond to TCP port but no longer to the unix socket.

It would be nice if someone could point out the details.

3 Answers
  1. cd /lib/systemd/system/
  2. vim docker-tcp.socket
  3. paste thie to docker-tcp.socket

    [Unit]
    Description=Docker Socket for the API
    PartOf=docker.service
    
    [Socket]
    ListenStream=2375
    
    BindIPv6Only=both
    Service=docker.service
    
    [Install]
    WantedBy=sockets.target
    
  4. systemctl daemon-reload

  5. systemctl stop docker.service
  6. systemctl enable docker-tcp.socket
  7. systemctl start docker-tcp.socket
  8. systemctl start docker.service
  9. verify 2375 port is opened docker -H 127.0.0.1 ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

I actually just posted this answer to an open issue on Github for PhotonOS. I created a gist with the instructions doc markdown as well as the equivalent shell script.

It allows for maintaining both local unix socket as well as remote TCP-based access to the API. Unlike most instructions, it follows the Docker supported method of creating the docker.socket service and binding it to docker service as a dependency, rather than hard-coding either/or TCP or unix fd sock on the command line, or hacking any system files that get overwritten at every upgrade.

Gist is at: https://git.io/fjhhO

Related