无法使用docker自动启动apache

Kar*_*Mtl 1 apache ubuntu docker dockerfile docker-compose

我为php开发做了一个简单的自定义docker设置.到目前为止一切都按预期工作.我唯一无法弄清楚的是为什么apache2不会自动启动.

这是我的dockerfile:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl

COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf

RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
RUN service apache2 restart
Run Code Online (Sandbox Code Playgroud)

这是我的docker-compose.yml:

version: '2'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: myexampleapp
    ports:
        - 8080:80
    tty: true
Run Code Online (Sandbox Code Playgroud)

这是输出docker-compose up命令:

me@mydell:~/workspace/mydockercompose$ docker-compose up -d --build
Creating network "mydockercompose_default" with the default driver
Building app
Step 1/7 : FROM ubuntu:latest
 ---> f975c5035748
Step 2/7 : RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
 ---> Using cache
 ---> 148c3a9d928a
Step 3/7 : COPY . /var/www/example
 ---> 1fbc1dbacf1e
Step 4/7 : COPY vhost.conf /etc/apache2/sites-available/example.conf
 ---> 9c08947b09e9
Step 5/7 : RUN a2ensite example
 ---> Running in 1ef64defe747
Enabling site example.
To activate the new configuration, you need to run:
  service apache2 reload
Removing intermediate container 1ef64defe747
 ---> ca1c8e7e80fc
Step 6/7 : RUN chown -R www-data:www-data /var/www/example/logs
 ---> Running in 57b0214be7a0
Removing intermediate container 57b0214be7a0
 ---> b3b270a36bf4
Step 7/7 : RUN service apache2 restart
 ---> Running in 09d2b1d3bd91
 * Restarting Apache httpd web server apache2
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
   ...done.
Removing intermediate container 09d2b1d3bd91
 ---> 19fa9a90f9de
Successfully built 19fa9a90f9de
Successfully tagged myexampleapp:latest
Creating mydockercompose_app_1
Run Code Online (Sandbox Code Playgroud)

它清楚地表明apache已成功重启.但实际上并没有:

me@mydell:~/workspace/mydockercompose$ docker exec -i -t 20add8ad9895 service apache2 status
 * apache2 is not running
Run Code Online (Sandbox Code Playgroud)

我是码头工的新手,所以所有的建议(即使他们没有回答这个具体的问题)来改善我到目前为止所做的工作都是值得欢迎的.

谢谢

Sho*_*oan 9

Docker服务必须在前台运行.在您的Dockerfile中,RUN service apache2 restart将启动apache作为后台进程.因此容器将退出.

要在前台运行apache,请将以下内容添加到Dockerfile.

CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl

COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf

RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
Run Code Online (Sandbox Code Playgroud)