1, dockerfile Preface
Dockfile is a script interpreted by Docker program. Dockerfile consists of one instruction, and each instruction corresponds to one command under Linux. The Docker program translates these dockerfile instructions into real linux commands. Dockerfile has its own writing format and supported commands. The Docker program solves the dependencies between these commands, similar to Makefile. The Docker program will read the dockerfile and generate a customized image according to the instructions. Compared with the black box of image, dockerfile, an obvious script, is easier to be accepted by users. It clearly shows how the image is generated. With dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on dockerfile and regenerate the image, eliminating the trouble of typing commands.
2, Writing rules and instruction usage of Dockerfile
The Dockerfile instruction ignores case. It is recommended to use uppercase and use # as comments. Each line only supports one instruction, and each instruction can carry multiple parameters.
Dockerfile instructions can be divided into two types according to their functions: build instructions and set instructions. The build instruction is used to build an image, and its specified operation will not be executed on the container running the image; The set directive is used to set the properties of an image, and its specified operation will be performed in the container in which the image is running.
Author: garyond
Link: https://www.jianshu.com/p/cbce69c7a52f
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.
3, Build SSH image
3.1 create image directory to facilitate management
mkdir /opt/sshd cd /opt/sshd
3.2 creating and writing dockerfile files
vim Dockerfile #The first line must indicate the underlying image based on FROM centos:7 #Author information MAINTAINER this is ssh image <clj> #Mirror operation instructions RUN yum -y update RUN yum -y install openssh* net-tools lsof telnet passwd #Install some required programs RUN echo 'abc1234' | passwd --stdin root #Change root password RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #PAM certification is not used RUN sed -ri '/^session\s+required\s+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd #Cancel pam restriction RUN ssh-keygen -t rsa -A #Generate key authentication file RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh # EXPOSE 22 #Open port 22 CMD ["/usr/sbin/sshd" , "-D"] #Specifies the command to execute by default
3.3 generate image
docker build -t sshd:centos .
3.4 start the container and change the root password
docker run -d -P sshd:centos docker ps -a ssh localhost -p 49155
4, Building a Systemctl image
4.1 create image directory to facilitate management
mkdir /opt/systemctl cd /opt/systemctl
4.2 creating and writing dockerfile files
vim Dockerfile FROM sshd:centos MAINTAINER this is systemctl image <clj> ENV container docker #Except SYSTEMd tmpfiles setup Service to delete all other files RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \ rm -f /lib/systemd/system/multi-user.target.wants/*; \ rm -f /etc/systemd/system/*.wants/*; \ rm -f /lib/systemd/system/local-fs.target.wants/*; \ rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ rm -f /lib/systemd/system/basic.target.wants/*;\ rm -f /lib/systemd/system/anaconda.target.wants/*; VOLUME [ "/sys/fs/cgroup" ] #CMD ["/usr/sbin/init"]
4.3 generate image
docker build -t systemd:centos . //Start the container, mount the host directory into the container, and initialize docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init & #--privileged: make the root in the container have real root permission. Otherwise, the root in the container is only an external ordinary user permission. docker ps –a
4.4 entering the container
docker exec -it a0d624d2bfa9 bash systemctl status sshd Method 2: docker run -d -P --privileged sshd:centos /usr/sbin/init &
5, Building nginx images
5.1 create image directory to facilitate management
mkdir /opt/nginx cd /opt/nginx/ cp /opt/nginx-1.12.0.tar.gz /opt/nginx
5.2 creating and writing dockerfile files
vim Dockerfile #Base mirror FROM centos:7 #User information MAINTAINER this is nginx image <clj> #Add environment package RUN yum -y update RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN useradd -M -s /sbin/nologin nginx #Upload the nginx software package and unzip it ADD nginx-1.12.0.tar.gz /usr/local/src/ #assign work directory WORKDIR /usr/local/src/nginx-1.12.0 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH #Specify http and https ports EXPOSE 80 EXPOSE 443 RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #Turn off nginx to run in the background #Add run. In the host SH into container ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"]
5.3 script writing
vim run.sh #!/bin/bash /usr/local/nginx/sbin/nginx
5.4 generating images
//Create a new mirror docker build -t nginx:centos . docker run -d -P nginx:centos #Since the dockerfile sets nginx as the foreground startup, there is no need to add a command here docker ps -a 5df9e4383b96 nginx:centos "/run.sh" 15 seconds ago Up 15 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp silly_davinci
5.5 browser access test
http://192.168.80.10:32769
6, Building a tomcat image
6.1 create image directory to facilitate management
mkdir /opt/tomcat cd /opt/tomcat cp /opt/jdk-8u91-linux-x64.tar.gz /opt/tomcat cp /opt/apache-tomcat-8.5.16.tar.gz /opt/tomcat
6.2 creating and writing dockerfile files
vim Dockerfile FROM centos:7 MAINTAINER this is tomcat image <clj> 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 ENV JRE_HOME ${JAVA_HOME}/jre ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib ENV PATH $JAVA_HOME/bin:$PATH ADD apache-tomcat-8.5.16.tar.gz /usr/local/ WORKDIR /usr/local/ RUN mv apache-tomcat-8.5.16 /usr/local/tomcat EXPOSE 8080 #CMD ["/usr/local/tomcat/bin/catalina.sh","run"] ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
6.3 generate image
docker build -t tomcat:centos . docker run -d --name tomcat01 -p 1216:8080 tomcat:centos
6.4 browser access test
http://192.168.80.10:1216
7, Build mysql image
7.1 create image directory to facilitate management
mkdir /opt/mysqld cd /opt/mysqld
7.2 creating and writing dockerfile files
vim Dockerfile FROM centos:7 MAINTAINER this is mysql image <clj> RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make RUN useradd -M -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 \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -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 \ --datadir=/usr/local/mysql/data RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ EXPOSE 3306 ADD run.sh /usr/local/src RUN chmod 755 /usr/local/src/run.sh RUN sh /usr/local/src/run.sh #CMD ["/usr/sbin/init"]
7.3 writing my CNF script
vim my.cnf [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
7.4 write run SH script
vim run.sh #!/bin/bash /usr/local/mysql/bin/mysqld systemctl enable mysqld
7.5 create a new image, start the container, and initialize it
docker build -t mysql:centos . #Start the container and initialize it docker run --name=mysql_server -d -P --privileged mysql:centos /usr/sbin/init &
7.6 access to containers
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f9a4d8f6c65f mysql:centos "/usr/sbin/init" 17 seconds ago Up 16 seconds 0.0.0.0:49153->3306/tcp mysql_server
7.7 enter the container and authorize remote connection to mysql
docker exec -it f9a4d8f6c65f /bin/bash mysql -u root -p grant all privileges on *.* to 'root'@'%' identified by 'abc123'; grant all privileges on *.* to 'root'@'localhost' identified by 'abc123'; flush privileges;
7.8 connecting mysql container on client
mysql -h 192.168.80.10 -u root -P 49153 -pabc123