代码之家  ›  专栏  ›  技术社区  ›  bos570

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

  •  0
  • bos570  · 技术社区  · 8 年前

    在开始之前,我会说我对docker的世界很陌生,尽管阅读了文档,但我仍然对一些事情有点困惑。

    我想用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撰写。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。 为什么安装失败?
    问题2。 这样做正确吗?我的centos dockerfile和apache+php应该分开吗。如果是,这是如何工作的?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Yannoff    8 年前

    systemctl 可能未提供CentOS docker图像。

    事实上,docker服务并不打算作为守护进程运行,而是在前台运行。看看 apache's original http-foreground shell script 为了更好地理解这个概念。

    问题2。不,这不是正确的方式。

    运行apache是 entrypoint command 剧本

    因此 RUN your-command-to-run-apache ,它宁愿是 CMD your-command-to-run-apache

    再一次 Apache official repository 我可以给你一些线索。

        2
  •  0
  •   Guido U. Draheim    8 年前

    在我看来,这些DockerFile看起来太旧了,因为它们试图在容器内映射外部docker守护进程。这是一种变通方法,因为systemd守护进程不能在容器中单独运行。

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

    推荐文章