简体   繁体   English

运行Docker容器时自动启动mysql

[英]Start mysql automatically when running a Docker container

I have created a Dockerfile to have a development environment on my computer with php7, mysql and some other library. 我创建了一个Dockerfile以在我的计算机上具有php7,mysql和其他一些库的开发环境。

The apache service starts automatically when a run the container but I can't get the same results with mysql. 当运行容器时,apache服务会自动启动,但是我无法通过mysql获得相同的结果。 The last sentence of my Dockerfile looks like this CMD ["bash", "run.sh"] and then in run.sh I have this: 我的Dockerfile的最后一句看起来像这样的CMD ["bash", "run.sh"] ,然后在run.sh中,我有这个:

#!/bin/bash
set -e

PHP_ERROR_REPORTING=${PHP_ERROR_REPORTING:-"E_ALL"}
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php/7.0/apache2/php.ini
sed -ri 's/^display_errors\s*=\s*Off/display_errors = On/g' /etc/php/7.0/cli/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php/7.0/apache2/php.ini
sed -ri "s/^error_reporting\s*=.*$//g" /etc/php/7.0/cli/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php/7.0/apache2/php.ini
echo "error_reporting = $PHP_ERROR_REPORTING" >> /etc/php/7.0/cli/php.ini

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND

chmod 0755 start.sh
sleep 1
start.sh

/bin/bash

That calls a file named start.sh that looks like this: 调用名为start.sh的文件,如下所示:

#!/usr/bin/env bash

# Mysql
sed -i -e 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
service mysql start

It is divided in two files because I tested everything but I can't get it working. 它分为两个文件,因为我测试了所有内容,但无法正常工作。 If I do a docker exec inside the container and I start mysql everything works fine. 如果我在容器内执行docker exec并启动mysql,一切正常。

Any idea of what I am doing wrong? 任何我做错事的想法吗?

You are running exec /usr/sbin/apache2 -DFOREGROUND relatively early in the run.sh script. 您在run.sh脚本中相对较早地运行exec /usr/sbin/apache2 -DFOREGROUND The thing about exec is that it does not start a sub-process of the shell, but it replaces the shell with a new process. 关于exec的事情是它不启动shell的子进程,而是用新进程替换了shell。

From the man page : 手册页

exec [-cl] [-a name] [command [arguments]]

If command is specified, it replaces the shell. 如果指定了command,它将替换shell。 No new process is created. 没有创建新的过程。 The arguments become the arguments to command. 自变量成为命令的自变量。

This means that your shell script will not continue running after invoking exec . 这意味着您的shell脚本在调用exec之后将不会继续运行。

I could now suggest a number of ways to hack around this with Bash, but all of these will be (in my opinion) messy and difficult to maintain. 我现在可以建议使用Bash解决此问题的多种方法,但所有这些(在我看来)将是混乱且难以维护的。 If you want to manage multiple processes within one container (like Apache and MySQL), I strongly suggest having a look at process managers like supervisord . 如果您想在一个容器(例如Apache和MySQL)中管理多个流程,我强烈建议您看一看诸如supervisor之类的流程管理器。 There's an excellent article in the official documentation that even uses a similar use case to yours as an example. 官方文档中有一篇出色的文章 ,甚至使用了与您类似的用例作为示例。

start.sh is given with no absolute or relative path, like if it was in your PATH, is it ? start.sh没有绝对或相对路径,就像它在您的PATH中一样,是吗?

You could add some "echo 'I am actually called' > log.txt" at the beginning of start.sh to check if it's even called. 您可以在start.sh的开头添加一些“ echo'我实际上被称为'> log.txt”,以检查它是否被调用。

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

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