简体   繁体   English

在 ubuntu 容器内运行 docker

[英]Run docker inside ubuntu container

2 days I try to run the docker inside an ubuntu container: 2 天我尝试在 ubuntu 容器中运行 docker:

  1. docker run -it ubuntu bash
  2. Install docker by instruction of https://docs.docker.com/engine/install/ubuntu/ or/andhttps://phoenixnap.com/kb/how-to-install-docker-on-ubuntu-18-04按照https://docs.docker.com/engine/install/ubuntu/或/和https://phoenixnap.com/kb/how-to-install-docker-on-ubuntu-18-04 的说明安装 docker
  3. Finally I have installed docker:最后我安装了 docker:
root@e65411d2b70a:/# docker -v
Docker version 19.03.6, build 369ce74a3c
  1. But when I try to run docker run hello-world have some problem但是当我尝试运行docker run hello-world时出现了一些问题
root@5ac21097b6f6:/# docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

In service list not docker:在服务列表中不是 docker:

root@5ac21097b6f6:/# service docker start
docker: unrecognized service
root@5ac21097b6f6:/# service  --status-all
 [ - ]  apparmor
 [ + ]  cgroupfs-mount
 [ - ]  dbus
 [ ? ]  hwclock.sh
 [ - ]  procps
 [ ? ]  ubuntu-fan

When try to run dockerd :当尝试运行dockerd

root@5ac21097b6f6:/# dockerd    
INFO[2020-04-23T07:01:11.622627006Z] Starting up                                  
INFO[2020-04-23T07:01:11.624389266Z] libcontainerd: started new containerd process  pid=154
INFO[2020-04-23T07:01:11.624460438Z] parsed scheme: "unix"                         module=grpc
INFO[2020-04-23T07:01:11.624477203Z] scheme "unix" not registered, fallback to default scheme  module=grpc
INFO[2020-04-23T07:01:11.624532871Z] ccResolverWrapper: sending update to cc: {[{unix:///var/run/docker/containerd/containerd.sock 0  <nil>}] <nil>}  module=grpc
INFO[2020-04-23T07:01:11.624560679Z] ClientConn switching balancer to "pick_first"  module=grpc
INFO[2020-04-23T07:01:11.664827037Z] starting containerd                           revision= version="1.3.3-0ubuntu1~18.04.2"
ERRO[2020-04-23T07:01:11.664943052Z] failed to change OOM score to -500            error="write /proc/154/oom_score_adj: permission denied"
...
INFO[2020-04-23T07:01:11.816951247Z] stopping event stream following graceful shutdown  error="context canceled" module=libcontainerd namespace=plugins.moby
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.6.1: can't initialize iptables table `nat': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.
 (exit status 3)

Not understand why Permission denied if user root .不明白为什么Permission denied if user root

Install sudo and add root to the group, but it's not help.安装sudo并将 root 添加到该组,但它没有帮助。

apt-get install sudo
usermod -a -G sudo root

- sudo dockerd have the save problem. - sudo dockerd有保存问题。

How to make work docker inside ubuntu container?如何在 ubuntu 容器内工作 docker? Do you have ideas?你有想法吗?

ps.附言。 I know about docker-in-docker, I need exactly docker inside ubuntu-container我知道 docker-in-docker,我需要 docker inside ubuntu-container

pss.附言。 I know about -v /var/run/docker.sock:/var/run/docker.sock - but needed independent the docker service inside ubuntu-container.我知道-v /var/run/docker.sock:/var/run/docker.sock - 但需要在 ubuntu-container 中独立 docker 服务。

When running docker in docker, the container must use the docker engine on your host.在 docker 中运行 docker 时,容器必须在您的主机上使用 docker 引擎。

Here is a simple working setup:这是一个简单的工作设置:

1) Create a dockerfile with docker CLI installed. 1) 创建一个安装了 docker CLI 的 dockerfile。 I am using the official compose image, so you also have docker-compose我用的是官方的compose图片,所以你也有docker-compose

FROM docker/compose:1.25.5
WORKDIR /app
ENTRYPOINT ["/bin/sh"]

2) When running it, mount the docker sock 2)运行时,安装docker sock

$ docker build -t dind .
$ docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock dind

Form within the container, you now have docker.在容器内形成,您现在拥有 docker。 Try running docker ps尝试运行docker ps

If you want to do docker in docker without -v /var/run/docker.sock:/var/run/docker.sock then I am afraid that there is no good way to do this.如果你想在没有-v /var/run/docker.sock:/var/run/docker.sock的情况下在 docker 中做 docker ,那么恐怕没有好的方法可以做到这一点。 Sharing the docker socket from host is the classic way to make docker containers run within another docker container.从主机共享 docker 套接字是使 docker 容器在另一个 docker 容器中运行的经典方法。

I was trying my best to run containers within containers just like you for the past few days.在过去的几天里,我一直在尽最大努力像你一样在容器中运行容器。 Wasted many hours.浪费了很多时间。 So far most of the people advise me to do stuff like using the docker's DIND image which is not applicable for my case, as I need the main container to be Ubuntu OS, or to run some privilege command and map the daemon socket into container, like -v /var/run/docker.sock:/var/run/docker.sock (Which never ever works for me, or for any Ubuntu OS I tried. Reason being, the main container which is based on Ubuntu OS does not comes with systemd which is important to run docker containers conveniently like a usual local machine)到目前为止,大多数人建议我做一些事情,比如使用不适用于我的情况的 docker 的 DIND 图像,因为我需要主容器是 Ubuntu 操作系统,或者运行一些特权命令和 map 守护程序套接字到容器中,像-v /var/run/docker.sock:/var/run/docker.sock (这对我或我试过的任何 Ubuntu 操作系统都不起作用。原因是,基于 Ubuntu 操作系统的主容器不带有 systemd,这对于像普通本地机器一样方便地运行 docker 容器很重要)

The solution I found was to use Nestybox on my Ubuntu 20.04 system and it works best.我找到的解决方案是在我的 Ubuntu 20.04 系统上使用 Nestybox,效果最好。 Its also extremely simple to execute, provided your local system is ubuntu (which they support best), as the container runtime are specifically deigned for such application.如果您的本地系统是 ubuntu(他们最支持),它的执行也非常简单,因为容器运行时是专门为此类应用程序设计的。 It also has the most flexible options.它还具有最灵活的选项。 The free edition of Nestybox is perhaps the best method as of Nov 2022. Highly recommends you to try it without bothering all the tedious setup other people suggest.截至 2022 年 11 月,免费版的 Nestybox 可能是最好的方法。强烈建议您尝试一下,而不要理会其他人建议的所有繁琐设置。 They have many pre-constructed solutions to address such specific needs with a simple command line.他们有许多预构建的解决方案,可以通过简单的命令行来满足此类特定需求。

The Nestybox provide special runtime environment for newly created docker container, they also provides some ubuntu/common OS images with docker and systemd in built. Nestybox 为新创建的 docker 容器提供了特殊的运行环境,他们还提供了一些 docker 和内置 systemd 的 ubuntu/common OS 镜像。 Their goal is to make the main container function exactly the same as a virtual machine securely.他们的目标是让主容器function安全的和虚拟机一模一样。 You can literally ssh into your ubuntu main container as well without the ability to access anything in the main machine.您也可以从字面上将 ssh 放入 ubuntu 主容器中,而无法访问主机中的任何内容。 From your main container you may create all kinds of containers like a normal local system does.从您的主容器中,您可以像普通本地系统一样创建各种容器。 That systemd is very important for you to setup docker conveniently inside the container. systemd 对于您在容器内方便地设置 docker 非常重要。

One simple common command to execute sysbox:执行 sysbox 的一个简单的常用命令:

    dock run --runtime=sysbox-runc -it any_image

If you think thats what you are looking for, you can find out more at their github: https://github.com/nestybox/sysbox如果你认为这就是你要找的东西,你可以在他们的 github 找到更多信息: https://github.com/nestybox/sysbox

Quicklink to instruction on how to deploy a simple sysbox runtime environment container: https://github.com/nestybox/sysbox/blob/master/docs/quickstart/README.md有关如何部署简单 sysbox 运行时环境容器的说明的快速链接: https://github.com/nestybox/sysbox/blob/master/docs/quickstart/README.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM