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

限于喜欢 提交于 2019-12-11 02:02:11

问题


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.


回答1:


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).




回答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.




回答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



回答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. Thanks




回答5:


Run Docker with sudo. If that doesn't work, try running the daemon in the following way:

sudo nohup docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock &


来源:https://stackoverflow.com/questions/51857634/cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock-is-the-docke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!