Build Docker Container Actual Warfare

Bowen structure
Building a Nginx container
Building Tomcat Containers
Building Mysql Container

Understanding Docker containers can be accomplished by Installation and simple operation To understand.

1. Building Nginx containers

Nginx is a lightweight WEB server as well as an excellent reverse proxy server. Nginx service takes up less memory and has a strong concurrency ability, which is welcomed by users at home and abroad.The following is how to create a Docker image with a Nginx service using a Dockerfile file.

1. Download Basic Mirror
Download a base mirror centos image to create the Nginx image:

[root@localhost media]# systemctl start docker
[root@localhost media]# docker load < centos

2. Establish working directory
Create working directory

[root@localhost /]# mkdir nginx
[root@localhost /]# cd nginx

3. Create and write Dockerfile files

[root@localhost nginx]# vim Dockerfile

FROM centos  \\Set Base Mirror
MAINTAINER the contos projec  \\Maintain this mirror user information
RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel
make openssl-devel   \\Install dependent packages
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz  
RUN tar zxf nginx-1.9.7.tar.gz   \\Download and unzip the source package
WORKDIR nginx-1.9.7  
RUN ./configure --prefix=/usr/local/nginx && make && make install  \\Compile Installation Nginx
EXPOSE 80
EXPOSE 443   \\Open ports 80 and 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
\\Copy service startup script and set permissions
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]  \\Execute script when starting container

4. Write execution scripts

[root@localhost nginx]# vim run.sh

#!/bin/bash
/usr/local/nginx/sbin/nginx

5. Generate a mirror

[root@localhost nginx]# docker build -t nginx:xws. \ There's a little behind it. Don't forget

6. Start the container for testing

[root@localhost nginx]# Docker run-d-P nginx:xws \-P is a mapped random port
4794d0f564c93f09827d33f70be21f3893733648a56e4d3985f82611efb2aa0b

You can see the port mapped to 32769

[root@localhost nginx]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                           NAMES
4794d0f564c9        nginx:xws           "/run.sh"           13 minutes ago      Up 13 minutes       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   optimistic_goldberg

Verification

2. Building Tomcat containers

Tomcat is a free, open source, lightweight Web server that is widely used in small and medium-sized enterprises and in situations where concurrent access is low. It is the preferred choice for developing and debugging JSP programs.The following is how to create a Docker image with a Tomcat service using a Dockerfile file.

1. Create a working directory

[root@localhost /]# mkdir tomcat
[root@localhost /]# cd tomcat/
[root@localhost tomcat]# cp /media/jdk-8u91-linux-x64.tar.gz .
[root@localhost tomcat]# ls
jdk-8u91-linux-x64.tar.gz

2. Create a Dockerfile

[root@localhost tomcat]# vim Dockerfile
FROM centos    \\base image
MAINTAINER the centos project \\Maintain this mirror user information
ADD jdk1.8.0_91 /usr/local/jdk-8u91  \\Set up jdk environment variable
ENV JAVA_HOME /usr/local/jdk-8u91
ENV JAVA_BIN /usr/local/jdk-8u91/bin
ENV JAVA_HOME /usr/local/jdk-8u91/jre
ENV PATH $PATH:/usr/local/jdk-8u91/bin:/usr/local/jdk-8u91/jre/bin
ENV CLASSPATH /usr/local/jdk-8u91/jre/bin:/usr/local/jdk-8u91/lib:/usr/local/jdk-8u91/jre/lib/charsets.jar
RUN yum install -y wget     \\install wget tool
RUN wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz
RUN tar zxf apache-tomcat-8.5.49.tar.gz
RUN mv apache-tomcat-8.5.49 /usr/local/tomcat
EXPOSE 8080 
ADD run.sh /run.sh    \\Add and execute scripts when starting containers
RUN chmod 775 /run.sh
CMD ["/run.sh"]

3. Write execution scripts

[root@localhost tomcat]# vim run.sh
#!/bin/bash
/usr/local/tomcat/bin/startup.sh
tailf /run

4. Generate mirrors with Dockerfile

[root@localhost tomcat]# docker build -t tomcat:aaa .
\There must be no fewer points behind

5. Run the container and verify

[root@localhost tomcat]# docker run -d --name asd -p 80:8080 tomcat:aaa

3. Building mysql container

MySQL is currently the most popular relational database. The SQL language used is the most common standardized language for accessing databases. With the advantages of small size, fast speed and low cost, MySQL has become the preferred database for small and medium-sized enterprises.The following is how to create a Docker image with MySQL services using a Dockerfile file.

1. Download the base image and create a working directory
Download mirror command using: docker pull docker.io

[root@localhost /]# cd /media/
[root@localhost media]# ls
centos  centos6  jdk-8u91-linux-x64.tar.gz
[root@localhost media]# docker load < centos6
5d574ede99e4: Loading layer   202 MB/202 MB
b077477046f8: Loading layer 35.54 MB/35.54 MB
b244861945e8: Loading layer 2.048 kB/2.048 kB
Loaded image: docker.io/guyton/centos6:latest
[root@localhost media]# mkdir /mysql
[root@localhost media]# cd /mysql/
[root@localhost mysql]# 

2. Create a Dockerfile

[root@localhost mysql]# vi Dockerfile

FROM guyton/centos6   \\Set Base Mirror
MAINTAINER the centos6   Maintain mirrored user information
RUN yum install -y mysql mysql-server
RUN /etc/init.d/mysqld start && \   \\open MySQL Service Authorization
mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" && \
mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';"
EXPOSE 3306   \\Open port 3306
CMD ["mysqld_safe"]     \\Run Initialization Script mysql_safe

3. Generate mirrors with Dockerfile

[root@localhost mysql]# docker build -t mysql:bbb .
\There must be no fewer points behind

4. Run the container and verify

[root@localhost mysql]# docker run -d --name mysql -P mysql:bbb

5. Validation

[root@localhost mysql]# mysql -h 192.168.6.132 -u root -P 32271 -p123456

Use the MysQL command here to connect to container databases on your local system
yum install mysql installs the mariaDB client.

Keywords: Linux Nginx MySQL Tomcat Docker

Added by Delaran on Fri, 29 Nov 2019 21:21:14 +0200