Cannot start apache automatically with docker - apache

I made a simple custom docker setup for php development. So far everything works as expected. The only thing that I cannot figure out is why apache2 does not start automatically.
Here is my dockerfile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf
RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
RUN service apache2 restart
And here is my docker-compose.yml:
version: '2'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: myexampleapp
ports:
- 8080:80
tty: true
And here is the output docker-compose up command:
me#mydell:~/workspace/mydockercompose$ docker-compose up -d --build
Creating network "mydockercompose_default" with the default driver
Building app
Step 1/7 : FROM ubuntu:latest
---> f975c5035748
Step 2/7 : RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
---> Using cache
---> 148c3a9d928a
Step 3/7 : COPY . /var/www/example
---> 1fbc1dbacf1e
Step 4/7 : COPY vhost.conf /etc/apache2/sites-available/example.conf
---> 9c08947b09e9
Step 5/7 : RUN a2ensite example
---> Running in 1ef64defe747
Enabling site example.
To activate the new configuration, you need to run:
service apache2 reload
Removing intermediate container 1ef64defe747
---> ca1c8e7e80fc
Step 6/7 : RUN chown -R www-data:www-data /var/www/example/logs
---> Running in 57b0214be7a0
Removing intermediate container 57b0214be7a0
---> b3b270a36bf4
Step 7/7 : RUN service apache2 restart
---> Running in 09d2b1d3bd91
* Restarting Apache httpd web server apache2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
...done.
Removing intermediate container 09d2b1d3bd91
---> 19fa9a90f9de
Successfully built 19fa9a90f9de
Successfully tagged myexampleapp:latest
Creating mydockercompose_app_1
It shows clearly that apache was restarted successfully. However it actually does not:
me#mydell:~/workspace/mydockercompose$ docker exec -i -t 20add8ad9895 service apache2 status
* apache2 is not running
I am new to docker, so all suggestions (even if they are not answering this specific question) to improve what I am doing so far are welcome.
Thanks

Docker services have to be running in the foreground. In your Dockerfile, RUN service apache2 restart will start apache as background process. Hence the container will exit.
To run apache in the foreground, add the following to the Dockerfile.
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf
RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

The above answer is probably correct that mentioned that you can start it with:
CMD apachectl -D FOREGROUND
With docker it is sometimes a good idea to use the absolute path to a binary. So for example maybe do this instead:
/usr/sbin/apache2 -D FOREGROUND
I had a look a bit on google to see what other people are doing. I found this example of a dockerfile where the guy mentions a script start.sh:
From here: https://github.com/jacksoncage/apache-docker/blob/master/Dockerfile
EXPOSE 80
ADD start.sh /start.sh
RUN chmod 0755 /start.sh
CMD ["bash", "start.sh"]
Here is the start.sh script: https://github.com/jacksoncage/apache-docker/blob/master/start.sh
Which simply just does:
#!/bin/bash
# Start apache
/usr/sbin/apache2 -D FOREGROUND
Unrelated tip:
Your dockerfile you need to pin the version for Ubuntu.
See: https://nickjanetakis.com/blog/docker-tip-18-please-pin-your-docker-image-versions
Let me know if this helps.

If it help you or anybody, i was preparing working example with complete docker-composer:
https://github.com/marekz/docker-composer-example
FROM ubuntu:18.04
EXPOSE 80
ENV TZ=Europe/Warsaw
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y libapache2-mod-php7.2 php7.2 php7.2-cli php7.2-gd php7.2-intl php7.2-mbstring php7.2-mysql php7.2-xml php7.2-xsl php7.2-bcmath php7.2-zip php-apcu npm lynx
RUN apt-get install -y mysql-client composer screen tmux vim nano iputils-ping
ENTRYPOINT service apache2 restart && bash

By analyzing the statements, I found a solution to my error
File: Dockerfile
FROM debian
RUN apt-get update -qq >/dev/null && apt-get install -y -qq procps telnet apache2 php7.3 -qq >/dev/null
RUN useradd --user-group --create-home --shell /bin/false app
RUN mkdir /data && chown -R app /data && chmod 777 /data
COPY php.conf /etc/apache2/mods-available/php7.3.conf
RUN a2enmod userdir && a2enmod php7.3
Error:
To activate the new configuration, you need to run:
service apache2 reload
Solutions:
FROM debian -> FROM debian:10
The reason was some conflict between the debian system and the php version. The latest version of debian has a newer version of PHP (PHP 7.4 at that time).

Related

SSL in production server using docker-compose and a dockerised apache2

I have a docker-compose production environment that comprises 4 different services running in an AWS ec2 instance. Everything is running fine, and one of the services is exposed to the world via mapping the internal private ip address to ec2's public ip address.The exposed service is a laravel-vue.js app running in an apache2 server using the image
FROM php:7.2-apache
and some custom commands below. My employer now bought a domain (not from Amazon route 53 but a different provider) and I want to get an ssl certificate for said domain and use it in my apache server. I have never put an ssl certificate before but i kind of understand the theory. My first step was to redirect the domain name from the provider to the public IP address. Now the app is accessible from the domain name via http instead of visiting an ip address.
I'm not sure what the next step should be. Can I get any ssl certificate e.g. from let's encrypt or digicert and place it in my apache server?Would the ssl work only if issued from the domain provider? In my service, would I only have to change the configuration to allow traffic through ssl and I'm done? My apache server doesn't do any dns, it simply exposes a webroot to port 80 and my docker-compose file forwards that ip address so that it becomes accessible to the world.Do I need to change apache dns settings and include the domain name?
This is my relevant docker-compose file part:
app:
ports:
- "172:31.31.159:80:80"
build:
context: .
dockerfile: app/Dockerfile
stdin_open: true
environment:
APACHE_DOCUMENT_ROOT: /var/www/html/public
depends_on:
- api
- app-db
And here is the dockerfile:
FROM php:7.2-apache
COPY /app /var/www/html
WORKDIR /var/www/html
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# update apt-get
RUN apt-get update
# install the required components
RUN apt-get install -y libmcrypt-dev g++ libicu-dev libmcrypt4 zlib1g-dev git libpq-dev libmagickwand-dev
RUN apt-get install zip unzip
# install the PHP extensions we need
RUN docker-php-ext-install pdo pdo_mysql
#RUN apt-get install php-zip
# delete the lists for apt-get as the take up space we do not need.
RUN rm -rf /var/lib/apt/lists/*
# install composer globally so that you can call composer directly
RUN curl -sSL https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
RUN composer install --optimize-autoloader --no-dev
# enable apache rewrite
RUN a2enmod rewrite
RUN service apache2 restart
RUN apt-get update && \
apt-get install -y --no-install-recommends gnupg && \
curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
apt-get update && \
apt-get install -y nodejs && \
npm install -g npm
RUN npm install
RUN npm audit fix
RUN npm run production
# set www permissions
RUN chown -R www-data:1001 .
RUN usermod -u 1001 www-data
RUN php artisan config:cache
Feel free to call out any problems with the dockerfile or docker-compose files as this is my first time using them for production.

Installing Apache OpenWhisk using docker-compose on Amazon Linux instance gets stuck at "waiting for the Whisk invoker to come up..."

I am trying to install openwhisk for dev mode using docker compose on Amazon Linux EC2 Instance. I am following this link for doing so https://github.com/apache/incubator-openwhisk -> Get Started.
Although, it has worked for me before once, in this installation, I am facing an issue. These are the steps I followed:
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo chkconfig docker on
sudo yum install -y python-pip
sudo pip install docker-compose
This step is because open whisk is using sudo for docker-compose, and based on previous steps, sudo docker-compose -v gives a command not found.
sudo cp /usr/local/bin/docker-compose /bin
sudo yum install -y git
cd ~
git clone https://github.com/apache/incubator-openwhisk-devtools.git
cd incubator-openwhisk-devtools/docker-compose
sudo make quick-start
Update: The problem is that the make command gets stuck at the stage where it is waiting for invokers.
I have not made any changes to any source code, nor I did any other steps before this on the instance. It was a freshly created instance.
Am I missing something in OpenWhisk or EC2 or the combination of both? Any help would be great.
Update: I tried the docker-compose method for installing open whisk on Amazon Linux 1, Amazon Linux 2, Ubuntu 16.04 as well as Ubuntu 14.04. On all platforms, it got stuck at sudo make quick-start where it is waiting for invokers.
Update: Instead of using python-pip for docker-compose installation, used the command from docker website as well.
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
if docker-compose command is not found it means you didn't install docker-compose correctly.
I don't think you can install docker-compose as a python library using pip
See the instructions here https://docs.docker.com/compose/install/#install-compose
someting like
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
Or take a look at https://medium.com/#khandelwal12nidhi/docker-setup-on-aws-ec2-instance-c670ff3d5f1b

Docker container closes after starting Apache

OK, So I've set up my docker container as I need it.
This is my current Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y \
build-essential sudo software-properties-common \
libboost-dev libboost-filesystem-dev libboost-program-options-dev \
libboost-regex-dev libboost-system-dev libboost-thread-dev \
libicu-dev libtiff5-dev libfreetype6-dev libpng12-dev \
libxml2-dev libproj-dev libsqlite3-dev libgdal-dev \
libcairo-dev libharfbuzz-dev
RUN apt-get install -y postgresql postgresql-contrib
RUN apt-get install -y nodejs
RUN apt-get install -y python3-dev python-dev git python-pip \
python-setuptools python-wheel python3-setuptools \
python3-pip python3-wheel python-cairo-dev libboost-python-dev
RUN apt-get install -y ruby ruby-dev
RUN apt-get install -y wget curl
RUN pip install --upgrade pip
RUN pip install mapnik
RUN sudo gem install awesome_print colorize twitter_cldr \
nokogiri unidecoder
RUN apt-get -y install apache2 php-pear lynx-cur
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 80
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
If I build it and run it with docker run -p 80:80 -it mycontainer, it runs fine. I can go via the terminal, do a /usr/bin/apache2ctl start, the server starts as usual and I can access it via localhost from my host's browser. Great!
Now, the idea is to simply put that /usr/bin/apache2ctl start command inside my Dockerfile, so that I don't have to write it every time the container starts.
However: if I put CMD ["/usr/sbin/apache2ctl", "start"] at the very end of my Dockerfile, build it and run it (with docker run -p 80:80 -it mycontainer), the container seems to start, outputs an Apache message and then it stops, no terminal, nothing. (nor does it run in the background).
What goes on? I just want to be able to start Apache automatically and keep the terminal live, so that I can do things there too.
Containers are a tool to isolate a running application, and they run until the application that was launched exits. If this application is a shell or command that launches a daemon in the background like you've done, when the shell or command returns, the container will promptly exit.
The solution is to run your application in the foreground. The steps to do this with Apache have already been done with the official images which I'd recommend using over building your own. You can see their Dockerfile here. And you can use their image on Dockerfile.

Unable to login to guacamole

I am very new to the guacamole project. I installed it following instruction online. But I am not able to login to the guacamole application. The instructions I followed are,
sudo apt-get update
sudo ufw enable
sudo ufw allow 22 && sudo ufw allow 8080
sudo apt-get install fail2ban build-essential htop libcairo2-dev libjpeg62-dev libpng12-dev libossp-uuid-dev tomcat7 -y
sudo apt-get install libfreerdp-dev libpango1.0-dev libssh2-1-dev libtelnet-dev libvncserver-dev libpulse-dev libssl-dev libvorbis-dev -y
sudo wget http://sourceforge.net/projects/guacamole/files/current/source/guacamole-server-0.9.8.tar.gz
tar -xzf guacamole-server-0.9.8.tar.gz
cd guacamole-server-0.9.8/
sudo ./configure --with-init-dir=/etc/init.d && sudo make && sudo make install
sudo ldconfig
sudo update-rc.d guacd defaults
sudo mkdir /etc/guacamole
sudo gedit /etc/guacamole/guacamole.properties
sudo gedit /etc/guacamole/user-mapping.xml
sudo mkdir /usr/share/tomcat7/.guacmole
sudo ln -s /etc/guacamole/guacamole.properties /usr/share/tomcat7/.guacmole
sudo wget http://sourceforge.net/projects/guacamole/files/current/binary/guacamole-0.9.8.war
sudo mv guacamole-0.9.8.war /var/lib/tomcat7/webapps/guacamole.war
sudo service guacd start
sudo service tomcat7 start
My user-mapping.xml file is configured as below,
<user-mapping>
<authorize username="sandesha" password="sandy" >
<connection name="RDP">
<protocol>rdp</protocol>
<param name="hostname">103.5.133.3</param>
<param name="port">22</param>
</connection>
</authorize>
</user-mapping>
When I hit localhost:8080/guacamole, I am getting one login screen, but if I enter the credential information I configured in user-mapping.xml, it is throeing invalid-credential error. How to resolve this?
The error you are getting indicates that the Guacamole application cannot find user-mapping.xml file. Normally, this file is stored in the following locations:
directory specified by system property guacamole.home
directory specified by environment variable GUACAMOLE_HOME
directory .guacamole located in the tomcat home directory
From the instructions you have posted it looks like you are using the last option. However, your directory is '.guacmole' with one 'a' missing. The two lines from your instructions should be:
sudo mkdir /usr/share/tomcat7/.guacamole
sudo ln -s /etc/guacamole/guacamole.properties /usr/share/tomcat7/.guacamole

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