为什么 MySQL 服务器不在 Docker 容器中启动?

huangapple go评论76阅读模式
英文:

Why does mysql server not start in docker container?

问题

我有这个相当简单的Docker容器,安装了一个MySQL服务器并尝试启动它,但它拒绝启动。为什么?我在这里做错了什么?

FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive 
RUN apt-get update -y
RUN apt-get -y full-upgrade;
RUN apt-get -y install apt-utils
RUN apt-get -y install default-mysql-server
RUN service mysql start
RUN service mysql status

使用以下命令构建:

docker build . --progress=plain

以以下错误结束:

#9 [6/7] RUN service mysql start
#9 0.662  * Starting MySQL database server mysqld
#9 0.725 su: warning: cannot change directory to /nonexistent: No such file or directory
#9 2.965    ...done.
#9 DONE 3.1s

#10 [7/7] RUN service mysql status
#10 0.832  * MySQL is stopped.
#10 ERROR: process "/bin/sh -c service mysql status" did not complete successfully: exit code: 3
------
 > [7/7] RUN service mysql status:
#10 0.832  * MySQL is stopped.
------
Dockerfile:8
--------------------
   6 |     RUN apt-get -y install default-mysql-server
   7 |     RUN service mysql start
   8 | >>> RUN service mysql status
--------------------
ERROR: failed to solve: process "/bin/sh -c service mysql status" did not complete successfully: exit code: 3

编辑:还尝试了以下版本:

FROM ubuntu:20.04

FROM ubuntu:18.04

以及

RUN apt-get -y install mariadb-server
RUN service mariadb start
RUN service mariadb status

所有尝试都导致相同的崩溃。

英文:

I have this fairly simple docker container installing a mysql server and attempting to start it, but it refuse to start. Why? What am i doing wrong here?

FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive 
RUN apt-get update -y
RUN apt-get -y full-upgrade;
RUN apt-get -y install apt-utils
RUN apt-get -y install default-mysql-server
RUN service mysql start
RUN service mysql status

building with
docker build . --progress=plain
ends with

#9 [6/7] RUN service mysql start
#9 0.662  * Starting MySQL database server mysqld
#9 0.725 su: warning: cannot change directory to /nonexistent: No such file or directory
#9 2.965    ...done.
#9 DONE 3.1s

#10 [7/7] RUN service mysql status
#10 0.832  * MySQL is stopped.
#10 ERROR: process "/bin/sh -c service mysql status" did not complete successfully: exit code: 3
------
 > [7/7] RUN service mysql status:
#10 0.832  * MySQL is stopped.
------
Dockerfile:8
--------------------
   6 |     RUN apt-get -y install default-mysql-server
   7 |     RUN service mysql start
   8 | >>> RUN service mysql status
--------------------
ERROR: failed to solve: process "/bin/sh -c service mysql status" did not complete successfully: exit code: 3

edit: have also tried

FROM ubuntu:20.04

and

FROM ubuntu:18.04

and

RUN apt-get -y install mariadb-server
RUN service mariadb start
RUN service mariadb status

same crash in all attempts 为什么 MySQL 服务器不在 Docker 容器中启动?

答案1

得分: 1

命令类似 service 在 Docker 中通常无法正常工作。您应该避免在容器内运行 servicesystemctl 或 init 脚本。

在这里的第一个复杂性是 Docker 镜像根本不会保留运行中的进程。当您运行 RUN service mysql start 时,作为该 RUN 步骤的清理的一部分,任何运行中的进程都会被停止。如果您在 Dockerfile 中执行任何操作以启动后台进程或守护程序,它将在下一步发生时不会运行。

Docker 容器仅运行单个前台进程,当该进程退出时,容器也将退出。在这种情况下,您应该将镜像的 CMD 设置为在前台运行数据库服务,例如:

# 而不是 `RUN service ...`
CMD ["mysqld_safe"]

还要考虑使用预构建的 mysql 镜像 而不是构建自己的。

service 还有另外两个常见问题。有时人们尝试设置 CMD service x start,但由于 service 命令会立即返回,容器也会退出。在某些设置中,这些命令依赖于运行中的 systemd,但这通常不在容器中运行,并且作为一个庞大的综合系统设置工具和 init,它不太适用于容器环境。)

英文:

Commands like service broadly just don't work in Docker. You should avoid running service or systemctl or init scripts inside a container.

Your first complication here is that a Docker image doesn't persist running processes at all. When you RUN service mysql start, as part of the cleanup for that RUN step, any running processes are stopped. If you do anything to start a background process or daemon in a Dockerfile, it will not be running when the next step happens.

A Docker container runs only a single foreground process and when that process exits the container will exit too. What you should do in this case is set the image's CMD to run the database service in the foreground; for example

# instead of `RUN service ...`
CMD ["mysqld_safe"]

Also consider using the prebuilt mysql image over building your own.

(There are two other common problems with service. You see occasional questions where people try to set CMD service x start, but since the service command returns immediately the container will exit as well. On some setups these commands depend on a running systemd, but this isn't normally running in a container, and as a heavyweight combined system-setup tool and init it doesn't quite fit in a container environment.)

huangapple
  • 本文由 发表于 2023年3月12日 09:12:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定