Apache2 Container on BlueMix won't stay up - apache

I've started with IBM's image:
registry.ng.bluemix.net/ibmnode:latest
It's Ubuntu 14.04, I then add Apache2 on, do some file copies of my site, and then EXPOSE 443. Lastly, I invoke a bash script with the following:
#!/bin/bash
set -e
rm -f /usr/local/apache2/logs/httpd.pid
exec /usr/sbin/apache2ctl -DFOREGROUND
When I run the container locally, it works fine and serves up what I need. When BlueMix builds from the Dockerfile, that works without error. Then deploys to a container successfully. Immediately after deploy, the container registers as 'STOPPED'. Restarting brings it up and then back down within a few seconds. 'cf ic logs my-process-id' doesn't show any feedback.
Other things I've tried:
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Using service apache2 restart
Dockerfile:
FROM registry.ng.bluemix.net/ibmnode:latest
RUN apt-get install -y apache2
RUN apt-get install -y nano
# ADD SSL
RUN a2enmod ssl
RUN a2enmod proxy_http
WORKDIR /var/www/dist
RUN mv ./* /var/www/html
COPY docker/httpd-foreground.sh /usr/local/bin/
EXPOSE 443
CMD ["httpd-foreground.sh"]
httpd-foreground.hs:
#!/bin/bash
set -e
rm -f /usr/local/apache2/logs/httpd.pid
exec /usr/sbin/apache2ctl -DFOREGROUND

What you are trying to do is getting node image, installing apache and overriding the command, and trying to run in the foreground. This is not really a good way to run a apache container on bluemix.
You should do something like this:
1. Follow information in here to pull the httpd image to your local, push the local image to your bluemix name space.
- docker pull httpd:2.4
- docker tag httpd:2.4 registry.ng.bluemix.net//httpd
- docker push registry.ng.bluemix.net//http
2. Once the image is pushed to your namespace, you can create custom image with your Dockerfile, note that I assume you have your website content in public-html folder
FROM registry.ng.bluemix.net//httpd:2.4 COPY ./public-html/
/usr/local/apache2/htdocs/ EXPOSE 80
Build your image
cf ic build --tag myhttp .
Run the container:
cf ic run --name myhttp -p 80 registry.ng.bluemix.net/<yourNameSpace>/myhttp
Bind IP address, using
cf ic bind <IP> myhttp
Access your container with the IP you bound

Related

Why can I access my Apache default page ONLY when I go in my container's bash?

First of all, I would like to say that I'm new to Docker and all that is around it.
I have been wanting to make a container where I have Apache, php and Firebird installed. So far, so good ; everything seems to work and I can get my default page when I type in my Internet search bar my ip address and :8080. I do so by first starting my container like this :
docker run -p 8080:80 -d apps
Where "apps" is the name of my container.
I have achieved this with my Dockerfile, which looks like this (it might be a bit messy, still learning the good practices !) :
# Download of base image - ubuntu 20.04
FROM ubuntu:20.04
# Updating/upgrading
RUN apt-get update -y && apt-get upgrade -y
# Installing apache2, php and firebird with modules
RUN DEBIAN_FRONTEND="noninteractive" apt-get install apache2 php libapache2-mod-php -y && \
apt-get install php-curl php-gd php-intl php-json php-mbstring php-xml php-zip -y && \
DEBIAN_FRONTEND="noninteractive" apt-get install firebird3.0-server -y && apt-get install firebird->
# Start up apache in foreground by default
CMD /usr/sbin/apache2 -D FOREGROUND
ENTRYPOINT service apache2 restart && /bin/bash
# Expose apache
EXPOSE 80
Now, my idea was to export this container to another computer and try the same thing. I have followed a few tutorials and got to import my container on the new machine. My problem here is that somehow, the command I previously used doesn't work ; it shows me this error :
docker: Error response from daemon: No command specified.
See 'docker run --help'.
Which is odd, because it works just fine on the other machine. I also did this command, WHICH WORKS :
docker run -i -t -p 8080:80 apps /bin/bash
This one works alright, but I don't want to have to access the bash everytime I want my Apache page to load. I would want my container to run without me having to get in my container, if that makes sense.
In my opinion, it probably comes from the fact that I only loaded the container, and not the image used to build it (maybe a bad practice? Couldn't find anything about it on google).
Here is my setup just in case ---
On the first machine (which is the one where I created the image and the container) :
Ubuntu 20.04 LTS
Apache/2.4.41
Docker 19.03.8
On the other machine which I'm trying to make my container work :
Ubuntu 18.04 LTS
Apache/2.4.29
Docker 19.03.6
Thank you for your patience and time !
apps is your docker image, if you want to give name for your container you can specify --name in the run command ie,
docker run --name container_name -p 8080:80 -d apps
You can use sudo docker save -o apps.tar apps to create a tar file of the image
then change the root permission of the tar file sudo chmod 777 apps.tar
Copy this tar file to the other system you want to try, then
sudo docker load --input apps.tar
This will load the image, then you can use the previous command to start the container
docker run -p 8080:80 -d apps
Where "apps" is the name of my container. <- This statement is incorrect and perhaps the misunderstood concept that leads you to the problem.
apps is the name of the image, not the name of the container. On the host on which you can run the container, you must have built that image from the Dockerfile that you shared using the command:
docker build -t apps .
Copy the Dockerfile on the host where you cannot run the container, built the image in-there as well and try again running the container.

Unable to start docker - httpd (pid 1) already running

I have hosted one docker with PHP in a shared server of our office environments. Previously it was working fine without any issue. All the users were able to access the site via port forwarding to 8080. Here is my docker file details -
# Choose Repo from Docker Hub
FROM centos:latest
# Provide details of maintainer
MAINTAINER ritu
#Install necessary software
RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
RUN yum -y install yum-utils
RUN yum-config-manager --enable remi-php56
RUN yum -y install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo php-devel php-pear make gcc systemtap-sdt-devel httpd unzip postfix
RUN export PHP_DTRACE=yes
RUN curl -sS https://getcomposer.org/installer | php
RUN mv -f composer.phar /usr/local/bin/composer
RUN chmod +x /usr/local/bin/composer
RUN composer require phpmailer/phpmailer
COPY phpinfo.php /var/www/html/
COPY php.ini /var/www/
COPY httpd.conf /var/www/
RUN cp -f /var/www/httpd.conf /etc/httpd/conf/
COPY *.rpm /var/www/
#Install & Configure OCI for PHP
COPY oci8-2.0.12.tgz /
RUN tar -xvf oci8-2.0.12.tgz
RUN yum -y localinstall /var/www/*.rpm --nogpgcheck
COPY client.sh /etc/profile.d/
RUN chmod +x /etc/profile.d/client.sh
RUN cp -f /var/www/php.ini /etc/
COPY php_oci8_int.h oci8-2.0.12/
COPY Log_Check.zip /
RUN unzip Log_Check.zip
RUN cp -a -R /Log_Check/* /var/www/html/
WORKDIR /oci8-2.0.12
RUN phpize
RUN ./configure --with-oci8=/usr/lib/oracle/12.2/client64
RUN cp -f /usr/include/oracle/12.2/client64/*.h /oci8-2.0.12/include/
RUN make
RUN make install
RUN ls /var/www/html/
RUN rm -rf /var/run/apache2/apache2.pid
#Expose necessary ports
EXPOSE 80
EXPOSE 1521
EXPOSE 25
#Provide Entrypoint
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
Suddenly one of my friend added another docker with same port 8080 in the same server. After that my docker got stopped. with below error -
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
httpd (pid 1) already running
After several hours of googling and after trying lots of commands, I found that its easy to remove the entire container as well as images from the server. Hence I removed all containers with docker rm followed by image deletion with docker rmi. Again i have recreated the docker image on my local system (its working here) and transferred to server. Again I tried to run the docker. But faced same issue again.
Unable to find out the cause & solution. Need some help.
first remove ENTRYPOINT from your Dockerfile and just use:
CMD [ "/usr/sbin/httpd", "-X" ]
the warning regarding AH00558 is comming from your configuration and it i complaining about you do not use www.test.com you can ignore that for now and apache will still working. if you want to read more see this

creating docker container to host website

I want to run static website inside a docker container.
For this i have create ubuntu EC2 machine,installed docker and pulled centos image.
docker pull centos
docker run -td 9f38484d220f bash
docker exec -it aa779e39eb0f bash
===>now inside the container i am using below command
yum update
yum install apache
service httpd start
but i am getting command not recognized error.
Please help me figure out what i am doing wrong.
Also i as i want to run static website i will be putting below code once apache is installed successfully
$touch /var/www/html/index.html
$chkconfig httpd on
$echo "<b>Hii this is my first conatiner running/b>"
>> /var/www/html/index.html
Is this correct way of doing it ?
You installed apache and you are trying to run httpd. Refer this to read the difference between apache2 and httpd. You can run following commands to install apache and run a static hello world page on local host.
$ sudo yum update -y
$ sudo yum install -y httpd
$ sudo service httpd start
$ echo "<html><h1>Hello World!</h1></html>" > test
$ cat test > /var/www/html/index.html
You don't need a container for hosting a static website. S3 is a better choice for this.
If you want to do it as an exercice, considere this simple nginx solution, see: https://hub.docker.com/_/nginx
You have an example in the section : Hosting some simple static content
FROM nginx:alpine
COPY . /usr/share/nginx/html
Remember that you usually don't start a container then start a service inside (for testing and debugging). Entrypoint and command are what start your service, aka what you would manually do.

Dockerized apache server not exposing port 80

I am trying to run a React application inside a docker container. My application image was built with the following Dockerfile:
Dockerfile
FROM node:latest
LABEL autor="Ed de Almeida"
RUN apt-get update
RUN apt-get install -y apache2
RUN mkdir /tmp/myapp
COPY . /tmp/myapp
RUN cd /tmp/myapp && npm install
RUN cd /tmp/myapp && npm run build
RUN cd /tmp/myapp/build && cp -Rvf * /var/www/html
RUN cd /var/www && chown -Rvf www-data:www-data html/
EXPOSE 80
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
CMD /usr/sbin/apache2ctl -D FOREGROUND
As you may see, I create a production build, copy it to the standard directory of the Apache server and then run the Apache server. I even exposed port 80, the Apache default port.
I am creating the image with
docker build -t myimage .
and running the container with
docker run -d -p 80:80 --name myapp myimage
I am probably missing something, because I am new to Docker, because the container is there, up and running, but when I point my browser to http://localhost I got nothing.
I entered the container with
docker exec -it myapp bash
and the application is running fine inside it.
Any hints?
When running on windows, Docker will be running on a virtual machine that is running in the backgound. Thus you need to connect to this virtual machine and not to localhost.
You can get the machine ip by running:
docker-machine ip default
This will give you the IP address of the machine, which you can use to connect from the browser.

Docker CentOS image does not auto start httpd

I'm trying to run a simple Docker image with Apache and a PHP program. It works fine if I run
docker run -t -i -p 80:80 my/httpd /bin/bash
then manually start Apache
service httpd start
however I cant get httpd to start automatically when running
docker run -d -p 80:80 my/httpd
Apache will startup then container exists. I have tried a bunch of different CMDs in my docker file
CMD /etc/init.d/httpd start
CMD ["service" "httpd" "start"]
CMD ["/bin/bash", "/etc/init.d/httpd start"]
ENTRYPOINT /etc/init.d/httpd CMD start
CMD ./start.sh
start.sh is
#!/bin/bash
/etc/init.d/httpd start
However every-time docker instance will exist after apache starts
Am I missing something really obvious?
You need to run apache (httpd) directly - you should not use init.d script.
Two options:
you have to run apache in foreground: /usr/sbin/apache2 -DFOREGROUND ... (or /usr/sbin/httpd in CentOS)
you have to start all services (including apache configured as auto-run) by executing /sbin/init as entrypoint.
Add this line in the bottom of your Dockerfile to run Apache in the foreground on CentOS
ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Simple Dockerfile to run httpd on centOS
FROM centos:latest
RUN yum update -y
RUN yum install httpd -y
ENTRYPOINT ["/usr/sbin/httpd","-D","FOREGROUND"]
Commands for building images and running container
Build
docker build . -t chttpd:latest
Running container using new image
docker container run -d -p 8000:80 chttpd:latest
In the end of Dockerfile, insert below command to start httpd
# Start httpd
ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]
This works for me
ENTRYPOINT /usr/sbin/httpd -D start && /bin/bash