Rayhan Muktader
Rayhan Muktader

Reputation: 2106

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

Upvotes: 56

Views: 128671

Answers (6)

vishal pambla
vishal pambla

Reputation: 31

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"]

Upvotes: 3

akp_24
akp_24

Reputation: 41

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.

Upvotes: 4

Vishal Raut
Vishal Raut

Reputation: 21

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 

Upvotes: 2

jbailey
jbailey

Reputation: 327

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"]

Upvotes: 8

Radek
Radek

Reputation: 499

For me last line with CMD was wrong:

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

Upvotes: 28

Bukharov Sergey
Bukharov Sergey

Reputation: 10185

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

Upvotes: 105

Related Questions