Introduction to Docker and Dockerfile of practical Application
1.Dockerfile format
2. Dockerfile instruction
3. Build image
4. Build a basic image of Nginx,PHP,Tomcat
Prerequisite: upload files dockerfile .zip and wordpress-4.9.4-zh_CN.tar.gz
[root@localhost first_stage] # unzip dockerfile\ .zip-d Dockerfile/Archive: dockerfile .zip inflating: Dockerfile/Dockerfile-nginx inflating: Dockerfile/Dockerfile-php inflating: Dockerfile/Dockerfile-tomcat inflating: Dockerfile/nginx.conf inflating: Dockerfile/php-fpm.conf inflating: Dockerfile/php.ini
Build the basic image of Nginx:
[root@localhost first_stage] # sed-I's rf 1.12.2 cat Dockerfile-nginxFROM centos:7MAINTAINER www.dengaosky.comRUN yum install [root@localhost first_stage] # cat Dockerfile-nginxFROM centos:7MAINTAINER www.dengaosky.comRUN yum install-y gcc gcc-c++ make\ openssl-devel pcre-devel gd-devel\ iproute net-tools telnet wget curl & &\ yum clean all & &\ rm-rf / var/cache/yum/*RUN wget http://nginx.org/ Download/nginx-1.15.5.tar.gz & &\ tar zxf nginx-1.15.5.tar.gz & &\ cd nginx-1.15.5 & &\. / configure-- prefix=/usr/local/nginx\-- with-http_ssl_module\-- with-http_stub_status_module & &\ make-j 4 & & make install & &\ rm-rf / usr/local/nginx/ Html/* & &\ echo "ok" > > / usr/local/nginx/html/status.html & &\ cd / & & rm-rf nginx-1.15.5* & &\ ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtimeENV PATH $PATH:/usr/local/nginx/sbinCOPY nginx.conf / usr/local/nginx/conf/nginx.confWORKDIR / usr/local/nginxEXPOSE 80CMD ["nginx" "- g", "daemon off "] [root@localhost first_stage] # docker build-t nginx:v1-f Dockerfile-nginx. [root@localhost first_stage] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx v1 d25e10f6dc2f 3 minutes ago 369MBcentos 7 9f38484d220f 4 months ago 202MB [root@localhost first_stage] #
Build the basic image of PHP:
[root@localhost first_stage] # cat Dockerfile-php FROM centos:7MAINTAINER www.dengaosky.comRUN yum install epel-release-y & &\ yum install-y gcc gcc-c++ make gd-devel libxml2-devel\ libcurl-devel libjpeg-devel libpng-devel openssl-devel\ libmcrypt-devel libxslt-devel libtidy-devel autoconf\ iproute net-tools telnet wget curl & &\ yum clean all & &\ rm-rf / var/cache/yum/*RUN wget http:// Docs.php.net/distributions/php-5.6.36.tar.gz &\ tar zxf php-5.6.36.tar.gz & &\ cd php-5.6.36 & &\. / configure-- prefix=/usr/local/php\-- with-config-file-path=/usr/local/php/etc\-- enable-fpm-- enable-opcache\-- with-mysql-- with-mysqli-- with- Pdo-mysql\-- with-openssl-- with-zlib-- with-curl-- with-gd\-- with-jpeg-dir-- with-png-dir-- with-freetype-dir\-- enable-mbstring-- with-mcrypt-- enable-hash & &\ make-j 4 & & make install & &\ cp php.ini-production / usr/local/php/etc/php.ini &\ cp sapi/fpm/php-fpm .conf / usr/local/php/etc/php-fpm.conf & &\ sed-I "90a\ daemonize = no" / usr/local/php/etc/php-fpm.conf & &\ mkdir / usr/local/php/log & &\ cd / & & rm-rf php* & &\ ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtimeENV PATH $PATH:/usr/local/php/sbinCOPY php.ini / usr/local / php/etc/COPY php-fpm.conf / usr/local/php/etc/WORKDIR / usr/local/phpEXPOSE 9000CMD ["php-fpm"] [root@localhost first_stage] # docker build-t php:v1-f Dockerfile-php. [root@localhost first_stage] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEphp v1 402fe17abd8f 4 seconds ago 522MBnginx v1 d25e10f6dc2f 15 minutes ago 369MBcentos 7 9f38484d220f 4 months ago 202MB [root@localhost first_stage] #
Build the basic image of Tomcat:
[root@localhost first_stage] # sed-I "rm 8.0.52 http://mirrors.shu.edu.cn/ 8.0.46max" Dockerfile-tomcat [root@localhost first_stage] # cat Dockerfile-tomcat FROM centos:7MAINTAINER www.dengaosky.comENV VERSION=8.0.46RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools-y & &\ rm-rf / var/cache/yum/*#RUN wget http://mirrors.shu.edu.cn/ Apache/tomcat/tomcat-8/v$ {VERSION} / bin/apache-tomcat-$ {VERSION} .tar.gz & &\ RUN wget http://archive.apache.org/dist/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz & &\ tar zxf apache-tomcat-$ {VERSION} .tar.gz & &\ mv apache-tomcat-$ {VERSION} / usr/local/tomcat & &\ rm- Rf apache-tomcat-$ {VERSION} .tar.gz / usr/local/tomcat/webapps/* & &\ mkdir / usr/local/tomcat/webapps/test & &\ echo "ok" > / usr/local/tomcat/webapps/test/status.html & &\ sed-I'1a JAVA_OPTS= "- Djava.security.egd=file:/dev/./urandom" / usr/local/tomcat/bin/catalina.sh & &\ ln-sf / usr / share/zoneinfo/Asia/Shanghai / etc/localtimeENV PATH $PATH:/usr/local/tomcat/binWORKDIR / usr/local/tomcatEXPOSE 8080CMD ["catalina.sh" "run"] [root@localhost first_stage] # docker build-t tomcat:v1-f Dockerfile-tomcat. [root@localhost first_stage] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtomcat v1 118b74bae95f 52 seconds ago 427MBphp v1 402fe17abd8f 55 minutes ago 522MBnginx V1 d25e10f6dc2f About an hour ago 369MBcentos 7 9f38484d220f 4 months ago 202MB [root@localhost first_stage] #
5. Quickly build LNMP website platform
Quickly deploy LNMP website platform 1, customize network docker network create lnmp2, create Mysql container docker run-d\-name lnmp_mysql\-- net lnmp\-- mount src=mysql-vol,dst=/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=123456-e MYSQL_DATABASE=wordpress mysql:5.7-- character-set-server=utf8 3, create PHP container docker run-d-name lnmp_php-- net lnmp-- mount src=wwwroot
Actual combat exercise:
Create a custom network [root@localhost ~] # docker network create lnmpe206a2a632cf78430fc6c32d6f50a810c0a8f3ad8ad32287b354cad32de04aac [root@localhost ~] # [root@localhost ~] # docker network lsNETWORK ID NAME DRIVER SCOPE67d8c68783da bridge bridge local2b6c8c029f58 host host locale206a2a632cf lnmp bridge localfc002cd520b3 none Null local [root@localhost ~] # View the created container [root@localhost ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~] # View the created volume: [root@localhost ~] # docker volume lsDRIVER VOLUME NAME [root@localhost ~] Create Mysql container docker run-d\-- name lnmp_mysql\-- net lnmp\-- mount src=mysql-vol Dst=/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=123456-e MYSQL_DATABASE=wordpress mysql:5.7-- character-set-server=utf8 create PHP container docker run-d-- name lnmp_php-- net lnmp-- mount src=wwwroot,dst=/wwwroot php:v1 create Nginx container docker run-d-- name lnmp_nginx-- net lnmp-p 88:80\-- mount type=bind,src=$ (pwd) / nginx.conf,dst=/usr/local/nginx/conf/nginx.conf-- mount src=wwwroot Dst=/wwwroot nginx:v1 # ensures that when the nginx.conf file is mounted in the current directory, the local volume mysql-vol, Wwwroot [root@localhost first_stage] # docker volume lsDRIVER VOLUME NAMElocal mysql-vollocal wwwroot [root@localhost first_stage] # docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe09f16c2e561 nginx:v1 "nginx-g 'daemon of..." 55 seconds ago Up 54 seconds 0.0.0.0 php-fpm 88-> 80/tcp lnmp_nginxcc9ca12735fd php:v1 "php-fpm" 4 minutes ago Up 4 minutes 9000/tcp lnmp_php22217c6c82ce mysql:5.7 "docker-entrypoint.s …" 5 minutes ago Up 5 minutes 3306/tcp, 33060/tcp lnmp_ MySQL [root @ localhost first_stage] # create a static test page: [root@localhost first_stage] # ls / var/lib/docker/volumes/wwwroot/_data/ [root @ localhost first_stage] # [root@localhost first_stage] # echo "Hello World. "> / var/lib/docker/volumes/wwwroot/_data/index.html browser access: http://192.168.16.121:88/ display content is: Hello,World. Create a dynamic test page: [root@localhost first_stage] # echo "> / var/lib/docker/volumes/wwwroot/_data/test.php browser access: http://192.168.16.121:88/test.php display: Hello,World. Deploy wordpress blog: [root@localhost first_stage] # wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz[root@localhost first_stage] # rm-rf / var/lib/docker/volumes/wwwroot/_data/* [root @ localhost first_stage] # tar xf wordpress-4.9.4-zh_CN.tar.gz [root@localhost first_stage] # mv wordpress/* / var/lib/docker/volumes/wwwroot/_ Data/ browser access: http://192.168.16.121:88/index.php display content is: WordPress deployment wizard. Database name: wordpress user name: root password: 123456 Database Host: lnmp_ MySQL table prefix: wp_ submit manually created wp-config.php file / var/lib/docker/volumes/wwwroot/_data/wp-config.php now installation site title: Jacky user name: admin password: 123456 confirm password: check "confirm to use weak password" your email: admin@123.com installation WordPress
The WordPress installation wizard is as follows:
So far, the WordPress blog has been successfully built.