【问题标题】:docker revert changes to containerdocker 还原对容器的更改
【发布时间】:2014-12-06 23:17:48
【问题描述】:

我正在尝试对我的 docker 容器进行快照,以便可以恢复到单个时间点。

我查看了docker savedocker export,但这些似乎都没有达到我想要的效果。我错过了什么吗?

【问题讨论】:

    标签: import load save export docker


    【解决方案1】:

    这是一个如何使用来自 Docker Hub 的 hello-world 映像的示例

    首先运行 hello-world 镜像,从而下载镜像:

    docker run hello-world 
    

    然后得到你想要得到的图像的哈希

    docker history hello-world
    

    你会看到类似的东西:

    IMAGE               CREATED
    fce289e99eb9        15 months ago 
    

    fce289e99eb9 是您的哈希码。

    要标记此图像,请运行:

    docker tag fce289e99eb9 hello-world:SNAPSHOT-1.0
    

    要列出存储库的所有标签,请使用:

    docker image ls hello-world
    

    你会得到类似的东西:

    REPOSITORY          TAG                 IMAGE ID            CREATED    SIZE 
    hello-world         SNAPSHOT-1.0        fce289e99eb9        15 months ago       1.84kB 
    hello-world         latest              fce289e99eb9        15 months ago       1.84kB
    

    【讨论】:

    【解决方案2】:

    您可能想使用docker commit。此命令将从您的一个 docker 容器 创建一个新的 docker 映像。这样,您以后可以轻松地基于该新图像创建一个新容器。

    请注意,docker commit 命令不会保存存储在 Docker data volumes 中的任何数据。对于那些你需要backups的人。


    例如,如果您正在使用以下 Dockerfile,它声明了一个卷,并将每 5 秒将日期写入两个文件(一个在卷中,另一个不在卷中):

    FROM base
    VOLUME /data
    CMD while true; do date >> /data/foo.txt; date >> /tmp/bar.txt; sleep 5; done
    

    从中构建图像:

    $ docker build --force-rm -t so-26323286 .
    

    并从中运行一个新容器:

    $ docker run -d so-26323286
    

    稍等一下,以便正在运行的 docker 容器有机会将日期写入这两个文件几次。

    $ docker ps
    CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS              PORTS               NAMES
    07b094be1bb2        so-26323286:latest   "/bin/sh -c 'while t   5 seconds ago       Up 5 seconds                            agitated_lovelace
    

    然后将你的容器提交到一个新的镜像so-26323286:snapshot1:

    $ docker commit agitated_lovelace so-26323286:snapshot1
    

    您现在可以看到有两个可用的图像:

    $ docker images | grep so-26323286
    so-26323286                    snapshot1           03180a816db8        19 seconds ago      175.3 MB
    so-26323286                    latest              4ffd141d7d6f        9 minutes ago       175.3 MB
    

    现在让我们验证从so-26323286:snapshot1 运行的新容器是否具有/tmp/bar.txt 文件:

    $ docker run --rm so-26323286:snapshot1 cat /tmp/bar.txt
    Sun Oct 12 09:00:21 UTC 2014
    Sun Oct 12 09:00:26 UTC 2014
    Sun Oct 12 09:00:31 UTC 2014
    Sun Oct 12 09:00:36 UTC 2014
    Sun Oct 12 09:00:41 UTC 2014
    Sun Oct 12 09:00:46 UTC 2014
    Sun Oct 12 09:00:51 UTC 2014
    

    并且见证这样的容器没有任何/data/foo.txt 文件(因为/data 是一个数据卷):

    $ docker run --rm so-26323286:snapshot1 cat /data/foo.txt
    cat: /data/foo.txt: No such file or directory
    

    最后,如果您想访问第一个(仍在运行的)容器中的/data/foo.txt 文件,您可以使用 docker run --volumes-from 选项:

    $ docker run --rm --volumes-from agitated_lovelace base cat /data/foo.txt
    Sun Oct 12 09:00:21 UTC 2014
    Sun Oct 12 09:00:26 UTC 2014
    Sun Oct 12 09:00:31 UTC 2014
    Sun Oct 12 09:00:36 UTC 2014
    Sun Oct 12 09:00:41 UTC 2014
    Sun Oct 12 09:00:46 UTC 2014
    Sun Oct 12 09:00:51 UTC 2014
    Sun Oct 12 09:00:56 UTC 2014
    Sun Oct 12 09:01:01 UTC 2014
    Sun Oct 12 09:01:06 UTC 2014
    Sun Oct 12 09:01:11 UTC 2014
    Sun Oct 12 09:01:16 UTC 2014
    

    【讨论】:

    • docker commit 似乎没有做我想做的事。我有一个wordpress图片。当我添加一个帖子并执行 docker commit,然后使用我的新图像启动一个新容器时,新帖子丢失了。我觉得 docker 对承诺的看法与世界其他地方不同。
    • 您应该研究该 wordpress 映像的 Dockerfile,以了解它是否使用卷来存储发布数据。此外,该 wordpress 容器可能依赖第二个数据库容器来存储其数据,如果是这种情况,那么您要保存数据库容器,而不是 wordpress 容器。
    • 不,都是自包含的,所以我不知道为什么数据不持久。
    • 也许你可以分享 WordPress Docker 文件
    • 或者您可以只备份该卷,为您的 mysql 数据库拍摄快照。
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多