简体   繁体   中英

Starting Apache on Docker container

I am playing with Docker to build an image with Apache and PHP but there is an issue with automatic start of Apache. This is Dockerfile:

FROM ubuntu:16.04

RUN apt-get update

# Apache
RUN apt-get -y install apache2

# Apache config
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN apache2ctl configtest
RUN a2dissite 000-default
COPY otpuskatame_backend.conf /etc/apache2/sites-available
RUN a2ensite otpuskatame_backend
RUN a2dissite 000-default
RUN rm /etc/apache2/sites-available/000-default.conf

# PHP
RUN apt-get -y install php libapache2-mod-php php-mcrypt

# Load container
CMD service apache2 start

EXPOSE 80

The image was built successfully but when run it with following command it breaks:

sudo docker run -d -v /var/www/project/:/var/www:rw -p 80:80 --name=lamp1 lamp1

Status:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS                  NAMES
1a3679275b1d        lamp1                   "/bin/sh -c 'servi..."   8 seconds ago       Exited (0) 6 seconds ago                          lamp1 

Logs look OK:

 * Starting Apache httpd web server apache2

If I remove CMD line from Dockerfile and then log into container and run service apache2 start manually everything works fine. But how to start it automatically?

It is due to main service in docker getting killed and because of this docker is exiting. You need to run service in the foreground

Try adding this to Dockerfile and rebuild it

CMD /usr/sbin/apache2ctl -D FOREGROUND

For more details refer to the site below:

https://writing.pupius.co.uk/apache-and-php-on-docker-44faef716150

It is due to main service in docker getting killed and because of this docker is exiting, you can execute:

docker run -d -t -p 80:80 --name apache image bash -c "service apache2 start && sleep 60"

The container will live for 60 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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