gpt4 book ai didi

docker-compose tmpfs 不工作

转载 作者:行者123 更新时间:2023-12-03 23:22:49 27 4
gpt4 key购买 nike

我有一个 docker-compose 文件,我试图通过将它创建的容器的根卷设为只读来保护它。
docker-compose.yml的相关部分:

version: '2'
services:
mysql:
image: mariadb:10.1
read_only: true
tmpfs:
- /var/run/mysqld:uid=999,gid=999
- /tmp
volumes:
- mysql:/var/lib/mysql
restart: always
volumes:
mysql:

麻烦的是, tmpfs没有被创建。如果我使用 docker-compose run --rm mysql /bin/bash 运行容器实例, /var/run/mysqld尽管 tmpfs 目录仍然是只读的条目,以及对 touch /var/run/mysqld/foo 的任何尝试失败。因为这是 MySQL 放置它的套接字和 pid 文件的地方,所以这会导致整个事情失败。我不知道为什么 tmpfs在这种情况下,条目不起作用。
mysql_1    | 2017-01-27 20:53:45 140515784030144 [Note] mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 1 ...
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Using mutexes to ref count buffer pool pages
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: The InnoDB memory heap is disabled
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Compressed tables use zlib 1.2.8
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Using Linux native AIO
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Using SSE crc32 instructions
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Initializing buffer pool, size = 256.0M
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Completed initialization of buffer pool
mysql_1 | 2017-01-27 20:53:45 140515784030144 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_1 | 2017-01-27 20:53:48 140515784030144 [Note] InnoDB: 128 rollback segment(s) are active.
mysql_1 | 2017-01-27 20:53:48 140515784030144 [Note] InnoDB: Waiting for purge to start
mysql_1 | 2017-01-27 20:53:48 140515784030144 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.34-79.1 started; log sequence number 239403989
mysql_1 | 2017-01-27 20:53:48 140515005662976 [Note] InnoDB: Dumping buffer pool(s) not yet started
mysql_1 | 2017-01-27 20:53:48 140515784030144 [Note] Plugin 'FEEDBACK' is disabled.
mysql_1 | 2017-01-27 20:53:49 140515784030144 [Note] Server socket created on IP: '::'.
mysql_1 | 2017-01-27 20:53:49 140515784030144 [ERROR] Can't start server : Bind on unix socket: Read-only file system
mysql_1 | 2017-01-27 20:53:49 140515784030144 [ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld.sock ?
mysql_1 | 2017-01-27 20:53:49 140515784030144 [ERROR] Aborting

我可以验证目录的权限是否正确( mysql 用户的 UID 是 999):
$ ls -la /var/run/mysqld 
total 8
drwxrwxrwx 2 mysql mysql 4096 Jan 17 22:14 .
drwxr-xr-x 4 root root 4096 Jan 18 22:55 ..

但我仍然不能:
$ touch /var/run/mysqld/foo
touch: cannot touch '/var/run/mysqld/foo': Read-only file system

即使我以 root 身份运行。

任何想法我做错了什么?

顺便说一句, /tmp文件系统工作正常。

最佳答案

我一直在这方面做了一些测试,看起来像 /var/run目录在 docker 中是特殊的。

这是一些示例配置和输出:

  ubuntu:
image: ubuntu
command: "bash -c 'mount'"
tmpfs:
- /var/run
- /var/cache

运行 docker-compose up ubuntu显示已安装的内容。可以看 /var/cache已安装,但 /var/run不是。
...
ubuntu_1 | tmpfs on /var/cache type tmpfs (rw,nosuid,nodev,noexec,relatime)
...

如果您使用 docker-compose run ubuntu bash你可以看到它也安装在那里,但不是 /var/run .

原因是 /var/run通常是 /run 的符号链接(symbolic link)因此你创建了 /var/run/mysql因为 tmpfs 不起作用。

如果你把它改成 /run/mysql 就可以了, 但是 /run无论如何,通常都会作为 tmpfs 安装,所以您不妨只制作 /run一个tmpfs。像这样:
  ubuntu:
image: ubuntu
command: "bash -c 'mount'"
tmpfs:
- /run
- /var/cache

注意:我想修改我的答案并展示使用 volumes 的方法:
services:
ubuntu:
image: ubuntu
command: "bash -c 'mount'"
volumes:
- cache_vol:/var/cache
- run_vol:/run

volumes:
run_vol:
driver_opts:
type: tmpfs
device: tmpfs
cache_vol:
driver_opts:
type: tmpfs
device: tmpfs

这也允许您分享 tmpfs如果需要,可以安装。

关于docker-compose tmpfs 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41902930/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com