【发布时间】:2020-07-28 12:27:15
【问题描述】:
我有一个 Dockerfile:
FROM php:7.2-apache
COPY ./mysql-init.sh /etc/
RUN apt-get update && apt-get install -y \
mariadb-server \
mariadb-client \
&& docker-php-ext-install mysqli && docker-php-ext-enable mysqli && \
chmod +x /etc/mysql-init.sh && /etc/init.d/mysql start && /etc/mysql-init.sh
COPY ./public-html/ /var/www/html/
CMD service mysql start && service apache2 start && bash
构建镜像:
docker build -t vhost .
并运行容器:
docker run -dt -p 8080:80 --name test vhost
一切正常,这是容器日志:
[ ok ] Starting MariaDB database server: mysqld.
[....] Starting Apache httpd web server: apache2ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
. ok
但是当我创建一个服务时在群中:
docker service create -p 8080:80 --name test vhost
服务失败并重新启动。这是服务日志:
test.1.5ywz5uq6p9qc@docker-VirtualBox | Starting MariaDB database server: mysqld.
test.1.5ywz5uq6p9qc@docker-VirtualBox | ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
test.1.5ywz5uq6p9qc@docker-VirtualBox | AH00557: apache2: apr_sockaddr_info_get() failed for 58ff827d9d51
test.1.5ywz5uq6p9qc@docker-VirtualBox | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
.
.
.
test.1.vvd65rc7xxco@docker-VirtualBox | Starting Apache httpd web server: apache2.
test.1.u5nepc5a1jib@docker-VirtualBox | Starting MariaDB database server: mysqld.
test.1.u5nepc5a1jib@docker-VirtualBox | ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
test.1.u5nepc5a1jib@docker-VirtualBox | AH00557: apache2: apr_sockaddr_info_get() failed for 1cd85211c4a7
test.1.u5nepc5a1jib@docker-VirtualBox | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
.
.
.
如何让服务运行一次并继续运行?
【问题讨论】:
-
这看起来像你想要两个独立的容器(用于 MySQL 和 Apache)。您现在拥有的容器是一个主要运行交互式 shell(在 Swarm 中可能会失败)的容器,并且恰好运行“有趣”的东西作为副作用。
-
嗨大卫。实际上我要建立一个主机,我需要一个容器,在每个主机的一个容器中包含 apache php 和 mariadb。你有什么想法可以解决这个问题吗?
-
虚拟机更自然地支持具有主管和多个进程的初始化系统。您可以使用 Ansible 等工具在 VM 中安装软件,并使用 Packer 等工具根据您的特定虚拟化技术构建固定的 VM 映像。如果你真的想在 Docker 中运行它,你可以,但重要的是你的主进程不是交互式 shell。