53

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
Rayhan Muktader
  • 2,038
  • 2
  • 15
  • 32

6 Answers6

102

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • it did not work. I copied and pasted your Dockerfile, ran `docker build -t my_img .`, ran `docker run -ti -p80:80 --name 'test2' my_img /bin/bash` but apache was not running. – Rayhan Muktader Jun 05 '17 at 21:26
  • i tried this approach and it works for me. Try to remove `/bin/bash` from your command – Bukharov Sergey Jun 05 '17 at 21:27
  • If you try to run multiple services in one container it will not work. Docker way is one container for one service.Please explain your issue and i will try to help you to solve it with docker true way. – Bukharov Sergey Jun 05 '17 at 21:40
  • I'm trying to pull down a live installation of redmine 1.3 from a production a server and run it on my local machine with docker. I need to have the same version of everything. I did not expect it to be this tough. Maybe docker isn't the right solution for something like this. – Rayhan Muktader Jun 05 '17 at 23:29
  • 2
    This worked for me, after hours of toiling over other things that most definitely did not work. Thank you, @BukharovSergey!!! A thousand internet points to you today. – ingernet Aug 02 '17 at 16:37
  • What does `-D` mean? I cannot find in [apachectl documentation](https://documentation.help/httpd-2.4/apachectl.html) – ikhvjs Mar 29 '22 at 13:45
  • @ikhvjs That doc shows `apachectl [ httpd-argument ]` and explains that "When acting in pass-through mode, apachectl can take all the arguments available for the httpd binary". That one references the `-D parameter` option and says: "Sets a configuration parameter which can be used with sections in the configuration files to conditionally skip or process commands at server startup and restart. Also can be used to set certain less-common startup parameters including -DNO_DETACH (prevent the parent from forking) and -DFOREGROUND (prevent the parent from calling setsid() et al)." – Ethan Jan 24 '23 at 23:04
  • @Ethan, thanks for your comment. I also found the documentation from the [next page](https://documentation.help/httpd-2.4/apxs.html) – ikhvjs Jan 25 '23 at 07:41
26

For me last line with CMD was wrong:

# it helped me
CMD ["apachectl", "-D", "FOREGROUND"]
Radek
  • 469
  • 4
  • 8
8

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"]
jbailey
  • 309
  • 4
  • 6
4
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.

akp_24
  • 41
  • 3
2
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"]
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • 5
    Please, edit your answer to add some explanations. – Syscall Jan 20 '22 at 10:28
  • See "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". While this might be technically correct it doesn't explain why it solves the problem or should be the selected answer. We should educate in addition to help solve the problem. – the Tin Man Jan 21 '22 at 23:06
1

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 
Eldar B.
  • 1,097
  • 9
  • 22
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '21 at 08:20