Dockerfile theory + practice!!!

Dockerfile overview

  Dockerfile is the description file of the image file in docker. The straight white point is exactly what the image file is composed of step by step.
  for example: you bought a hanger on Taobao, but the seller did not send you a complete hanger, but some components and a drawing. You assemble the hanger step by step according to the drawing, and it will become the appearance you need. Then the dockerfile is the drawing, and the image file is the hanger you need. Dockerfile is not recommended to be named casually, just dockerfile.
  therefore, the Dockerfile contains instructions, each of which builds a layer. Therefore, the content of each instruction is to describe how the layer should be built.

The general process of Dockerfile execution by Docker:

(1) docker runs a container from the basic image;
(2) Execute an instruction and make changes to the container;
(3) Perform the operation similar to docker commit to submit a new image layer;
(4) docker then runs a new container based on the newly submitted image;
(5) Execute the next instruction in the dockerfile until all instructions are executed.

Different stages:

1. Dockerfile: it is the raw material of the software. You need to define a dockerfile, which defines everything the process needs. The contents of dockerfile include execution code or file, environment variable, dependency package, runtime environment, dynamic link library, etc;

2. Docker image: it is the delivery of software. After defining a file with Dockerfile, a docker image will be generated when docker is built. When docker image is running, services will be provided;

3. Docker container: it can be considered as the running state of software, and the container provides services directly.

Details of parameters of Dockerfile:

instructions Meaning
FROM mirror image Specifies the image on which the new image is based. The first instruction must be the FROM instruction, which is required for each image created
MAINTAINER name Explain the maintainer information of the new image
RUN command Executes commands on the mirror on which they are based and commits them to a new mirror
CMD ["program to run", "Parameter1", "parameter2"] The command or script to run when the command starts the container. The Dockerfile can only have one CMD command. If multiple commands are specified, only the last one can be executed
Exit port number Specify the port to open when the new image is loaded into Docker
ENV environment variable value Setting the value of an environment variable will be used by the following RUN
ADD source file / directory target file / directory Copy the source file to the target file. The source file should be in the same directory as the Dockerfile, or a URL
COPY source file / directory target file / directory Copy the file / directory on the local host to the destination, and the source file / directory should be in the same directory as the Dockerfile
VOLUME [directory] Create a mount point in the container
USER user name / UID Specify the user when the container runs
WORKDIR path Specify working directory for subsequent RUN, CMD, ENTRYPOINT
ONBUILD command Specifies the command to run when the generated image is used as a base image
HEALTHCHECK Health examination

Dockerfile practice

1. Building sshd image

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir sshd  ##Create directory
[root@localhost opt]# cd sshd/
[root@localhost sshd]# vim Dockerfile  ##Write a dockerfile file

FROM centos    ##Download mirroring
MAINTAINER this is sshd <xu>   ##Descriptive information
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123456' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\^/#/' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22  ##port
CMD ["/usr/sbin/sshd" , "-D"]
[root@localhost sshd]# docker build -t sshd:new .   ##create mirror
[root@localhost sshd]# docker run -d -P sshd:new  ##Create maps and containers
[root@localhost sshd]# docker ps -a  ##View container status
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
89432272695a        sshd:new            "/usr/sbin/sshd -D"   7 seconds ago       Up 6 seconds>22/tcp   sad_fermi
[root@localhost sshd]# ssh localhost -p 32768  ##Log in locally with ssh

2. Build system CTL image

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir systemctl   ##Create directory
[root@localhost opt]# cd systemctl/
[root@localhost systemctl]# vim Dockerfile   ##Write a dockerfile file

FROM sshd:new
ENV container docker   ##Environmental Science
RUN (cd /lib/systemd/system/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/*; \
rm -f /lib/systemd/system/*udev*; \
rm -f /lib/systemd/system/*initctl*; \
rm -f /lib/systemd/system/*; \
rm -f /lib/systemd/system/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
[root@localhost systemctl]# docker build -t systemd:lasted .   ##create mirror
[root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro systemd:lasted /sbin/init
##The root in the private container has the real root permission. Otherwise, the root in the container is only a common external user permission.
[root@localhost ~]# docker exec -it 23a50d568c75 bash  ##Container entry
[root@23a50d568c75 /]# systemctl status sshd   ##View state

3. Build Nginx image

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir nginx   ##Create Nginx directory
[root@localhost opt]# cd nginx/
[root@localhost nginx]# vim Dockerfile

FROM centos:7
MAINTAINER The is nginx <xu>
RUN yum install -y proc-devel gcc gcc-c++ zlib zlib-devel make openssl-devel wget
ADD nginx-1.12.2.tar.gz /usr/local
WORKDIR /usr/local/nginx-1.12.2/
RUN ./configure --prefix=/usr/local/nginx && make && make install
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
WORKDIR /root/nginx
RUN chmod 755 /
CMD ["/"]
[root@localhost nginx]# vim

/usr/local/nginx/sbin/nginx   ##Start Nginx service
[root@localhost nginx]# mount.cifs // /mnt / ාා񖓿ාmount image
Password for root@//  
[root@localhost nginx]# cp /mnt/nginx-1.12.2.tar.gz ./   ##Copy to current directory
[root@localhost nginx]# docker build -t nginx:new .   ##create mirror
[root@localhost nginx]# docker run -d -P nginx:new    ##Create container
[root@localhost nginx]# docker ps -a   ##View container
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                                           NAMES
228c1f5b8070        nginx:new           "/"                9 seconds ago       Up 8 seconds        >80/tcp,>443/tcp   busy_booth

Accessing web pages with browser

4. Building Tomcat image

[root@localhost opt]# mkdir tomcat
[root@localhost opt]# cd tomcat
[root@localhost tomcat]# cp /mnt/tomcat/jdk-8u91-linux-x64.tar.gz ./ ##Copy to current directory
[root@localhost tomcat]# cp /mnt/Tomcat1/tomcat/apache-tomcat-9.0.16.tar.gz ./
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat
ADD jdk-8u91-linux-x64.tar.gz /usr/local
WORKDIR /usr/local
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java     ##Setting environment variables
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat8
ENTRYPOINT ["/usr/local/tomcat8/bin/","run"]
[root@localhost tomcat]# docker build -t tomcat:centos .  ##create mirror
[root@localhost tomcat]# docker run --name tomcat01 -p 1234:8080 -it  tomcat:centos /bin/bash
##Create container

Access with browser

5. Building MySQL image

[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql
[root@localhost mysql]# cp /mnt/mysql-boost-5.7.20.tar.gz ./  ##Copy the compressed package to the current directory
[root@localhost mysql]# vim my.cnf  ##Create profile template in current directory

port = 3306
socket = /usr/local/mysql/mysql.sock

port = 3306
socket = /usr/local/mysql/mysql.sock

user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/
socket = /usr/local/mysql/mysql.sock
server-id = 1

[root@localhost mysql]# vim Dockerfile   ##Write a dockerfile file
FROM centos:7
RUN yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
make \
gcc \
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" > /
RUN chmod 755 /
RUN sh /
CMD ["init"]
[root@localhost mysql]# docker build -t centos:mysql .   ##create mirror
[root@localhost mysql]# docker run --name=mysql_server -d -P --privileged centos:mysql 
##Create container
[root@localhost mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                     NAMES
998dc9797102        centos:mysql        "init"              About a minute ago   Up About a minute>3306/tcp   mysql_server
[root@localhost mysql]# docker exec -it 998dc9797102 /bin/bash

[root@998dc9797102 mysql]# mysql
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
[root@localhost ~]# mysql -h -u root -P 32768 -pabc123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

Tags: Linux MySQL Docker Nginx Tomcat

Posted on Wed, 15 Jan 2020 03:25:46 -0500 by venom999