Why docker command connects to daemon via tcp by default - docker

Docker is configured by default to listen on socket (Ubuntu 18.04).
$ ps aux | grep docker
root 1966 0.2 0.4 1451444 69700 ?
Ssl 09:57 0:01 /usr/bin/dockerd -H fd://
--containerd=/run/containerd/containerd.sock
But when I run docker ps it raises:
$ docker ps
Cannot connect to the Docker daemon at
tcp://127.0.0.1:2375. Is the docker daemon running?
I need to specify:
$ docker -H unix:///var/run/docker.sock ps
CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS
NAMES 7ffd2dc852f2 jwilder/nginx-proxy
"/app/docker-entrypo…" 5 minutes ago Up 5 minutes
0.0.0.0:80->80/tcp nginx-proxy
Then works fine, but why cli wants to connect daemon by default via tcp? How to avoid that?

docker client uses DOCKER_HOST environment value if set otherwise default to unix:///var/run/docker.sock. Check if you have DOCKER_HOST environment variable set?

Related

Cannot connect to the Docker daemon after I set -H tcp://X.X.X.X:2375 in the dockerd arguments

I have launched Jenkins as a Docker container and in the Jenkins dashboard I am configuring the Docker plugin to communicate with the Docker daemon. However, when I add "-H tcp://0.0.0.0:2375" in the Docker daemon and I restart Docker, executing any docker commands results in the message: "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"
This is the first time I am working with Docker and Jenkins and I have spent a lot of time looking for a solution everywhere.
I have modified the /lib/systemd/system/docker.service by replacing the line:
ExecStart=/usr/bin/dockerd
with
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375
Then I restart the Docker daemon:
systemctl daemon-reload
systemctl reload docker
systemctl restart docker.service
I check that the process is listening in the port 2375:
netstat -lntp | grep dockerd
tcp6 0 0 :::2375 :::* LISTEN 10855/dockerd
When I try any docker command I get the following:
docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Docker normally listens on the unix socket /var/run/docker.sock. You have asked it to instead listen on the tcp socket tcp://0.0.0.0:2375, so attempts by the client to connect to the default unix socket will fail.
If you want it to listen to both places, then you need:
dockerd -H tcp://0.0.0.0:2735 -H unix:///var/run/docker.sock
Thank so much, I was forgetting to also add listening on the default UNIX socket /var/run/docker.sock. Now, I can use Jenkins and and its Docker plugin.

Docker, host-OS restart and busy ports

I started learning docker and I use it on Linux ubuntuserver 4.15.0-34-generic. I created container by this command:
sudo docker run -d --privileged --name FBSServer --hostname BACKUPSERVER --restart=always -p 4530:4530 -p 4531:4531 -v /home/adminek/synology:/fbs ferro/backupsystem FBS_Server
As you see container is listening on ports 4530 and 4531. When I ran this command for the first time, everything was OK. The container is running and I can communicate with an app using ports 4530/4531.
But after rebooting the host system the container is in Exited state
root#ubuntuserver:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e77a4729847 ferro/backupsystem "/start.sh FBS_Server" 7 hours ago Exited (128) About a minute ago FBSServer
and I can not start the container. I see this:
root#ubuntuserver:~# docker container start FBSServer
Error response from daemon: driver failed programming external connectivity on endpoint FBSServer (c2afb35a61a18c65e8c306bbf36d4e2c9cdf428aafa386e12281d2d9e714b238): Error starting userland proxy: listen tcp 0.0.0.0:4531: bind: address already in use
Error: failed to start containers: FBSServer
In order to start I have to type these commands:
root#ubuntuserver:~# lsof -i :4530 | grep LISTEN
docker-pr 1328 root 4u IPv6 35086 0t0 TCP *:4530 (LISTEN)
root#ubuntuserver:~# lsof -i :4531 | grep LISTEN
docker-pr 1316 root 4u IPv6 35059 0t0 TCP *:4531 (LISTEN)
Then I kill the processes (in this example 1328 and 1316) and then I can start container by typing:
docker container start FBSServer
So the question number 1:
Why after rebooting host system ports are "busy"?
and number 2:
Why sometimes after rebooting host system I see that the changes in container were not saved? In other words - after reboot and made commands from above container is up, but it is in state without last changes I made a while before reboot the host.
Try restarting your docker service after , sudo /etc/init.d/docker restart it should resolve the problem
Normally if your docker container is starting automatically and allocating some port, you added by mistake the option --restart to start containers automatically.
So If after you restart the docker service, the docker-pr service allocates the port of some container, your container is starting automatically.
After starting the docker, to check if you have some container running use:
docker ps
To stop this container, we can use:
docker stop container_name
If you have more than one container, we can use:
docker stop $(docker ps -a -q)
Normally, if you have some container starting automatically, you set this using docker --restart we need to remove it from the auto restart.
To do this use the following command:
docker update --restart=no container_name
After this, you will able to restart your docker service and you will not found any container starting automatically.

docker swarm - unable to connect to nginx container

I have setup Docker swarm on single node and have created a container on it running nginx server but i am unable to connect to nginx server on specified port. Also, i am not able to attach to container. Any help is much appreciated.
[user#hostname yum.repos.d]$ sudo docker swarm init
[user#hostname yum.repos.d]$ sudo docker info | grep -i "swarm: active"
Swarm: active
[user#hostname yum.repos.d]$
[user#hostname yum.repos.d]$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
xxxxxxxxxxxxxxx * hostname.server.com Ready Active Leader
[user#hostname yum.repos.d]$ sudo docker service create --name web --publish 8080:80 nginx
[user#hostname ~]$ sudo docker ps -a
[sudo] password for user:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxxxxxxxxxx nginx#sha256:ccdb5fdf47709493f9fc5af32478c0d86b3cbee0c306e3f04a0d3e640a50ea2d "nginx -g 'daemon ..." About an hour ago Up About an hour 80/tcp web.1.
[user#hostname ~]$ elinks http://localhost:8080 //says request sent but no response received.
Above elinks command is unable to connect to url.
It works!!
[user#hostname ~]$ sudo docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
xxxxxxxxxxxx web.1 httpd:latest hostname.myserver.com Running Running 3 minutes ago
[user#hostname ~]$
[user#hostname ~]$ elinks http://hostname.mylabserver.com:80
This works and connects to nginx running on port 80.
Thanks!!

Docker Swarm Cluster: daemonize

I'm in the process of setting up a Docker Swarm cluster
Im on this step:
sudo docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
However, it looks like it's not deamonizing it (the program is still holding the screen)
How do I daemonize it?
You need to make sure you run the daemon as a background process using nohup / &, using your example:
sudo nohup docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock &
Note: docker daemon has been deprecated and this should be dockerd now.
This is no different than running any other Linux process in the background: How to run process as background and never die?

How to detect a docker daemon port

I have installed Ubuntu and Docker. I am trying to launch Raik container:
$ DOCKER_RIAK_AUTOMATIC_CLUSTERING=1 DOCKER_RAIK_CLUSTER_SIZE=5 DOCKER_RIAK_BACKEND=leveldb make start-cluster ./bin/start
and get the error message:
It looks like the environment variable DOCKER_HOST has not been set.
The Riak cluster cannot be started unless this has been set
appropriately. For example:
export DOCKER_HOST="tcp://127.0.0.1:2375"
If I set
export DOCKER_HOST="tcp://127.0.0.1:2375"
all my other containers stop working and said, that can not find the Docker daemon.
It looks like my Docker damon use other than 2375 port. How can I check it ?
By default, the docker daemon will use the unix socket unix:///var/run/docker.sock (you can check this is the case for you by doing a sudo netstat -tunlp and note that there is no docker daemon process listening on any ports). It's recommended to keep this setting for security reasons but it sounds like Riak requires the daemon to be running on a TCP socket.
To start the docker daemon with a TCP socket that anybody can connect to, use the -H option:
sudo docker -H 0.0.0.0:2375 -d &
Warning: This means machines that can talk to the daemon through that TCP socket can get root access to your host machine.
Related docs:
http://basho.com/posts/technical/running-riak-in-docker/
https://docs.docker.com/install/linux/linux-postinstall/#configure-where-the-docker-daemon-listens-for-connections
Prepare extra configuration file. Create a file named /etc/systemd/system/docker.service.d/docker.conf. Inside the file docker.conf, paste below content:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Note that if there is no directory like docker.service.d or a file named docker.conf then you should create it.
Restart Docker. After saving this file, reload the configuration by systemctl daemon-reload and restart Docker by systemctl restart docker.service.
Check your Docker daemon. After restarting docker service, you can see the port in the output of systemctl status docker.service
like /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock.
Hope this may help
Thank you!
Reference docs of docker: https://docs.docker.com/install/linux/linux-postinstall/#configure-where-the-docker-daemon-listens-for-connections
There are 2 ways in configuring the docker daemon port
1) Configuring at /etc/default/docker file:
DOCKER_OPTS="-H tcp://127.0.0.1:5000 -H unix:///var/run/docker.sock"
2) Configuring at /etc/docker/daemon.json:
{
"debug": true,
"hosts": ["tcp://127.0.0.1:5000", "unix:///var/run/docker.sock"]
}
If the docker default socket is not configured Docker will wait for infinite period.i.e
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
NOTE : BUT DON'T CONFIGURE IN BOTH THE CONFIGURATION FILES, the following error may occur :
Waiting for /var/run/docker.sock
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts: (from flag: [tcp://127.0.0.1:5000 unix:///var/run/docker.sock], from file: tcp://127.0.0.1:5000)
The reason for adding both the user port[ tcp://127.0.0.1:5000] and default docker socket[unix:///var/run/docker.sock] is that the user port enables the access to the docker APIs whereas the default socket enables the CLI. In case the default port[unix:///var/run/docker.sock] is not mentioned in /etc/default/docker file the following error may occur:
# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
This error is not because that the docker is not running, but because of default docker socket is not enabled.
Once the configuration is enabled restart the docker service and verify the docker port is enabled or not:
# netstat -tunlp | grep -i 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 31661/dockerd
Applicable for Docker Version 17.04, may vary with different versions of docker.
Since I also had the same problem of "How to detect a docker daemon port" however I had on OSX and after little digging in I found the answer. I thought to share the answer here for people coming from osx.
If you visit known-issues from docker for mac and github issue, you will find that by default the docker daemon only listens on unix socket /var/run/docker.sock and not on tcp. The default port for docker is 2375 (unencrypted) and 2376(encrypted) communication over tcp(although you can choose any other port).
On OSX its not straight forward to run the daemon on tcp port. To do this one way is to use socat container to redirect the Docker API exposed on the unix domain socket to the host port on OSX.
docker run -d -v /var/run/docker.sock:/var/run/docker.sock -p 127.0.0.1:2375:2375 bobrik/socat TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
and then
export DOCKER_HOST=tcp://localhost:2375
However for local client on mac os you don't need to export DOCKER_HOST variable to test the api.
If you run ps -aux | grep dockerd you should see the endpoints it is running on.
Try add -H tcp://0.0.0.0:2375(at end of Execstart line) instead of -H 0.0.0.0:2375.

Resources