使用Cetnos7 apache和php构建Docker容器。

时间:2017-10-31 14:10:34

标签: php apache docker docker-compose

我先说这是对码头工人世界的新手,尽管阅读文档,我仍然对一些事情感到困惑。

我想用centos7 apache和php构建一个容器。我不想使用已经存在的图像,想要构建自定义容器。我有以下文件夹结构

enter image description here

我的rw / docker / webserver / Dockerfile:

FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]


RUN yum -y install httpd
RUN systemctl start httpd
RUN systemctl enable httpd
RUN yum update -y && yum install -y libpng-dev curl libcurl4-openssl-dev

RUN docker-php-ext-install pdo pdo_mysql gd curl

RUN a2enmod rewrite

我的docker-compose.yml

    version: '2'

services: 
    webserver: 
        build: ./docker/webserver
        ports:
            - "80:80"
            - "443:443"
        volumes: 
            - /**PATH**/rw/services:/var/www/html
        links:
            - db

    db: 
        image: mysql:5.7
        ports: 
           - "3306:3306"
        volumes: 
           - ./db:/var/lib/mysql
        environment: 
            - MYSQL_ROOT_PASSWORD=****
            - MYSQL_DATABASE=****

当docker尝试使用错误启动httpd时,此操作失败      ERROR: Service 'webserver' failed to build: The command '/bin/sh -c systemctl start httpd' returned a non-zero code: 1

Q1。为什么安装失败?
Q2。这是正确的方法吗?我的dockerfile for centos和apache + php应该是分开的吗?如果是,那怎么办?

2 个答案:

答案 0 :(得分:0)

Q1。我认为systemctl可能没有提供CentOS docker镜像。

实际上,docker服务并不是作为守护进程运行,而是在前台运行。请查看apache's original http-foreground shell script以更好地理解该概念。

Q2。这不是恕我直言的正确方式。

运行apache是​​entrypointcommand脚本的作用。

因此,而不是RUN your-command-to-run-apache,而是CMD your-command-to-run-apache

再一次,Apache official repository可以为您提供一些线索。

答案 1 :(得分:0)

在我看来,这些Dockerfiles看起来太老了,因为他们试图将外部docker守护进程映射到容器内部。作为systemd守护程序的解决方法不能在容器中单独运行。

相反,我正在使用docker-systemctl-replacement脚本。 docker systemctl.py可以解析普通的* .service文件,以了解如何启动和停止服务。您可以将其注册为图像的CMD,在这种情况下,它将查找所有启用了systemctl的服务 - 这些服务将以正确的顺序启动和停止。

甚至还有一些LAMP堆栈的测试用例,所以它应该在你的情况下非常顺利。 systemctl.py脚本与systemd systemctl兼容,只需覆盖映像中的/ usr / bin / systemctl - 所有非docker安装说明都适用于docker构建。