Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? inside a Dockerfile
Asked Answered
A

10

22

I have the following Dockerfile:

FROM ubuntu

ENV NPM_CONFIG_LOGLEVEL warn
ENV admin_user="PeerAdmin" network_name=$1 version=$2 hversion=hlfv1     fabrik_path=/fabric-tools project_dir=$(pwd) 
ENV card_store_dir=$project_dir/.card-store stage_dir=$project_dir/.stage     env_dir=$project_dir/env is_ok=1 FABRIC_VERSION=hlfv1 

WORKDIR /app
COPY . /app

USER root
# RUN chown -R ubuntu:ubuntu .
WORKDIR /app
RUN apt-get update && \
    mkdir "$fabrik_path" && \
    cd "$fabrik_path" && \
    export FABRIC_VERSION=hlfv1 && \
    apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \
    apt-get -y install curl && \
    apt-get -y install unzip && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
    apt-get -y install docker.io && \
    curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.zip && \
    unzip fabric-dev-servers.zip && \
    service docker start && \
    ./downloadFabric.sh && \
    ./startFabric.sh

Attempting to execute it, I am receiving an error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Commands like service docker start or systemctl do not work.

Absonant answered 15/8, 2018 at 11:13 Comment(3)
open cmd and run docker version. Does both the client and server show or is there an error?Nominative
yes, the same: 18.06.0-ceAbsonant
How did you fix this , Even i have a requirement to run docker inside docker container. Please let me know the design you followed to overcome thisConcatenate
S
12

You can't (*) run Docker inside Docker containers or images. You can't (*) start background services inside a Dockerfile. As you say, commands like systemctl and service don't (*) work inside Docker anywhere. And in any case you can't use any host-system resources, including the host's Docker socket, from anywhere in a Dockerfile.

You need to redesign this Dockerfile so that it only installs the software and makes no attempt to start it. Ideally a container would start only a single server, and would run it in the foreground as its CMD; otherwise you might depend on things like supervisord to have multiple servers if you must. If your application heavily relies on being able to start things in Docker, you might find it much easier to install in a virtual machine.

(*) Technically there are ways to do all of these things, but they're all tricky and complicated and have implications (up to potentially giving your container unrestricted root access over the host, and your container startup actively reconfiguring some low-level host details).

Silvester answered 15/8, 2018 at 12:6 Comment(0)
S
22

Try this cmd:

sudo service docker restart
Sturm answered 1/12, 2020 at 10:1 Comment(0)
S
20

1. Check the docker.service

sudo systemctl status docker

output is something like this:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/docker.service.d
           └─http-proxy.conf
   Active: active (running) since Sat 2021-12-04 19:11:03 +0330; 2min 59s ago
     Docs: https://docs.docker.com
 Main PID: 28223 (dockerd)
    Tasks: 29
   CGroup: /system.slice/docker.service
           └─28223 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.813464635+03:30" level=info msg="New memberlist node - Node:elitedesk will use memberlist nodeID:57d6b077dc1b with config:&{NodeID:57d6b077dc1b Hostname:elitedesk BindAddr:0.0.0.0 AdvertiseAddr:192.168.1.45 BindPort:0 Keys:[[214 188 67 83 106 206 215 54 212 222 237 172 208 101 235 219] [232 26 246 100 135 196 113 220 147 95 50 132 140 206 7 126] [236 55 254 140 107 62 111 184 64 103 33 77 246 154 26 50]] PacketBufferSize:1400 reapEntryInterval:1800000000000 reapNetworkInterval:1825000000000 StatsPrintPeriod:5m0s HealthPrintPeriod:1m0s}"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.813277123+03:30" level=info msg="Daemon has completed initialization"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.813269814+03:30" level=info msg="initialized VXLAN UDP port to 4789 "
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.814239176+03:30" level=info msg="Node 57d6b077dc1b/192.168.1.45, joined gossip cluster"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.814424565+03:30" level=info msg="Node 57d6b077dc1b/192.168.1.45, added to nodes list"
Dec 04 19:11:03 elitedesk systemd[1]: Started Docker Application Container Engine.
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.891119330+03:30" level=info msg="API listen on /var/run/docker.sock"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.964000220+03:30" level=error msg="error reading the kernel parameter net.ipv4.vs.conn_reuse_mode" error="open /proc/sys/net/ipv4/vs/conn_reuse_mode: no such file or directory"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.964029536+03:30" level=error msg="error reading the kernel parameter net.ipv4.vs.expire_nodest_conn" error="open /proc/sys/net/ipv4/vs/expire_nodest_conn: no such file or directory"
Dec 04 19:11:03 elitedesk dockerd[28223]: time="2021-12-04T19:11:03.964042003+03:30" level=error msg="error reading the kernel parameter net.ipv4.vs.expire_quiescent_template" error="open /proc/sys/net/ipv4/vs/expire_quiescent_template: no such file or directory"

If seemed to have an error, restart it:

sudo systemctl restart docker

And check the status again. If did not solve, next

2. Request to the daemon's socket, see if it is up:

curl --unix-socket /var/run/docker.sock  http://localhost/_ping; echo

Output is just OK message

OK

If did not get OK and got something like this:

curl: (7) Couldn't connect to server

3. Check the socket service

sudo systemctl status docker.socket

And a fine output will be:

● docker.socket - Docker Socket for the API
   Loaded: loaded (/lib/systemd/system/docker.socket; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-12-04 19:10:57 +0330; 6min ago
   Listen: /var/run/docker.sock (Stream)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/docker.socket

Dec 04 19:10:57 elitedesk systemd[1]: Starting Docker Socket for the API.
Dec 04 19:10:57 elitedesk systemd[1]: Listening on Docker Socket for the API.

And restart it even if it looked good. Because after stopping the daemon and start it again, it does not work and we should (as long as I know) restart docker.socket.

sudo systemctl restart docker.socket

Yet you were not able to fix it? next:

4. Run the daemon directly in debug mode

sudo dockerd -D -l debug
Smalley answered 4/12, 2021 at 15:54 Comment(2)
sudo systemctl restart docker.socket saved my life on Unbuntu 22. Thx !!!!Dissidence
sudo systemctl restart docker.socket solved my issue as well, but this issue keeps popping up and my solution always have been to restart my server and up all the containers again, any idea why this keeps happening?Ballon
S
12

You can't (*) run Docker inside Docker containers or images. You can't (*) start background services inside a Dockerfile. As you say, commands like systemctl and service don't (*) work inside Docker anywhere. And in any case you can't use any host-system resources, including the host's Docker socket, from anywhere in a Dockerfile.

You need to redesign this Dockerfile so that it only installs the software and makes no attempt to start it. Ideally a container would start only a single server, and would run it in the foreground as its CMD; otherwise you might depend on things like supervisord to have multiple servers if you must. If your application heavily relies on being able to start things in Docker, you might find it much easier to install in a virtual machine.

(*) Technically there are ways to do all of these things, but they're all tricky and complicated and have implications (up to potentially giving your container unrestricted root access over the host, and your container startup actively reconfiguring some low-level host details).

Silvester answered 15/8, 2018 at 12:6 Comment(0)
M
10

Launch the Terminal and execute the commands below:

$ sudo service --status-all

$ sudo service docker start

https://appuals.com/cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock/ Solution 4: Start Docker with the Service command

Mou answered 5/5, 2021 at 2:54 Comment(0)
S
4

It might be the case that the user with which you have logged in to docker engine is not having the correct permission. You can add the user to docker group with below command:

sudo usermod -a -G docker $USER

$USER is the username of the currently logged in user.

Southernly answered 20/9, 2019 at 14:31 Comment(1)
Thanks, don't forget to log-out and log-in again to refresh the group membership. Please note the security implications: wiki.archlinux.org/title/Docker#InstallationRone
A
3

I had this same issue, run this command in your terminal. This fixed the problem.

 sudo apt-get install docker-ce docker-ce-cli containerd.io
Anaphrodisiac answered 13/8, 2019 at 10:31 Comment(0)
D
2

The error may come from "service docker start". If you want to follow the installation instructions from the upstream vendor for a docker container then you need to prepare the environment for that. I can run these commands easily by using the dockers-systemctl-replacement script.

Damnify answered 16/11, 2018 at 11:38 Comment(0)
S
1

If you have Docker Desktop on Windows and are using WSL, you might need to go to "Resources" -> "WSL integration" and turn on the slider under the category of "Enable integration with additional distros"

Docker image

Southeastwardly answered 27/3, 2024 at 19:7 Comment(0)
C
0

if you install docker with deb files, probably not install docker cli. So after install docker, install docker cli, @Jayanth's answer

Chisolm answered 24/12, 2022 at 7:44 Comment(0)
M
0

In my case, I had a directory left after removing docker desktop: ~/.docker. After removing it, docker command started working.

Majewski answered 7/8, 2024 at 15:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.