如何在Docker中删除目录? [英] How to remove a directory in docker?

查看:2694
本文介绍了如何在Docker中删除目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎docker容器中的 rm -rf 仅适用于文件。

It seems that rm -rf in docker container works only with files.

docker run ubuntu /bin/bash -c 'sudo rm  -rf ./mydir; ls'

在没有目录的目录中列出 mydir 打印任何错误。

it lists mydir among directories without printing any errors.

我将ubuntu 14.04用于主机和图像。

I use ubuntu 14.04 for host and image.

编辑:

我认为该错误是在我添加了另一个错误之后出现的文件到目录,但是我弄错了。最后,更改存储驱动程序有助于解决该问题。

I thought that the bug appeared after I had added another file to the directory, but I was mistaken. Finally, changing storage driver helped to solve the issue.

推荐答案

当您似乎无法删除目录时,这里有两个可能性:

When you can't seem to remove a directory, here are two possibilities:


  1. 图像和容器之间的用户混乱。每个 docker运行 c创建一个新容器,并且容器中的所有更改(例如删除目录或运行修改文件系统的操作)均与原始映像以及所有其他容器隔离。

  2. Aufs 分层bug /功能。查看 docker info 来查看您是否正在使用Storage Driver aufs或imagemapper。要重新安装docker以使用imagemapper:将所有不可替代的唯一图像备份到tar文件中,删除docker.io,按照官方安装文档,然后将 / etc / default / docker 更改为包含 DOCKER_OPTS =- -storage-driver = devicemapper;

  1. User confusion between images and containers. Each docker run creates a new container, and any changes in containers, like removing a directory, or running something that modifies the file system, are isolated from the original image, and all other containers.
  2. Aufs layering bug/feature. Look at docker info to see if you are using Storage Driver aufs or imagemapper. To reinstall docker to use imagemapper: backup any irreplaceable, unique images to tar files, remove docker.io, install lxc-docker from the ppa as described in the official install docs and change /etc/default/docker to contain DOCKER_OPTS="--storage-driver=devicemapper"

它也是提交目录然后删除目录的一种反模式数据可能仍在某处。即使它从文件系统中消失了,它仍然可能在内部浪费空间。

It is also probably an anti-pattern to commit directories and then remove them because that data may still be in there somewhere. Even if it disappears from the file system, it could still be wasting space internally.

Docker映像是分层构建的,每个提交都是一层。

To看到这一点,尝试 docker history< image> 来查看您的图像中的内容。

Docker images are built in layers, each commit is a layer.
To see this, try docker history <image> to see what is in your image.

第一种情况值得一提一个简单的示例,但看起来您可能遇到了第二种情况。

The first case is deserving of a short, simple example, but it looks like you are possibly experiencing the second case.

我们创建一个私有图像来自 ubuntu:14.04 mystuff ,然后在下面稍后尝试rm -rf / opt:

Let's create a private image called mystuff from ubuntu:14.04 and, later below, try to rm -rf /opt:

$ docker run -it ubuntu:14.04 /bin/bash
root@435e68479c5b:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@435e68479c5b:/# exit
$ docker commit 435e6 mystuff

让我们看看里面( -t 仅在这里用于一行漂亮的打印):

Let's look inside (-t is only used here for pretty printing on one line):

docker run -t mystuff ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

现在让我们尝试删除 / opt (通常是可选内容,可以安全删除):

Now let's try to remove /opt (usually that is optional stuff, safe to remove):

docker run -t mystuff rm -rf /opt

然后在里面寻找它,它是还在那里!!! (似乎是报告的问题):

And then look inside for it, and it is still there!!! (seems like the reported problem):

docker run -t mystuff ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

发生了什么事?


  • 每次使用 docker run mystuff 时,它都会根据图像 mystuff 新容器。 $ c>

  • 除非使用docker commit,否则不会提交对映像的更改

  • each time docker run mystuff is used, it makes a new container from the image mystuff
  • Changes to the image are not committed unless you use docker commit

容器,更改就在那里:

docker ps -a
... lots of stuff...
e9e8be13d928        mystuff:latest              "rm -rf /opt"          2 minutes ago        Exited (0) 2 minutes ago                                  nostalgic_archimedes    

取出压缩包,查找opt,将其复制为dir开头

Get a tarball out and look for opt, grep it for dir beginning with opt, it isn't there!:

docker export e9e8 | tar tv | grep ^opt

注意: e9e8 是我的工作所特有的这个例子。要引用容器,可以在第一个列中使用 docker ps 中的十六进制字符串的一部分。

Note: e9e8 is particular to my run of this example. To refer to a container, you can use part of the hex string from docker ps in the first col.

现在,我提交了具有rm -rf更改为新映像的容器 noopt

Now I commit the container that has the rm -rf change into a new image noopt:

docker commit e9e8 noopt

然后查看由noopt创建的容器:

And look inside a container created from noopt:

docker run -t noopt ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  proc  root  run  sbin  srv  sys  tmp  usr  var

果然,opt消失了!

但是由于docker映像是分层的,因此分发noopt将在较早的历史映像中包含/ opt,因此您可以

But because docker images are in layers, distributing noopt will include /opt in an earlier historical image, so you can't keep other people from getting it back:

docker history noopt
IMAGE               CREATED             CREATED BY                                      SIZE
bc4e69f9e871        59 minutes ago      rm -rf /opt                                     0 B
d198d23dab38        About an hour ago   /bin/bash                                       3 B
04c5d3b7b065        9 days ago          /bin/sh -c #(nop) CMD [/bin/bash]               0 B
d735006ad9c1        9 days ago          /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/   1.895 kB
70c8faa62a44        9 days ago          /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic   194.5 kB
c7b7c6419568        9 days ago          /bin/sh -c #(nop) ADD file:d4aab24fc178303dc0   192.5 MB
511136ea3c5a        18 months ago                                                       0 B

要返回删除/ opt之前的图层,我们只需要选择d198d ...并运行它即可。

To get back to the layer before /opt was removed, we just need to pick out d198d... and run it

docker run -t d198d23dab38 ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

摘要:


  • Docker具有容器和映像。

    • 图像的意图是更永久。

    • 图像在称为 commits 的图层中创建,类似于git存储库的概念

    • 从图像创建容器,并且容器中的更改不保留在图像中(除非明确提交)

    • Docker has containers and images.
      • Images are intended to be more permanent.
      • Images are created in layers called commits, similar in concept to git repositories
      • Containers are created from images, and changes in containers are not kept in images (unless explicitly committed)

      Docker还有一个很棒的构建系统,应该在这里使用。编写该脚本的人应该学习 docker build 并编写适当的Dockerfile

      Docker also has a great build system that should have been used here. Whoever wrote that script should instead learn about docker build and write a proper Dockerfile

      这篇关于如何在Docker中删除目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆