如何在我的 ubuntu 容器中安装 Docker?

mmo*_*ssa 3 ubuntu containers docker

我在ubuntu:18.04运行我的 nodejs 应用程序的容器中安装了 docker,我需要在这个容器中安装 docker,因为我需要对其他小应用程序进行 dockerize

她是我的 Dockerfile

FROM ubuntu:18.04

WORKDIR /app

COPY package*.json ./

# Install Nodejs
RUN apt-get update
RUN apt-get -y install curl wget dirmngr apt-transport-https lsb-release ca-certificates software-properties-common gnupg-agent
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get -y install nodejs

# Install Chromium
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update
RUN apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst \
      --no-install-recommends
RUN rm -rf /var/lib/apt/lists/*

# Install Docker
RUN curl -fsSL https:/download.docker.com/linux/ubuntu/gpg | apt-key add -
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
RUN apt-get update -y
RUN apt-get install -y docker-ce docker-ce-cli containerd.io

RUN npm install

COPY . .

CMD [ "npm", "start" ]

EXPOSE 3000
Run Code Online (Sandbox Code Playgroud)

当容器启动时, i docker exec -it app bash。如果我做了service docker start,然后ps ax,得到这个

  PID TTY      STAT   TIME COMMAND
  115 ?        Z      0:00 [dockerd] <defunct>
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能在容器内使用 docker 或者是否有不使用 apk 而是 apt-get 的 docker 图像?因为当我需要使用它时,我收到了这个错误:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Run Code Online (Sandbox Code Playgroud)

Adi*_*iii 7

首先最好使用一个基本映像,无论是用于node-image和 install docker,还是用于docker-image和已安装的 node,而不是从头开始创建映像。一切你需要的

FROM  node:buster
RUN apt-get update 
RUN apt install docker.io -y
RUN docker --version
ENTRYPOINT nohup dockerd >/dev/null 2>&1 & sleep 10 && node /app/app.js


Run Code Online (Sandbox Code Playgroud)

第二件事,报错Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?,原因是你没有在Dockefile中启动docker进程,也不建议在容器中运行多个进程,因为如果Docker进程死了你不知道状态,你必须放一个进程在后台。

CMD nohup dockerd >/dev/null 2>&1 & sleep 10 && node /app/app.js
Run Code Online (Sandbox Code Playgroud)

并运行

docker run --privileged  -it -p 8000:8000  -v /var/run/docker.sock:/var/run/docker.sock your_image
Run Code Online (Sandbox Code Playgroud)