gpt4 book ai didi

docker - 无法在容器内运行 mysql,该容器使用 dockerFile 构建镜像

转载 作者:行者123 更新时间:2023-12-02 18:38:07 26 4
gpt4 key购买 nike

我试图用 dockerFile 构建一个 mysql 镜像。
以下是我的代码:

FROM ubuntu
MAINTAINER jimmy

#install mysql
RUN apt-get update
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client
RUN apt-get install -y libmysqlclient-dev

#set permission of mysql
RUN service mysql stop
RUN usermod -d /var/lib/mysql/ mysql
RUN chown -R mysql:mysql /var/lib/mysql

当我运行 docker build -t mysql-test 。 --no-cache 它将成功构建图像。

但是,如果我在其中构建一个容器,然后运行 ​​ 服务mysql启动 ,它会失败。
* Starting MySQL database server mysqld                     [fail] 

我必须输入 chown -R mysql:mysql/var/lib/mysql 再次,在容器内,以便 服务mysql启动可以成功运行。

这是为什么?我已经 运行 chown -R mysql:mysql/var/lib/mysql 在 dockerFile 中不是吗?

最佳答案

尝试定义/var/lib/mysql目录作为 Dockerfile 中的卷-

FROM ubuntu
MAINTAINER jimmy

#install mysql
RUN apt-get update -y
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client
RUN apt-get install -y libmysqlclient-dev

RUN echo yes
VOLUME /var/lib/mysql #change

RUN mkdir -p /var/run/mysqld #change
RUN usermod -d /var/lib/mysql/ mysql
RUN find /var/lib/mysql -type f -exec touch {} \; #change
RUN chown -R mysql:mysql /var/lib/mysql /var/run/mysqld #change

这个 Dockerfile 为我解决了这个问题。 Docker 建议您在 docker 中使用 mysql 服务时使用卷。

似乎它仍然是一个悬而未决的问题 - https://github.com/docker/for-linux/issues/72

引用 - https://github.com/moby/moby/issues/35503

详解 -

Yes, it's known that overlayfs only supports a subset of the POSIX standard (also see docker/for-linux#72 (comment), thanks Akihiro)

Generally, stateful data should not be stored in the container's filesystem but in a volume. Not only does that allow you to upgrade containers without loosing your data (which is even more relevant when running those containers as part of a service, because updating a service will replace containers with a new one), it also has a big impact on performance, as writing in the container's filesystem will add the overhead of copy-on-write filesystem that's used in containers.

See the "volumes" section in the documentation for more information; https://docs.docker.com/engine/admin/volumes/, and the "Where to Store Data" section of the official MySQL image on Docker Hub https://hub.docker.com/_/mysql/

If your goal is to run a MySQL container, I would definitely recommend looking at the official MySQL images, in which mysql is setup for running in a container, and which also allow you to initialise new instances from a backup using the /docker-entrypoint-initdb.d (see the "Initializing a fresh instance" section in the MySQL readme on Docker Hub)

关于docker - 无法在容器内运行 mysql,该容器使用 dockerFile 构建镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51872198/

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