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
Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.
One of my objectives for running Apache in a Docker container is to have a uniform and repeatable process for getting Apache up and running on any development machine (and configured for the project at hand). Enter Docker: you build, you run, it works. Or at least in theory. The general idea for the Docker Apache container is as follows:
To begin, let’s install Docker using the following command. This will download and run a shell script that will add the Docker repository to our system and install the package. Next, use systemctl command to start the main Docker service and check its status.
Start containers automatically. Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.
The actual executable file that is the Apache webserver is named httpd in this distro. So once the container has been initialized by Docker, we want to run the /usr/sbin/httpd program. The way to tell Docker what to run once the container is initialized is by way of the CMD statement.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With