简体   繁体   English

为什么 dockerfile 不能用“ CMD /etc/init.d/mysql start ”启动数据库?

[英]why dockerfile can't start db with " CMD /etc/init.d/mysql start "?

I downloaded a docker image with mariadb and phpmyadmin, then wrote two dockerfiles below..我用mariadb和phpmyadmin下载了一个docker镜像,然后在下面写了两个dockerfiles..

# dockerfile A
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin

CMD /etc/init.d/mysql start && /etc/init.d/apache2 start
# dockerfile B
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin

CMD service mysql start && /usr/sbin/apachectl -D FOREGROUND 

dockerfile B worked well, dockerfile B 运行良好,

but dockerfile A failed.但是 dockerfile A 失败了。

I can build image from dockerfileA,我可以从 dockerfileA 构建图像,

then spin-up container from it docker run -it -p 80:80 <img id> bash然后从中启动容器docker run -it -p 80:80 <img id> bash

the container up successfully,容器成功启动,

but while I inside the container, I found the services of mariadb and apache2 not working.但是当我进入容器时,我发现 mariadb 和 apache2 的服务不起作用。

After I execute /etc/init.d/mysql start && /etc/init.d/apache2 start ,在我执行/etc/init.d/mysql start && /etc/init.d/apache2 start

mariadb and apache2 works! mariadb 和 apache2 有效!

Trying to get error messages by docker logs <container id> , but got nothing.试图通过docker logs <container id>获取错误消息,但什么也没得到。

What my question is我的问题是什么

"If I run the docker image without dockerfile just by commands, “如果我只通过命令运行没有 dockerfile 的 docker 镜像,

like what I did in dockerfile A. The container works well.就像我在 dockerfile A 中所做的一样。容器运行良好。 "

$ docker run -it -p 80:80 alfvenjohn/raspbian-buster-mariadb-phpmyadmin bash
$ /etc/init.d/mysql start && /etc/init.d/apache2 start

Why?为什么? Didn't dockerfile A do the same thing, as I spin up my container with commands ?当我使用命令启动容器时,dockerfile A 没有做同样的事情吗?

You need to remove the bash at this end of the command.您需要在命令的这一端删除 bash。 This replace the command inside your dockerfile.这将替换 dockerfile 中的命令。

docker run -d -p 80:80 <img id>

You can use this command to connect inside the container afterward:之后您可以使用此命令在容器内部进行连接:

docker exec -it <container_id> bash 

A Docker image runs a single command only.一个 Docker 镜像只运行一个命令。 When that command exits, the container exits too.当该命令退出时,容器也会退出。

This combination usually means a container runs a single server-type process (so run MySQL and Apache in separate containers), and it means the process must run in the foreground (so the lifetime of the container is the lifetime of the server process).这种组合通常意味着一个容器运行单个服务器类型的进程(因此在单独的容器中运行 MySQL 和 Apache),并且意味着该进程必须运行在前台(因此容器的生命周期就是服务器进程的生命周期)。

In your setup where you launch an interactive shell instead of the image CMD , you say you can start the servers by running在您启动交互式 shell 而不是图像CMD ,您说可以通过运行来启动服务器

/etc/init.d/mysql start && /etc/init.d/apache2 start

This is true, and then you get a command prompt back .这是真的,然后你会得到一个命令提示符 That means the command completed.这意味着命令完成。 If you run this as an image's CMD then "you get a command prompt back" or "the command completed" means "the container will exit".如果您将其作为图像的CMD运行,那么“您将返回命令提示符”或“命令完成”意味着“容器将退出”。

Generally you want to launch separate database and Web server containers;通常,您希望启动单独的数据库和 Web 服务器容器; if you have other application containers you can add those to the mix too.如果您有其他应用程序容器,您也可以将它们添加到组合中。 Docker Compose is a common tool for this. Docker Compose 是一个常用的工具。 You may want to look through Docker's sample applications for some examples, or other SO questions.您可能需要查看 Docker 的示例应用程序以获取一些示例或其他 SO 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM