Docker Containerized Series

Posted by NewPHP_Coder on Sat, 03 Aug 2019 14:47:59 +0200

Docker Containerization

[External Link Picture Transfer Failure (img-PGrNBYx3-1564835831568) https://github.com/grandhappy/docker/blob/master/images/0.png)]

Take nginx+tomcat cluster as an example to illustrate how to build services. The usual way is to download, install, configure and deploy code. How to simplify these operations? This time, we provide docker containerization of these services, so as to reduce the difficulties caused by software migration and reduce the repetitive installation, configuration, deployment and so on.

A journey of a thousand miles begins at the foot

Let's make a docker image of the service

1. Making Mirrors

1.1 Making tomcat01 Mirror

  • Create a directory and download jdk and tomcat

[External Link Picture Transfer Failure (img-3kjVYle 4-1564835831569)] https://github.com/grandhappy/docker/blob/master/images/1.png)]

Code directory: To store our service source code, the source code is index.html.
shell directory: to store our sh script and start the tomcat service.
software directory: software used to store running services, such as jdk and tomcat.
Dockerfile file: The document used to make the docker image is also our core document.

  • Writing Dockerfile content
#Inheritance Mirror
FROM ubuntu:14.04
#author
MAINTAINER docker_user (zule@qq.com)

#===========tomcat=============
#Setting environment variables, all operations are non-interactive
ENV DEBIAN_FRONTEND noninteractive
#Modify the time zone
RUN echo "Asia/Shanghai" > /etc/timezone && \
	dpkg-reconfigure -f noninteractive tzdata
#Install software related to tomcat user authentication
RUN apt-get update
RUN apt-get install -yq --no-install-recommends wget pwgen ca-certificates && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/*

#Configuring tomcat environment variables
ENV JAVA_HOME=/jdk
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#Copy tomcat and jdk to mirror
ADD software/tomcat8-01 /tomcat
ADD software/jdk1.8 /jdk
ADD code/index.html /tomcat/webapps
#ADD create_tomcat_admin_user.sh /create_tomcat_admin_user.sh
ADD shell/run.sh /run.sh
RUN chmod +x /*.sh
RUN chmod +x /tomcat/bin/*.sh
#Open Port
EXPOSE 8080


#Setting Self-Start Command
CMD ["/run.sh"]
  • Make a mirror: Enter the Tomcat 1_ubuntu directory and execute the docker builder command

docker builder –t tomcat01:1.0 .

1.2 Making tomcat02 Mirror

Repeat Chapter 1.1

1.3 Making nginx Mirror

  • Create the appropriate directory and download the nginx installation package

[External Link Picture Transfer Failure (img-HfO YuJCY-1564835831571) https://github.com/grandhappy/docker/blob/master/images/2.png)]

  • Writing Dockerfile content
#Inheritance Mirror
FROM ubuntu:14.04
#author
MAINTAINER docker_user (zule@qq.com)

#===========nginx=============
#Copy nginx to mirror
ADD ./nginx-1.13.0 /opt/nginx-1.13.0
RUN apt-get update
RUN apt-get install build-essential -y \
    && apt-get install libtool -y \
    && apt-get update \
    && apt-get install libpcre3 libpcre3-dev -y \
    && apt-get install zlib1g-dev -y
RUN cd /opt/nginx-1.13.0 \
    && ./configure --prefix=/opt/nginx \
    && make && make install \
    #Modify ports
    && sed -i 's/listen       80;/listen       81;/g' /opt/nginx/conf/nginx.conf \
    #Delete nginx installation package
    && rm -rf /opt/nginx-1.13.0
#Open Port
EXPOSE 81


ADD run.sh /opt/run.sh
RUN chmod 755  /opt/run.sh
#Setting Self-Start Command
CMD ["/opt/run.sh"]
  • Modify nginx configuration

vim nginx/conf/nginx.conf

[External Link Picture Transfer Failure (img-DIPLLQjA-1564835831572)] https://github.com/grandhappy/docker/blob/master/images/3.png)]

  • Make Mirrors: Enter the nginx_ubuntu directory and execute the docker builder command

docker builder –t nginx:1.0 .

Create containers

docker run –d –it --name tomcat01 tomcat01:1.0
docker run –d –it --name tomcat01 tomcat02:1.0
docker run -d -ti --name nginx -p 81:81 --link tomcat01:tomcat01_link --link tomcat02:tomcat02_link nginx:1.0

test

Open the browser for testing

[External Link Picture Transfer Failure (img-IqpLN6UT-1564835831573)] https://github.com/grandhappy/docker/blob/master/images/4.png)]

[External Link Picture Transfer Failure (img-8D9X8BPm-1564835831574) https://github.com/grandhappy/docker/blob/master/images/5.png)]

Doubt

1.Dockerfile CMD instruction not executed?

docker run-t-it -- name tomcat01 tomcat01:1.0/bin/bash, which covers cmd, should be removed/bin/bash

2. Container Interconnection

docker run t id name nginx link tomcat01:tomcat01_link, using link, hosts will be established if established in the container
[External Link Picture Transfer Failure (img-AYIpWpcl-1564835831574) https://github.com/grandhappy/docker/blob/master/images/6.png)]

Topics: Docker Nginx Tomcat github