简体   繁体   中英

How to start apache2 automatically in a ubuntu docker container?

I am trying to create a Dockerfile that will start apache automatically. Nothing has worked. But If I log into the container and run service apache2 start it works. Why can I not run that command from my Dockerfile?

FROM ubuntu

# File Author / Maintainer
MAINTAINER rmuktader

# Update the repository sources list
RUN apt-get update

# Install and run apache
RUN apt-get install -y apache2 && apt-get clean

#ENTRYPOINT ["/usr/sbin/apache2", "-k", "start"]


#ENV APACHE_RUN_USER www-data
#ENV APACHE_RUN_GROUP www-data
#ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80
CMD service apache2 start

The issue is here: CMD service apache2 start When you execute this command process apache2 will be detached from the shell. But Docker works only while main process is alive.

The solution is to run Apache in the foreground . Dockerfile must look like this: (only last line changed).

FROM ubuntu

# File Author / Maintainer
MAINTAINER rmuktader

# Update the repository sources list
RUN apt-get update

# Install and run apache
RUN apt-get install -y apache2 && apt-get clean

#ENTRYPOINT ["/usr/sbin/apache2", "-k", "start"]


#ENV APACHE_RUN_USER www-data
#ENV APACHE_RUN_GROUP www-data
#ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80
CMD apachectl -D FOREGROUND

For me last line with CMD was wrong:

# it helped me
CMD ["apachectl", "-D", "FOREGROUND"]

My project was slightly different where I installed a bunch of other stuff, but the apache start portion matched above. Once I built this image and used it, my server started fine.

FROM ubuntu:latest

#install all the tools you might want to use in your container
RUN apt-get update
RUN apt-get install curl -y
RUN apt-get install vim -y
#the following ARG turns off the questions normally asked for location and timezone for Apache
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get install apache2 -y

#change working directory to root of apache webhost
WORKDIR var/www/html

#copy your files, if you want to copy all use COPY . .
COPY index.html index.html

#now start the server
CMD ["apachectl", "-D", "FOREGROUND"]

Simple docker file for run ubuntu with apache server

RUN apt-get update
ARG DEBIAN_FRONTEND=noninteractive  # for remove time zone
RUN apt-get -y install apache2
ADD . /var/www/html
ENTRYPOINT apachectl -D FOREGROUND
ENV name Vishal                   # anything you can give 
FROM ubuntu

RUN apt update

ARG DEBIAN_FRONTEND=noninteractive
RUN apt install apache2 -y && apt-get clean

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80
CMD ["apachectl", "-D",  "FOREGROUND"]

Adding the ARG DEBIAN_FRONTEND=noninteractive should do the trick.

FROM ubuntu
RUN apt-get update
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get -y install apache2
ADD index.html /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]

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