Apache Tomcat 8 not starting within a docker container - apache

I am experimenting with Docker and am very new to it. I am struck at a point for a long time and am not getting a way through and hence came up with this question here...
Problem Statement:
I am trying to create an image from a docker file containing Apache and lynx installation. Once done I am trying to access tomcat on 8080 of the container which is in turn forwarded to the 8082 of the host. But when running the image I never get tomcat started in the container.
The Docker file
FROM ubuntu:16.10
#Install Lynx
Run apt-get update
Run apt-get install -y lynx
#Install Curl
Run apt-get install -y curl
#Install tools: jdk
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
Run curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat- 8/v8.5.12/bin/apache-tomcat-8.5.12.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp opt/tomcat/logs
Run cd /opt/tomcat/bin
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out
When the image is built I tried running the container by the two below methods
docker run -d -p 8082:8080 imageid tail -f /dev/null
While using the above, container is running but tomcat is not started inside the container and hence not accessible from localhost:8082. Also I do not see anything if I perform docker logs longcontainerid
docker run -d -p 8082:8080 imageid /path/to/catalina.sh start tail -f /dev/null
I see tomcat started when I do docker logs longconatainrid
While using the above the container is started and stopped immediately and is not running as I can see from docker ps and hence again not accessible from localhost:8082.
Can anyone please tell me where I am going wrong?
P.s. I searched a lot on the internet but could not get the thing right. Might be there is some concept that i am not getting clearly.

Looking at the docker run command documentation, the doc states that any command passed to the run will override the original CMD in your Dockerfile:
As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND
1/ Then when you run:
docker run -d -p 8082:8080 imageid tail -f /dev/null
The container is run with COMMAND tail -f /dev/null, the original command starting tomcat is overridden.
To resolve your problem, try to run:
docker run -d -p 8082:8080 imageid
and
docker log -f containerId
To see if tomcat is correctly started.
2/ You should not use the start argument with catalina.sh. Have a look at this official tomcat Dokerfile, the team uses :
CMD ["catalina.sh", "run"]
to start tomcat (when you use start, docker ends container at the end of the shell script and tomcat will start but not maintain a running process).
3/ Finally, why don't you use tomcat official image to build your container? You could just use the :
FROM tomcat:latest
directive at the beginning of your Dockerfile, and add you required elements (new files, webapps war, settings) to the docker image.

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

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.

"no such file or directory" when running Docker image

I'm new to Docker and trying to create an image with owncloud 7 on centos 6.
I've created a Dockerfile. I've built an image.
Everything goes right except that when I run the image :
docker run -i -t -d -p 80:80 vfoury/owncloud7:v3
I get the error :
Cannot start container a7efd9be6a225c19089a0f5a5c92f53c4dd1887e8cf26277d3289936e0133c69:
exec: "/etc/init.d/mysqld start && /etc/init.d/httpd start":
stat /etc/init.d/mysqld start && /etc/init.d/httpd start: no such file or directory
If I run the image with /bin/bash
docker run -i -t -p 80:80 vfoury/owncloud7:v3 /bin/bash
then I can run the command
/etc/init.d/mysqld start && /etc/init.d/httpd start
and it works.
Here is my Dockerfile content :
# use the centos6 base image
FROM centos:centos6
MAINTAINER Vincent Foury
RUN yum -y update
# Install SSH server
RUN yum install -y openssh-server
RUN mkdir -p /var/run/sshd
# add epel repository
RUN yum install epel-release -y
# install owncloud 7
RUN yum install owncloud -y
# Expose les ports 22 et 80 pour les rendre accessible depuis l'hote
EXPOSE 22 80
# Modif owncloud conf to allow any client to access
COPY owncloud.conf /etc/httpd/conf.d/owncloud.conf
# start httpd and mysql
CMD ["/etc/init.d/mysqld start && /etc/init.d/httpd start"]
Any help would be greatly appreciated
Vincent F.
After many tests, here is the Dockerfile that works to install ouwncloud (without MySQL):
# use the centos6 base image
FROM centos:centos6
RUN yum -y update
# add epel repository
RUN yum install epel-release -y
# install owncloud 7
RUN yum install owncloud -y
EXPOSE 80
# Modif owncloud conf to allow any client to access
COPY owncloud.conf /etc/httpd/conf.d/owncloud.conf
# start httpd
CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]
then
docker build -t <myname>/owncloud
then
docker run -i -t -p 80:80 -d <myname>/owncloud
then you should be able to open
http://localhost/owncloud
in your browser
I think this is because you're trying to use && within the Dockerfile CMD instruction.
If you intend to run multiple services within a Docker container, you may want to check Supervisor. It enables you to run multiple daemons within the container. Check the Docker documentation at https://docs.docker.com/articles/using_supervisord/.
Alternatively you could ADD a simple bash script to start the two daemons and then set the CMD to use the bash file you added.
The issue is that your CMD argument contains shell operations, but you're using the exec-form of CMD instead of the shell-form. The exec-form passes the arguments to one of the exec functions, which will not interpret the shell operations. The shell-form passes the arguments to sh -c.
Replace
CMD ["/etc/init.d/mysqld start && /etc/init.d/httpd start"]
with
CMD /etc/init.d/mysqld start && /etc/init.d/httpd start
or
CMD ["sh", "-c", "/etc/init.d/mysqld start && /etc/init.d/httpd start"]
See https://docs.docker.com/reference/builder/#cmd.

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