英文:
Docker data best practices
问题
I've been using Docker for about 6 months. Up until last Friday I thought I had found the holy grail. I love all the benefits of Docker, where I can upgrade my container but leave my data intact.
上周五之前,我已经使用Docker大约6个月了。直到上周五,我认为我已经找到了圣杯。我喜欢Docker的所有好处,我可以升级容器但保留我的数据不变。
On Friday, something happened which lead me to wonder if there is an even better way. When I signed into my account on Friday Docker asked me to upgrade, so I did. When Docker restarted I got a fatal error saying that Docker wasn't able to start. I ended up uninstalling Docker and reinstalling. No big deal, or so I thought. All of my data is gone. This leads me to a couple questions.
星期五发生了一些事情,让我想知道是否有更好的方法。星期五我登录我的帐户时,Docker要求我升级,所以我就升级了。当Docker重新启动时,我收到了一个致命错误,说Docker无法启动。最后,我卸载了Docker并重新安装了它。我以为没什么大不了的,但我的所有数据都不见了。这引发了我一些问题。
For context, here is the database service definition in my docker-compose file.
为了提供背景信息,这是我docker-compose文件中的数据库服务定义。
So, with that being said. I have the following questions:
所以,说了这些之后。我有以下几个问题:
- is my data truly gone? Or is there some super secret archive that Docker maintains that I can retrieve?
我的数据真的丢失了吗?还是Docker维护了一些超级秘密的归档,我可以检索到?
- what is the best practice surrounding data and containers?
关于数据和容器,最佳实践是什么?
- ideally, what I would like to do is have a system set up where my data can exist. Then when django comes out with a new version, or when MariaDb comes out with an update, I can just update them. That's what I like about Docker. However, I hadn't thought of Docker itself having issues. Is there a way that I can update my containers regularly, but still leave my data intact even if Docker has an issue?
理想情况下,我想建立一个系统,让我的数据可以存在。然后,当django发布新版本,或者MariaDb发布更新时,我只需更新它们。这就是我喜欢Docker的原因。然而,我没有考虑到Docker本身可能存在问题。是否有一种方法可以定期更新我的容器,但即使Docker出现问题,仍然保留我的数据?
英文:
I've been using Docker for about 6 months. Up until last Friday I thought I had found the holy grail. I love all the benefits of Docker, where I can upgrade my container but leave my data intact.
On Friday, something happened which lead me to wonder if there is an even better way. When I signed into my account on Friday Docker asked me to upgrade, so I did. When Docker restarted I got a fatal error saying that Docker wasn't able to start. I ended up uninstalling Docker and reinstalling. No big deal, or so I thought. All of my data is gone. This leads me to a couple questions. For context, here is the database service definition in my docker-compose file.
db:
image: mariadb:10.9
volumes:
- mariadb_data:/var/lib/mysql
environment:
TZ: 'America/Chicago'
MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
MYSQL_ROOT_PASSWORD: 'rootpwd'
MYSQL_USER: 'User'
MYSQL_PASSWORD: 'UserPassword1234'
MYSQL_DATABASE: 'Database'
ports:
- 3306:3306
restart: always
networks:
- backend
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
start_period: 5s
interval: 5s
timeout: 5s
retries: 55
volumes:
mariadb_data:
So, with that being said. I have the following questions:
- is my data truly gone? Or is there some super secret archive that Docker maintains that I can retrieve?
- what is the best practice surrounding data and containers?
- ideally, what I would like to do is have a system set up where my data can exist. Then when django comes out with a new version, or when MariaDb comes out with an update, I can just update them. That's what I like about Docker. However, I hadn't thought of Docker itself having issues. Is there a way that I can update my containers regularly, but still leave my data intact even if Docker has an issue?
答案1
得分: 1
你描述的设置似乎没问题,但你的数据可能已丢失。你可以从备份中使用 mysqlrestore
恢复吗?
在 Docker 中,通常有两个存储数据的地方。绑定挂载 是指主机系统上的路径;这有时会引入权限问题,尤其在使用 Docker Desktop 时可能会较慢。命名卷 将数据存储在由 Docker 控制的空间中,这可能会使直接访问变得困难。
对于数据库数据,我通常建议使用命名卷,因为这可以避免潜在的权限和性能问题,而且你不能对不透明的数据库存储做太多有用的操作。
但是:
> 我最终卸载了 Docker 并重新安装了。
如果你使用的是 Docker Desktop 并执行此操作,它将销毁所有 Docker 内容。容器本质上是临时的,镜像可以被拉取或重建,但这也会销毁命名卷中的内容。所以...抱歉,但数据已丢失。
根据我的经验,通常情况下不应该需要这种“销毁一切”的方法,但偶尔会发生。 (多年来,我已经在本机 Linux 上使用 sudo rm -rf /var/lib/docker
,类似地销毁了命名卷。)
即使你使用 Docker,通用的计算实践,如“制作备份”,仍然适用。如果你更新数据库,你还需要担心数据完整性,就像在物理机上一样。
英文:
Your setup as you've described it seems fine, but your data is probably gone. Can you mysqlrestore
it from a dump?
In Docker in general, there are two places you can store data. A bind mount refers to a path on the host system; this can sometimes introduce permission problems, and especially using Docker Desktop it can be slower. A named volume stores the data in space controlled by Docker, which can make it hard to access directly.
For database data, my typical recommendation is to prefer a named volume, since that avoids the potential permission and performance problems, and you can't really do much useful with the opaque database storage.
However:
> I ended up uninstalling Docker and reinstalling.
If you're using Docker Desktop and do this, it will destroy all Docker content. Containers are inherently temporary, and images can either be pulled or rebuilt, but this also destroys named-volume content. So...sorry, but the data is gone.
IME you should not normally need this "destroy everything" path but it does occasionally happen. (I haven't needed to for many years now, but I have sudo rm -rf /var/lib/docker
on native Linux in the past, which similarly destroys named volumes.)
Even if you're using Docker, general computing practices like "make backups" still apply. You'll also have to worry about data integrity if you update the database, in the same way you would on bare metal.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论