【问题标题】:docker-compose seems to ignore .env-variables on mariadbdocker-compose 似乎忽略了 mariadb 上的 .env-variables
【发布时间】:2021-12-25 15:42:27
【问题描述】:

我在一段时间内遇到了docker-compose 的问题,无法弄清楚那里发生了什么。给定的是以下场景 - 以前有效但停止了新项目的工作,现在在我建立的每个新堆栈上工作:

$ docker --version
Docker version 20.10.12, build e91ed57  
$ docker-compose --version
Docker Compose version v2.2.2

OS: Pop!_OS 21.10 (NVIDIA)  

文件结构:

/myfolder
+ app-src (containing my laravel-project)
+ database-dumps (for database-dumps - containing a .gitkeep-file)
+ database_persist (for persistent data - containing a .gitkeep-file)
- .env
- .gitignore
- buildcontrainer.sh (custom script)
- container-start.sh (custom script)
- docker-compose.yml
- migrate-database.sh (custom script)
- README.md

这是缩短的docker-compose.yml-file(缩短,因为它只影响数据库......nginx和php-container工作正常):

version: '3'
services:
  database:
    environment:
      - MYSQL_DATABASE=${DB_DATABASE}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    image: mariadb:10.6.1
    ports:
      - "${DB_EXPOSE_PORT}:3306"
    volumes:
      - "./database_persist:/var/lib/mysqldata"

还有 .env 文件:

COMPOSE_PROJECT_NAME=MyProject

DB_DATABASE="myprojectdb"
DB_USER="myproject"
DB_PASSWORD="12345"
DB_ROOT_PASSWORD="12345"
DB_EXPOSE_PORT=9036

WEB_EXPOSE_PORT_HTTP=9080
WEB_EXPOSE_PORT_HTTPS=9043

现在我的问题是 - 每当我尝试设置场景中给出的环境并运行 docker-compose builddocker-compose up 之后,我都会得到以下输出:

Creating network "myproject_default" with the default driver
Creating myproject_database_1 ... done
Attaching to myproject_database_1
database_1  | 2021-12-25 15:30:53+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.6.1+maria~focal started.
database_1  | 2021-12-25 15:30:53+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
database_1  | 2021-12-25 15:30:53+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.6.1+maria~focal started.
database_1  | 2021-12-25 15:30:53+00:00 [Note] [Entrypoint]: Initializing database files
database_1  | 2021-12-25 15:30:53 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
database_1  | 
database_1  | 
database_1  | PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
database_1  | To do so, start the server, then issue the following commands:
database_1  | 
database_1  | '/usr/bin/mysqladmin' -u root password 'new-password'
database_1  | '/usr/bin/mysqladmin' -u root -h  password 'new-password'
database_1  | 
database_1  | Alternatively you can run:
database_1  | '/usr/bin/mysql_secure_installation'
database_1  | 
database_1  | which will also give you the option of removing the test
database_1  | databases and anonymous user created by default.  This is
database_1  | strongly recommended for production servers.
database_1  | 
database_1  | See the MariaDB Knowledgebase at https://mariadb.com/kb or the
database_1  | MySQL manual for more instructions.
database_1  | 
database_1  | Please report any problems at https://mariadb.org/jira
database_1  | 
database_1  | The latest information about MariaDB is available at https://mariadb.org/.
database_1  | You can find additional information about the MySQL part at:
database_1  | https://dev.mysql.com
database_1  | Consider joining MariaDB's strong and vibrant community:
database_1  | https://mariadb.org/get-involved/
database_1  | 
database_1  | 2021-12-25 15:30:57+00:00 [Note] [Entrypoint]: Database files initialized
database_1  | 2021-12-25 15:30:57+00:00 [Note] [Entrypoint]: Starting temporary server
database_1  | 2021-12-25 15:30:57+00:00 [Note] [Entrypoint]: Waiting for server startup
database_1  | 2021-12-25 15:30:57 0 [Note] mysqld (mysqld 10.6.1-MariaDB-1:10.6.1+maria~focal) starting as process 110 ...
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Number of pools: 1
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
database_1  | 2021-12-25 15:30:57 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Using Linux native AIO
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Initializing buffer pool, total size = 134217728, chunk size = 134217728
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Completed initialization of buffer pool
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: 128 rollback segments are active.
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Creating shared tablespace for temporary tables
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: 10.6.1 started; log sequence number 42616; transaction id 18
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
database_1  | 2021-12-25 15:30:57 0 [Note] Plugin 'FEEDBACK' is disabled.
database_1  | 2021-12-25 15:30:57 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
database_1  | 2021-12-25 15:30:57 0 [Warning] 'user' entry 'root@66d18f02ea8a' ignored in --skip-name-resolve mode.
database_1  | 2021-12-25 15:30:57 0 [Warning] 'proxies_priv' entry '@% root@66d18f02ea8a' ignored in --skip-name-resolve mode.
database_1  | 2021-12-25 15:30:57 0 [Note] InnoDB: Buffer pool(s) load completed at 211225 15:30:57
database_1  | 2021-12-25 15:30:57 0 [Note] mysqld: ready for connections.
database_1  | Version: '10.6.1-MariaDB-1:10.6.1+maria~focal'  socket: '/run/mysqld/mysqld.sock'  port: 0  mariadb.org binary distribution
database_1  | 2021-12-25 15:30:58+00:00 [Note] [Entrypoint]: Temporary server started.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
database_1  | 2021-12-25 15:31:01 5 [Warning] 'proxies_priv' entry '@% root@66d18f02ea8a' ignored in --skip-name-resolve mode.
database_1  | 2021-12-25 15:31:01+00:00 [Note] [Entrypoint]: Creating database myprojectdb
database_1  | 2021-12-25 15:31:01+00:00 [Note] [Entrypoint]: Creating user myproject
database_1  | 2021-12-25 15:31:01+00:00 [Note] [Entrypoint]: Giving user myproject access to schema myprojectdb
database_1  | 
database_1  | 2021-12-25 15:31:01+00:00 [Note] [Entrypoint]: Stopping temporary server
database_1  | 2021-12-25 15:31:01 0 [Note] mysqld (initiated by: root[root] @ localhost []): Normal shutdown
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: FTS optimize thread exiting.
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: Starting shutdown...
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: Buffer pool(s) dump completed at 211225 15:31:01
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: Removed temporary tablespace data file: "./ibtmp1"
database_1  | 2021-12-25 15:31:01 0 [Note] InnoDB: Shutdown completed; log sequence number 42628; transaction id 19
database_1  | 2021-12-25 15:31:01 0 [Note] mysqld: Shutdown complete
database_1  | 
database_1  | 2021-12-25 15:31:02+00:00 [Note] [Entrypoint]: Temporary server stopped
database_1  | 
database_1  | 2021-12-25 15:31:02+00:00 [Note] [Entrypoint]: MariaDB init process done. Ready for start up.
database_1  | 
database_1  | 2021-12-25 15:31:02 0 [Note] mysqld (mysqld 10.6.1-MariaDB-1:10.6.1+maria~focal) starting as process 1 ...
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Number of pools: 1
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
database_1  | 2021-12-25 15:31:02 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Using Linux native AIO
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Initializing buffer pool, total size = 134217728, chunk size = 134217728
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Completed initialization of buffer pool
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: 128 rollback segments are active.
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Creating shared tablespace for temporary tables
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: 10.6.1 started; log sequence number 42628; transaction id 18
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
database_1  | 2021-12-25 15:31:02 0 [Note] Plugin 'FEEDBACK' is disabled.
database_1  | 2021-12-25 15:31:02 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
database_1  | 2021-12-25 15:31:02 0 [Note] Server socket created on IP: '0.0.0.0'.
database_1  | 2021-12-25 15:31:02 0 [Note] Server socket created on IP: '::'.
database_1  | 2021-12-25 15:31:02 0 [Warning] 'proxies_priv' entry '@% root@66d18f02ea8a' ignored in --skip-name-resolve mode.
database_1  | 2021-12-25 15:31:02 0 [Note] InnoDB: Buffer pool(s) load completed at 211225 15:31:02
database_1  | 2021-12-25 15:31:02 0 [Note] mysqld: ready for connections.
database_1  | Version: '10.6.1-MariaDB-1:10.6.1+maria~focal'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

在我看来,.env-parameters 似乎不再提供给数据库容器。所有使用docker-compose build 重新创建数据库容器或通过docker image prunedocker-compose pull 删除所有图像的尝试都是徒劳的。两天前我已将我的操作系统从 21.04 更新到 21.10,但我认为这不会导致问题(如果有,请在此处说明)。有人知道如何解决这个问题吗?直接在the docker-compose.yml 中设置MYSQL_ROOT_PASSWORD 也无济于事。 database_persist 的权限是通过sudo chown -R $USER:$USER database_persist/ 设置的,所以这也不是问题。

谢谢(圣诞快乐:))

编辑:我尝试重新安装 docker 并将 docker-compose 更新到版本 2.2.2(相应编辑),但没有效果。其中一位评论者问我是否可以连接到容器。是的,我可以,但不能以 root 身份。

【问题讨论】:

  • 以后能不能用用户名/密码连接?
  • 是的,我可以,但不能以 root 身份。查看我的编辑。
  • 如果你可以使用那个用户和pw,说明这个env文件已经被读取和使用过了,否则它就可以工作。您确定您为 root 密码变量使用了正确的名称吗?
  • 是的 - 我确定我用对了。就像我说的 - 我从现有项目中复制了它并调整了变量。另外 - 它以前有效,现在无效。

标签: docker docker-compose mariadb


【解决方案1】:

.env 文件可以作为docker-compose.yaml 的一部分进行解析。尽管我个人从未像您那样使用过这种方式,因为您也可以将文件直接传递到容器中。使用env_file

version: '3'
services:
  database:
    env_file: .env
    image: mariadb:10.6.1
    ports:
      - "${DB_EXPOSE_PORT}:3306"
    volumes:
      - "./database_persist:/var/lib/mysqldata"

显然,您还需要更改 .env 文件中的变量名称。我个人会将其重命名为更清晰的名称。

【讨论】:

  • 感谢您的回答 - 问题是 docker-compose 应该根据文档自动解析一个仅名为“.env”的 .env 文件。另外 - 即使我将变量直接放在 yaml 中,它们也不会被处理。
  • 是的,应该是。不过,我仅按照文档中的说明使用它。在图像或端口号。 `即使我将变量直接放在山药中`是一个很好的提示,在你的问题中忽略了这一点,对不起。这是圣诞酒的罪魁祸首,;) 您是否尝试删除 database_persist 目录以及固定的书面内容?
  • 哈哈哈 - 不用担心... :) 编辑:它是空的。
【解决方案2】:

好的——出于某种奇怪的原因,输出保持不变,但是当我尝试登录 dbroot 时,dbuser 实际上使用了在 env 文件中指定的密码。加上使用其他任何东西都会导致“访问被拒绝”。所以我不知道更新是否解决了问题,或者日志输出是否只是吓到我,这实际上是预期的行为。如果我有任何新见解,我会更新此内容。

【讨论】:

  • 你不是说它不适用于根密码吗?
  • 是的 - 但在重新安装 + 更新后确实如此。
  • 我怀疑它一直在工作。这也是为什么我最初问它是否有效。我认为这种信息总是会显示出来。一个变量被传递但另一个不被传递也不太可能。您可能已将密码更改为已保存在卷中的密码。重新启动不会更改存储在那里的密码。它在该卷的整个生命周期内只设置一次。在这种情况下,重建什么都不做,因为它是一个 vanilla 图像。没有什么可构建的。
猜你喜欢
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2016-09-30
  • 2016-03-16
  • 2014-02-03
  • 2012-03-11
  • 2013-09-07
相关资源
最近更新 更多