Docker on RHEL 7.4 (Apache HTTPD and Tomcat)
Environment
- Red Hat Enterprise Linux 7.4 VM on Oracle VM Server
- Docker 1.12.6 (from Red Hat yum repository)
- Oracle JDK SE 9 (from Oracle)
- Apache HTTPD 2.4.6 (from Red Hat yum repository)
- Tomcat 8.5.23 (from Apache)
Install Docker on Red Hat Enterprise Linux 7
- Join Red Hat Enterprise Linux developer subscription
- Then, refer to the GETTING STARTED WITH CONTAINERS to install Docker and corresponding configurations.
Dockerfile -Apache HTTPD
- mkdir -p /root/httpd-project
- cd /root/httpd-project
- vi DockerfileDockerfile for Apache HTTPDDockerfile
# My cool Docker image # Version 1 # If you loaded redhat-rhel-server-7.0-x86_64 to your local registry, uncomment this FROM line instead: # FROM registry.access.redhat.com/rhel # Pull the rhel image from the local registry FROM registry.access.redhat.com/rhel MAINTAINER Calvin Yiu USER root # Update image RUN yum repolist --disablerepo=* && \ yum-config-manager --disable \* > /dev/null && \ yum-config-manager --enable rhel-7-server-rpms > /dev/null RUN yum update -y # Add httpd package. procps and iproute are only added to investigate the image later. RUN yum install httpd procps iproute -y RUN echo hostname.abc.com > /etc/hostname #RUN mkdir /run/httpd COPY httpd-proxy.conf /etc/httpd/conf.modules.d/ # Create an index.html file #RUN bash -c 'echo "Your Web server test is successful." >> /var/www/html/index.html' EXPOSE 80 # Start the service CMD /usr/sbin/httpd -D FOREGROUND
- vi httpd-proxy.confhttpd-proxy.conf
<Proxy "balancer://cluster"> BalancerMember "ajp:/tomcat:8009" loadfactor=1 BalancerMember "ajp://tomcat2:8009" loadfactor=2 ProxySet lbmethod=bytraffic </Proxy> ProxyPassMatch ^/(.*\.jsp)$ balancer://cluster/$1
Dockerfile - Tomcat
- mkdir -p /root/tomcat-project
- Download binary files from Apache
- Download Java 8 rpm file from Oracle
- Copy above files into /root/tomcat-project
- vi DockerfileDockerfile for TomcatDockerfile
# My cool Docker image # Version 1 # If you loaded redhat-rhel-server-7.0-x86_64 to your local registry, uncomment this FROM line instead: # FROM registry.access.redhat.com/rhel # Pull the rhel image from the local registry #FROM registry.access.redhat.com/rhel FROM rhel MAINTAINER Calvin Yiu # The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile. USER root ENV CATALINA_HOME /usr/local/tomcat ENV PATH $CATALINA_HOME/bin:$PATH RUN mkdir -p "$CATALINA_HOME" # Update image COPY jdk-9.0.1_linux-x64_bin.rpm . COPY apache-tomcat-8.5.23.tar.gz . RUN rpm -ivh jdk-9.0.1_linux-x64_bin.rpm RUN tar xvf apache-tomcat-8.5.23.tar.gz -C ${CATALINA_HOME} --strip-components=1 EXPOSE 8009 WORKDIR ${CATALINA_HOME} CMD ["catalina.sh", "run"]
Build Apache HTTPD and Tomcat container images
- cd /root/httpd-project
- docker build -t rhel_httpd .
- cd /root/tomcat-project
- docker build -t rhel_tomcat .
- docker images
[root@workflow tomcat-project]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE rhel_httpd latest 101b0144da85 5 hours ago 523 MB rhel_tomcat latest 8d73b26c2609 29 hours ago 1.135 GB registry.access.redhat.com/rhel7/rhel latest db7a70a0414e 5 weeks ago 195.9 MB registry.access.redhat.com/rhel latest db7a70a0414e 5 weeks ago 195.9 MB
Create local network for the containers
It is recommended to use user-defined bridge networks to control which containers can communicate with each other, and also to enable automatic DNS resolution of container names to IP addresses. User-defined networks
docker network create --driver bridge my_bridge_network
- docker network list
NETWORK ID NAME DRIVER SCOPE f1f1b26c48dd bridge bridge local 4f7f8e164833 host host local 8aad24fc4399 my_bridge_network bridge local f8361e507611 none null local
Start the Tomcat and HTTP containers
bash docker run -i -d -t -v /data/uat/webapps:/usr/local/tomcat/webapps --network=my_bridge_network -p 8080:8080 --name=tomcat --add-host=m-docker.abc.com:10.1.10.182 \--add-host=webdb12cuat:10.1.96.142 rhel_tomcat
Definition of the options: --tty , -t Allocate a pseudo-TTY --detach , -d Run container in background and print container ID --interactive , -i Keep STDIN open even if not attached --volume , -v Bind mount a volume --add-host Add a custom host-to-IP mapping (host:ip) (add to /etc/hosts) --network Connect a container to a network --publish , -p Publish a container’s port(s) to the host --name Assign a name to the container
docker run -i -d -t -v /data/uat/webapps:/usr/local/tomcat/webapps --network=my_bridge_network -p 8081:8080 --name=tomcat2 --add-host=m-docker.abc.com:10.1.10.182 --add-host=webdb12cuat:10.1.96.142 rhel_tomcat
- Check the running containers now:
[root@workflow tomcat-project]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a7fc047032c rhel_tomcat "catalina.sh run" 5 hours ago Up 5 hours 8009/tcp, 0.0.0.0:8081->8080/tcp tomcat2 188eefc95caa rhel_httpd "/bin/sh -c '/usr/sbi" 5 hours ago Up 5 hours 0.0.0.0:80->80/tcp httpd ef8553297fcb rhel_tomcat "catalina.sh run" 6 hours ago Up 6 hours 8009/tcp, 0.0.0.0:8080->8080/tcp tomcat