dockerfile构建镜像&&搭建wordpress

一,Dockerfile介绍

Dockerfile是由一行行志玲语句构成的一个,创建docker镜像的配置文件 三个部分组成: 基础(必须要有),镜像,运行指令,容器默认执行命令。 PS: FROM是必须要的 所有的指令必须大写 

二,dockerfile中常用的指令

1,FROM:指定基础镜像,必须为第一个命令

格式: FROM <image> FROM <image>:<tag> FROM <image>@<digest> 示例: FROM mysql:5.6; (提供基础镜像) 注: tag 或 digest 是可选的,如果不使用这两个值时,会使用 latest 版本的基础镜像 

2, MAINTAINER: 维护者信息

格式: MAINTAINER <name> 示例: MAINTAINER Jasper Xu MAINTAINER sorex@163.com MAINTAINER Jasper Xu <sorex@163.com> [root@\ docker~]# vim Dockerfile FROM nginx MAINTAINER zxx [root@\ docker~]# docker build -t test:v1 . Sending build context to Docker daemon 658.4MB Step 1/2 : FROM nginx ---> f6d0b4767a6c Step 2/2 : MAINTAINER zxx ---> Running in 5588d82876b8 Removing intermediate container 5588d82876b8 ---> cc421ec1dc93 Successfully built cc421ec1dc93 Successfully tagged test:v1 [root@\ docker~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test v1 cc421ec1dc93 25 seconds ago 133MB [root@\ docker~]# docker inspect test:v1 | grep Author "Author": "zxx", 

3, RUN:构建镜像时执行的命令

基于FROM指定的docker镜像,运行一个指令;将结果反应给新的镜像 执行的指令必须是镜像中已经存在的命令(比如nginx里不可以执行ip a) [root@\ docker~]# vim Dockerfile FROM nginx MAINTAINER zxx RUN echo 'hello' > /usr/share/nginx/html/index.html [root@\ docker~]# docker build -t test:v2 . [root@\ docker~]# docker run -it test:v2 bash root@fe1d23720d97:/# cd /usr/share/nginx/html/ root@fe1d23720d97:/usr/share/nginx/html# cat index.html hello 

4, CMD:构建容器后调用,也就是在容器启动时才进行调用

[root@\ docker~]# vim Dockerfile FROM nginx MAINTAINER zxx RUN echo 'hello' > /usr/share/nginx/html/index.html CMD /bin/bash [root@\ docker~]# docker build -t test:v3 . #运行镜像会直接进入到bash中 [root@\ docker~]# docker run -it test:v3 root@43a5544061af:/# 

5, ADD:将本地文件添加到容器中(可以解压,支持tar包解压;支持远程下载)

[root@\ docker~]# vim Dockerfile FROM centos:7 ADD php.tar.gz /root # 可以使用ADD 下载链接 /root [root@\ docker~]# docker build -t test:v5 . [root@\ docker~]# docker run -it test:v5 bash [root@021d11772a60 /]# cd /root [root@021d11772a60 ~]# ls anaconda-ks.cfg php71w-cli-7.1.31-1.w7.x86_64.rpm autoconf-2.69-11.el7.noarch.rpm php71w-common-7.1.31-1.w7.x86_64.rpm automake-1.13.4-3.el7.noarch.rpm php71w-devel-7.1.31-1.w7.x86_64.rpm libX11-1.6.5-2.el7.x86_64.rpm php71w-embedded-7.1.31-1.w7.x86_64.rpm ... ... 

6, COPY:功能类似 ADD,但是是不会自动解压文件,也不能访问网络资源

[root@\ docker~]# vim Dockerfile FROM centos:7 COPY php.tar.gz /root [root@\ docker~]# docker build -t test:v4 . [root@\ docker~]# docker run -it test:v4 bash [root@fd0af3e93312 /]# cd /root [root@fd0af3e93312 ~]# ls anaconda-ks.cfg php.tar.gz 

7, ENV:设置容器的环境变量

8, EXPOSE:指定于外界交互的端口

只有制定了expose,才可以使用-P;可以指定多个端口 [root@\ docker~]# vim Dockerfile FROM nginx EXPOSE 8080 CMD nginx -g "daemon off;" [root@\ docker~]# docker build -t test:v7 . [root@\ docker~]# docker run -d test:v7 [root@\ docker~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0c713e9b2158 test:v7 "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp, 8080/tcp silly_elgamal 

9, ARG ,用于指定传递给构建运行时的变量

用于构建镜像时,传入的参数 [root@\ docker~]# vim Dockerfile FROM centos:7 ARG APP=xxx RUN yum install -y $APP CMD nginx -g "daemon off;" [root@\ docker~]# docker build --build-arg=APP=nginx -t test:v8 . Sending build context to Docker daemon 678.3MB Step 1/4 : FROM centos:7 ---> 8652b9f0cb4c Step 2/4 : ARG APP=xxx ---> Running in b4da42ad3020 Removing intermediate container b4da42ad3020 ---> 93b0f3cc8330 Step 3/4 : RUN yum install -y $APP ---> Running in b56d19cbbeca Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.ustc.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.ustc.edu.cn No package nginx available. Error: Nothing to do # -build-arg=APP=nginx 可改变指定设置的变量,如果不指定nginx,那么下载的就是xxx 

10, VOLUME:用于指定持久化目录

-- 设置需要挂载的目录;没有实现挂载 FROM centos:7 VOLUME ["/opt","/var/log"] CMD nginx -g "daemon off;" [root@\ docker~]# docker build -t test:v9 . [root@\ docker~]# docker inspect test:v9 ... "Volumes": { "/opt": {}, "/var/log": {} }, # 提醒作用,使用该镜像就知道需要挂载哪个目录 

11, WORKDIR:工作目录,类似于 cd 命令

[root@\ docker~]# vim Dockerfile FROM centos:7 WORKDIR /opt CMD nginx -g "daemon off;" [root@\ docker~]# docker build -t test:v10 . # 运行时的开始目录就是/opt [root@\ docker~]# docker run -it test:v10 bash [root@3b5c6dd984e9 opt]# # 设置后,RUN CMD ADD COPY等命令都会在该目录下执行 

12, ONBUILD:用于设置镜像触发器

触发条件:当前镜像用作基础镜像时触发 [root@\ docker~]# vim Dockerfile FROM centos:7 WORKDIR /opt ONBUILD RUN echo "偷镜像吗" CMD nginx -g "daemon off;" # 生成镜像 [root@\ docker~]# docker build -t test:v11 . Sending build context to Docker daemon 678.3MB Step 1/4 : FROM centos:7 ---> 8652b9f0cb4c Step 2/4 : WORKDIR /opt ---> Using cache ---> 1cbdd9c77083 Step 3/4 : ONBUILD RUN echo "偷镜像吗" ---> Running in cef2b047e2e9 Removing intermediate container cef2b047e2e9 ---> f89871fbdf03 Step 4/4 : CMD nginx -g "daemon off;" ---> Running in 75a8053f9dc5 Removing intermediate container 75a8053f9dc5 ---> f8e928a8f19e Successfully built f8e928a8f19e Successfully tagged test:v11 #把镜像11当作基础镜像 [root@\ docker~]# vim Dockerfile FROM test:v11 WORKDIR /opt ONBUILD RUN echo "偷镜像吗" CMD nginx -g "daemon off;" # 执行时会先出现“偷镜像吗” [root@\ docker~]# docker build -t test:v12 . Sending build context to Docker daemon 678.3MB Step 1/4 : FROM test:v11 # Executing 1 build trigger ---> Running in c6a05b668887 偷镜像吗 Removing intermediate container c6a05b668887 ---> 8a3b66d9e8bc Step 2/4 : WORKDIR /opt ---> Running in e032ee131f16 Removing intermediate container e032ee131f16 ---> 592bf106a608 Step 3/4 : ONBUILD RUN echo "偷镜像吗" ---> Running in 84e37e1576ff Removing intermediate container 84e37e1576ff ---> e94d5f5de7f4 Step 4/4 : CMD nginx -g "daemon off;" ---> Running in 678e52ffc6fa Removing intermediate container 678e52ffc6fa ---> b0aa928e2876 Successfully built b0aa928e2876 Successfully tagged test:v12 

三,搭建LNMP(注意,虚拟机要内存2G,cpu2核)

1,创建网桥及目录

[root@\ docker~]# docker network create lnmp 14db2907cadaca14dcf78aa2d8f461e0a3d39f2bded2662292f1f925e958cdd1 #查看网桥 [root@\ docker~]# docker network ls NETWORK ID NAME DRIVER SCOPE d083bc15c0d9 bridge bridge local deb1424a66c9 host host local 14db2907cada lnmp bridge local f1c26e4592d9 none null local # 创建目录 [root@\ docker~]# mkdir lnmp [root@\ docker~]# cd lnmp/ [root@\ docker~/lnmp]# mkdir nginx [root@\ docker~/lnmp]# mkdir php [root@\ docker~/lnmp]# mkdir wordpress 

2,搭建LNMP_nginx

[root@\ docker~/lnmp/nginx]# ll -rw-r--r-- 1 root root 396 2021-03-24 11:50 default.conf -rw-r--r-- 1 root root 178 2021-03-24 11:27 Dockerfile -rw-r--r-- 1 root root 641 2021-03-24 11:36 nginx.conf #准备好官方源 [root@\ docker/lnmp/nginx]# vim nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true # 准备好配置文件 [root@\ docker~/lnmp/nginx]# cat default.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } } #准备好nginx配置文件nginx.conf;并修改用户 更改用户名user www #编辑Dockerfile [root@\ docker/lnmp/nginx]# vim DockerfileFROM centos:7 ADD nginx.repo /etc/yum.repos.d/ RUN yum install -y nginx RUN useradd www ADD default.conf /etc/nginx/conf.d/ ADD nginx.conf /etc/nginx/ EXPOSE 80 WORKDIR /usr/share/nginx CMD nginx -g "daemon off;" PS: 以上文件可以在宿主机先下载好nginx;或者打开一个nginx容器,用docker cp 把需要的文件复制在文件夹中 # 在当前生成镜像 [root@\ docker~/lnmp/nginx]# docker build -t lnmp-nginx:v1 . 

2,搭建lnmp_php

 [root@\ docker~/lnmp]# cd php #下载php包,注意版本选择5.6,支持mysql扩展库 [root@\ docker~]# wget https://www.php.net/distributions/php-5.6.40.tar.gz #把php复制在容器中 [root@\ docker~]# docker cp php-5.6.40.tar.gz 3757b128b9b5:/opt/ # 新建立一个容器,在里面编译安装php [root@\ docker~]# docker run -it centos:7 bash [root@3757b128b9b5 /]# cd /opt/ [root@3757b128b9b5 opt]# ls php-5.6.40.tar.gz # 安装依赖包 [root@3757b128b9b5 php-5.6.40]# yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers #解压php [root@3757b128b9b5 opt]# tar xf php-5.6.40.tar.gz [root@a011352ebb65 opt]# ls php-5.6.40 php-5.6.40.tar.gz #编译 [root@3757b128b9b5 opt]# cd php-5.6.40 [root@3757b128b9b5 php-5.6.40]# ./configure --with-mysql --enable-fpm [root@3757b128b9b5 php-5.6.40]# make -j [root@3757b128b9b5 php-5.6.40]# make install # 检查mysql扩展库 [root@3757b128b9b5 php-5.6.40]# php -m | grep mysql mysql mysqlnd # 复制php.ini到/usr/local/lib(php --ini查看是没有该文件的) [root@3757b128b9b5 php-5.6.40]# php --ini Configuration File (php.ini) Path: /usr/local/lib Loaded Configuration File: (none) Scan for additional .ini files in: (none) Additional .ini files parsed: (none) [root@3757b128b9b5 php-5.6.40]# cp php.ini-development /usr/local/lib/php.ini [root@3757b128b9b5 php-5.6.40]# php --ini Configuration File (php.ini) Path: /usr/local/lib Loaded Configuration File: /usr/local/lib/php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) # 以上编译好可以打标签先保存在自己的仓库以备使用 把容器保存为镜像 [root@\ docker~]# docker commit -a 'zxx' -m '编译好的php' 3757b128b9b5 php:v1-fpm-mysql sha256:b2c1d6c762d0906341dadd1f28084ecceaa23674143c6778fb3de0f1080af0dd [root@\ docker~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php v1-fpm-mysql b2c1d6c762d0 9 seconds ago 1.05GB # 登录自己的官网镜像上传 [root@\ docker~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: urbanezxx Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded #先打标签 [root@\ docker~]# docker tag b2c1d6c762d0 urbanezxx/php:v1-fpm-mysql #上传 [root@\ docker~]# docker push urbanezxx/php:v1-fpm-mysql # [root@\ docker~]# docker cp 3757b128b9b5:/usr/local/etc/php-fpm.conf.default /lnmp/php/ [root@\ docker~]# cd /lnmp/php/ [root@\ docker/lnmp/php]# ll total 28 -rw-r--r--. 1 root root 104 Mar 26 11:18 Dockerfile -rw-r--r--. 1 root root 23024 Mar 26 11:06 php-fpm.conf.default [root@\ docker/lnmp/php]# mv php-fpm.conf.default php-fpm.conf # 修改php-fpm.conf request_terminate_timeout = 0 listen = 9000 user = www group = www # 以编译好的php镜像为基础搭建新的镜像 [root@\ docker/lnmp/php]# vim Dockerfile FROM php:v1-fpm-mysql RUN useradd www ADD php-fpm.conf /usr/local/etc/ EXPOSE 9000 CMD ["php-fpm","-F"] # 生成镜像 [root@\ docker/lnmp/php]# docker build -t lnmp-php:v1 . 

3,搭建wordpress

准备好wordpress包 #先开启mysql [root@\ docker/lnmp/nginx]# docker run -d -e MYSQL_ROOT_PASSWORD=123 --network=lnmp --name mysql mysql:5.7 # 再开启php [root@\ docker/lnmp/nginx]# docker run -d -v /lnmp/wordpress/wordpress:/usr/share/nginx/html --network=lnmp --name php lnmp-php:v1 # 最后开启nginx [root@\ zxx/lnmp/nginx]# docker run -d -v /lnmp/wordpress/wordpress:/usr/share/nginx/html -p 80:80 --network=lnmp --name nginx lnmp-nginx:v1 f14b4fae5452df6f6be8105d1475810ff290a223bb9f8070c52895584442fa5d # 查看3个容器都已经启动 [root@\ zxx/lnmp/nginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f14b4fae5452 lnmp-nginx:v1 "/bin/sh -c 'nginx -…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp nginx 8ac1839c2a76 lnmp-php:v1 "php-fpm -F" 44 seconds ago Up 43 seconds 9000/tcp php 196fb1237a32 mysql:5.7 

4,验证

dockerfile构建镜像&&搭建wordpress插图

image.png

dockerfile构建镜像&&搭建wordpress插图1

image.png

遇到以上问题可以修改

[root@\ docker/lnmp/wordpress/wordpress]# cp wp-config-sample.php wp-config.php #清空内容并把提示中的代码输入进去,修改 [root@\ docker/lnmp/wordpress/wordpress]# vim wp-config.php define( 'WP_DEBUG', true ); # false修改为true 

原文链接:https://www.jianshu.com/p/4e7168bda350

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享