If you don't know what Dockerfile is, you can check my previous blog:
https://blog.51cto.com/14557905/2489466
Dockerfile image production practice
Three steps for Dockerfile to create image
1, Making sshd image
1. Write Dockerfile file
[root@docker sshd]# vim Dockerfile #base image FROM centos:7 #User information MAINTAINER this is sshd project #Based on basic image system update, software installation RUN yum -y update RUN yum -y install openssh* net-tools lsof telnet passwd #Set user password RUN echo '123456' | passwd --stdin root #Turn off PAM certification RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #Add asymmetric key RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key #Close pam session module RUN sed -i '/^session\s\+requied\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd #Create ssh working directory and set permissions RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh #Set port number EXPOSE 22 #Take action when starting container CMD ["/usr/sbin/sshd","-D"]
2. Create image
[root@docker sshd]# docker build -t sshd:new . [root@docker sshd]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE sshd new 43e794cf8abd 19 seconds ago 585MB centos 7 5e35e350aded 5 months ago 203MB
3. Create container validation
[root@docker sshd]# docker run -d -P sshd:new [root@docker sshd]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99cd8001ff28 sshd:new "/usr/sbin/sshd -D" 4 seconds ago Up 3 seconds 0.0.0.0:32768->22/tcp compassionate_beaver #The host uses ssh to log in the container [root@docker sshd]# ssh localhost -p 32768 The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established. RSA key fingerprint is SHA256:DND5bet+Io1sjjiKpNS6BvoeJC+YD07ejhRcp7s8VtQ. RSA key fingerprint is MD5:a4:0e:3d:5a:18:1e:b2:17:85:f6:df:a5:ce:ab:d2:82. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[localhost]:32768' (RSA) to the list of known hosts. root@localhost's password: [root@2a9bcdd7681f ~]#
2, Create systemctl image
After creating the sshd image, you cannot use the systemctl status sshd command to view the ssh status. You can add the systemctl function according to the sshd image
1. Write Dockerfile file
[root@docker systemctl]# vim Dockerfile #base image FROM sshd:new #Setting environment variables ENV container docker #Enter the specified directory, use for loop to traverse all files under the directory and delete the specified 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/*; #Mount the cgrups volume of the host to execute the container VOLUME ["/sys/fs/cgroup"] #Execute initialization command CMD ["/usr/sbin/init"]
2. Create image
[root@docker systemctl]# docker build -t systemctl:new . [root@docker systemctl]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE systemctl new b029bbbd8a6b 9 minutes ago 585MB sshd new 4b69240205be 33 minutes ago 585MB centos 7 5e35e350aded 5 months ago 203MB
3. Create container validation
[root@docker systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro systemctl:new /sbin/init [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d98f478ccaf3 systemctl:new "/sbin/init" 2 minutes ago Up 2 minutes 22/tcp practical_fermat [root@docker ~]# docker exec -it d98f478ccaf3 bash #Normal use of systemctl command to view [root@d98f478ccaf3 /]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:sshd(8) man:sshd_config(5)
3, Create nginx image (compiled manually)
1. Write dockerfile file
[root@docker nginx]# vim Dockerfile #base image FROM centos:7 #User information MAINTAINER This is Nginx test #Based on basic image system update, environment package installation RUN yum -y update RUN yum -y install make gcc gcc-c++ pcre-devel zlib-devel tar #Creating nginx users RUN useradd -M -s /sbin/nologin nginx #Upload nginx package and decompress it (you can decompress the uploaded package directly by using ADD option) COPY nginx-1.12.2.tar.gz /opt/ RUN tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt #Start compiling and installing nginx in the specified directory WORKDIR /opt/nginx-1.12.2/ RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module RUN make && make install #Setting environment variables ENV PATH /usr/local/nginx/sbin:$PATH #Specify port EXPOSE 80 #Modify the Nginx configuration file to start in a non daemon mode RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #Add running script, set permissions ADD run.sh /run.sh RUN chmod 755 /run.sh #Specifies the script to execute when the container is opened CMD ["/run.sh"]
2. Write run.sh startup script and upload nginx package
[root@docker nginx]# vim run.sh #!/bin/bash /usr/local/nginx/sbin/nginx
3. Create image
[root@docker nginx]# docker build -t nginx:new . [root@docker nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx new 24b44e614799 14 seconds ago 585MB centos 7 5e35e350aded 5 months ago 203MB
4. Create container validation
#Create container and randomly generate mapping port [root@docker nginx]# docker run -d -P nginx:new #View container and mapped port number [root@docker nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b51160232ad8 nginx:new "/run.sh" 4 seconds ago Up 3 seconds 0.0.0.0:32775->80/tcp sad_rhodes
Access authentication in browser
4, Create Tomcat image
1. Write Dockerfile file
[root@docker tomcat]# vim Dockerfile #base image FROM centos:7 #User information MAINTAINER Tomcat test #Update system based on basic image RUN yum -y update #Install java environment package COPY jdk-8u201-linux-x64.rpm /opt/ RUN rpm -ivh /opt/jdk-8u201-linux-x64.rpm #Setting environment variables of java ENV JAVA_HOME /usr/java/jdk1.8.0_201-amd64 ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar ENV PATH $JAVA_HOME/bin:$PATH #Install tomcat ADD apache-tomcat-9.0.16.tar.gz /opt/tomcat RUN mv /opt/tomcat/apache-tomcat-9.0.16/ /usr/local/tomcat #Specify port EXPOSE 8080 #Specifies the operation when the container starts, where the ENTERYPOINT can also be replaced with CMD ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
2. Upload the required installation package to the current directory
[root@docker tomcat]# ls apache-tomcat-9.0.16.tar.gz Dockerfile jdk-8u201-linux-x64.rpm
3. Image production
[root@docker tomcat]# docker build -t tomcat:new . [root@docker tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat new 8b86bbc483a4 3 minutes ago 981MB centos 7 5e35e350aded 5 months ago 203MB
4. Create container validation
[root@docker tomcat]# docker run -d -P tomcat:new [root@docker tomcat]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d94d5eddca0 tomcat:new "/usr/local/tomcat/b..." 3 seconds ago Up 2 seconds 0.0.0.0:32776->8080/tcp optimistic_dubinsk
5. Access authentication on Browser
Create mysql image
1. Write Dockerfile file
[root@docker mysql]# vim Dockerfile #base image FROM centos:7 #User information MAINTAINER This is MySQL Project #Based on basic image system update and environment package installation RUN yum -y update RUN yum install -y ncurses-devel autoconf cmake gcc gcc-c++ make pcre-devel expat-devel pcre #Add mysql-5.6 installation package ADD mysql-5.6.26.tar.gz /opt #Enter the specified directory to compile and install mysql WORKDIR /opt/mysql-5.6.26 RUN cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=all \ -DSYSCONFIDIR=/etc \ -DMYSQL_DATADIR=/home/mysql/ \ -DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock RUN make && make install #Add mysql profile RUN rm -f /etc/my.cnf RUN cp /opt/mysql-5.6.26/support-files/my-default.cnf /etc/my.cnf #Add mysql startup script and set permissions RUN cp /opt/mysql-5.6.26/support-files/mysql.server /etc/init.d/mysqld RUN chmod 755 /etc/init.d/mysqld #Setting environment variables ENV PATH $PATH:/usr/local/mysql/bin #Create mysql user and change the permission of mysql installation directory RUN useradd -s /sbin/nologin mysql RUN chown -R mysql:mysql /usr/local/mysql/ #Initialize database RUN /usr/local/mysql/scripts/mysql_install_db \ --user=mysql \ --ldata=/var/lib/mysql \ --basedir=/usr/local/mysql \ --datadir=/home/mysql #Establish the soft connection of the sock file and change the startup script file RUN ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock RUN sed -i '46 s/basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysqld RUN sed -i '47 s/datadir=/datadir=\/home\/mysql/' /etc/init.d/mysqld #Specify port EXPOSE 3306 #Specifies the action when the container starts CMD ["mysqld_safe"]
2. Upload the installation package to the current directory
[root@docker mysql]# ls Dockerfile mysql-5.7.26.tar.gz
3. Create image
[root@docker mysql]# docker build -t mysqld:new . [root@docker mysql]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysqld new 9b2703bd15c0 54 minutes ago 5.11GB centos 7 5e35e350aded 5 months ago 203MB
4. Create container validation
[root@docker mysql]# docker run -d -P mysqld:new [root@docker mysql]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fff4ac6a524f mysqld:new "mysqld_safe" 4 seconds ago Up 3 seconds 0.0.0.0:32777->3306/tcp infallible_babbage #Enter the container to set the permission account of mysql [root@docker mysql]# docker exec -it fff4ac6a524f /bin/bash [root@fff4ac6a524f ~]# mysql -uroot -p mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456'; mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; #Test and verify the mysql client installed on the host [root@docker mysql]# yum install mariadb -y [root@docker ~]# mysql -h 192.168.7.168 -uroot -p123456 -P 32777 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 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)]>
Summary:
The above are some common cases of using dockerfile to create images. When using dockerfile to create images, the most important thing is the process of writing dockerfile files. We need to know the significance of each step of operation, and be careful not to make mistakes due to the use of some regular expressions.