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  
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 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
RUN chmod 755 /
CMD ["/"]  \\Execute script when starting container

4. Write execution scripts

[root@localhost nginx]# vim


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

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           "/"           13 minutes ago      Up 13 minutes>80/tcp,>443/tcp   optimistic_goldberg


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

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
RUN tar zxf apache-tomcat-8.5.49.tar.gz
RUN mv apache-tomcat-8.5.49 /usr/local/tomcat
EXPOSE 8080 
ADD /    \\Add and execute scripts when starting containers
RUN chmod 775 /
CMD ["/"]

3. Write execution scripts

[root@localhost tomcat]# vim
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

[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:
[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 -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