1、获取代码,fork到自己的仓库
2、将代码克隆到服务器本地
(1)运行git命令前需要先安装git
安装git命令:
yum -y install git
检验:
[root@aubin ~]# git --version git version 1.8.3.1
克隆:
[root@aubin ~]# git clone https://gitee.com/zhai_huabing/docker-training.git Cloning into 'docker-training'... remote: Enumerating objects: 2057, done. remote: Counting objects: 100% (2057/2057), done. remote: Compressing objects: 100% (1331/1331), done. remote: Total 2057 (delta 688), reused 2057 (delta 688), pack-reused 0 Receiving objects: 100% (2057/2057), 7.13 MiB | 765.00 KiB/s, done. Resolving deltas: 100% (688/688), done.
进入克隆到的目录:
[root@aubin docker-training]# ls
centos7 LICENSE mysql php-fpm README.md wordpress
进入centos的镜像文件,查看镜像的Dockerfile:
[root@aubin centos7]# ls aliyun-epel.repo aliyun-mirror.repo Dockerfile supervisord.conf [root@aubin centos7]# vi Dockerfile # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION 1.6.2 # # Dockerizing CentOS7: Dockerfile for building CentOS images # FROM centos:centos7.1.1503 MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> ENV TZ "Asia/Shanghai" ENV TERM xterm ADD aliyun-mirror.repo /etc/yum.repos.d/CentOS-Base.repo ADD aliyun-epel.repo /etc/yum.repos.d/epel.repo RUN yum install -y curl wget tar bzip2 unzip vim-enhanced passwd sudo yum-utils hostname net-tools rsync man && yum install -y gcc gcc-c++ git make automake cmake patch logrotate python-devel libpng-devel libjpeg-devel && yum install -y --enablerepo=epel pwgen python-pip && yum clean all RUN pip install supervisor ADD supervisord.conf /etc/supervisord.conf RUN mkdir -p /etc/supervisor.conf.d && mkdir -p /var/log/supervisor EXPOSE 22 ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
3、利用DockerFile创建一个centos镜像并生成容器
(1)build一个镜像
docker build -t zhai/20201027/centos:7.1 .
点代表的是当前目录
(2)查看镜像
[root@aubin centos7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhai/20201027/centos 7.1 1a86793f175f 41 seconds ago 626MB zhai/centos latest 8f28e3fd97da 4 days ago 215MB tomcat latest 625b734f984e 4 days ago 648MB mysql 5.6 c580203d8753 4 days ago 302MB redis latest bd571e6529f3 13 days ago 104MB mongo latest ba0c2ff8d362 4 weeks ago 492MB centos latest 0d120b6ccaa8 2 months ago 215MB hello-world latest bf756fb1ae65 9 months ago 13.3kB centos centos7.1.1503 e1430271e2f9 19 months ago 212MB tomcat 8.5.32 5808f01b11bf 2 years ago 463MB
(3)根据镜像生成容器
[root@aubin centos7]# docker run -d -p 1233:22 --name centosbase zhai/20201027/centos:7.1 880d09c3e6e245d9cef60c0ff78c268c7110d46b24fd36eb0811f5a7792b002e [root@aubin centos7]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 880d09c3e6e2 zhai/20201027/centos:7.1 "/usr/bin/supervisor猞" 19 seconds ago Up 9 seconds 0.0.0.0:1233->22/tcp centosbase 89c7173b7764 mysql:5.6 "docker-entrypoint.s猞" 3 days ago Up 20 hours 0.0.0.0:12345->3306/tcp mysql
-d:后台运行
exec -it:交互式模式
-p:自定义端口映射
-P:随机端口映射
4、利用DockerFile创建一个php镜像并创建容器
(1)查看php镜像的Dockerfile
[root@aubin docker-training]# ls centos7 LICENSE mysql php-fpm README.md wordpress [root@aubin docker-training]# cd php-fpm [root@aubin php-fpm]# ls Dockerfile nginx_default.conf nginx_nginx.conf php_www.conf supervisor_nginx.conf supervisor_php-fpm.conf [root@aubin php-fpm]# vi Dockerfile # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION 1.6.2 # # Dockerizing php-fpm: Dockerfile for building php-fpm images # FROM zhai/20201027/centos:7.1 基于centos7.1作为父镜像,这个镜像要确保自己的docker中已经存在该镜像 MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> 谁来维护,作者和邮箱 # Set environment variable ENV APP_DIR /app RUN rpm --rebuilddb && yum -y swap -- remove fakesystemd -- install systemd systemd-libs && yum -y install nginx php-cli php-mysql php-pear php-ldap php-mbstring php-soap php-dom php-gd php-xmlrpc php-fpm php-mcrypt && yum clean all 安装一些服务 ADD nginx_nginx.conf /etc/nginx/nginx.conf ADD nginx_default.conf /etc/nginx/conf.d/default.conf ADD php_www.conf /etc/php-fpm.d/www.conf RUN sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini RUN mkdir -p /app && echo "<?php phpinfo(); ?>" > ${APP_DIR}/info.php EXPOSE 80 443 ADD supervisor_nginx.conf /etc/supervisor.conf.d/nginx.conf ADD supervisor_php-fpm.conf /etc/supervisor.conf.d/php-fpm.conf ONBUILD ADD . /app ONBUILD RUN chown -R nginx:nginx /app ~
(2)创建镜像并创建该镜像的一个容器
创建镜像:
docker build -t zhai/php-fpm:5.4 .
创建镜像的容器:
[root@aubin php-fpm]# docker run -d -p 1234:80 --name zhaiphp zhai/php-fpm:5.4 e6184d5a6777034f7faf257ab5b206f4421bac8926af0af266bd19959b859462
(3)访问info.php页面测试php的容器是否创建成功
(4)查看容器的服务
[root@e6184d5a6777 /]# supervisorctl nginx RUNNING pid 8, uptime 0:08:26 php-fpm RUNNING pid 9, uptime 0:08:26 supervisor>
退出用exit命令,退出一个容器也用exit命令
5、创建mysql镜像
(1)查看mysql的Dockerfile
[root@aubin docker-training]# ls centos7 LICENSE mysql php-fpm README.md wordpress [root@aubin docker-training]# cd mysql [root@aubin mysql]# ls Dockerfile mysqld_charset.cnf scripts [root@aubin mysql]# vi Dockerfile # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION 1.6.2 # # Dockerizing Mariadb: Dockerfile for building Mariadb images # FROM csphere/centos:7.1 MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> ENV DATA_DIR /var/lib/mysql # Install Mariadb RUN rpm --rebuilddb && yum install -y mariadb mariadb-server && yum clean all ADD mysqld_charset.cnf /etc/my.cnf.d/ COPY scripts /scripts RUN chmod +x /scripts/start EXPOSE 3306 VOLUME ["/var/lib/mysql"] 数据卷,进行容器与宿主机的映射,保证在容器删除的情况下,容器中的数据不会丢失 ENTRYPOINT ["/scripts/start"] ~ ~
(2)运用docker的build命令创建一个镜像
(3)创建一个容器
不指定数据卷,在删除容器后容器的数据不保存:
[root@aubin mysql]# docker run -d -p 3306:3306 --name dbserver zhai/20201027/mysql:5.5 53718319f10aed8a41b0ecfa681a39ce91ee880e55a7c50224c2aa9347620434
定义数据卷,在删除容器后日志与数据依旧能够保存:
docker run -p 12345:3306
--name mysql
-v /zzyyuse/mysql/conf:/etc/mysql/conf.d
-v /zzyyuse/mysql/logs:/logs
-v /zzyyuse/mysql/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456
-d mysql:5.6
(4)交互式运行
[root@aubin mysql]# docker exec -it dbserver /bin/bash [root@53718319f10a /]# mysql Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 1 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.05 sec)
6、利用容器构建一个动态网站
(1)查看wordpress的Dockerfile文件
[root@aubin wordpress]# cd .. [root@aubin docker-training]# cd wordpress [root@aubin wordpress]# ls Dockerfile license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php index.php readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php init.sh wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php [root@aubin wordpress]# vi Dockerfile from zhai/php-fpm:5.4 add init.sh /init.sh 脚本 entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
该文件是与php的Dockerfile文件相关联的:
ADD supervisor_nginx.conf /etc/supervisor.conf.d/nginx.conf ADD supervisor_php-fpm.conf /etc/supervisor.conf.d/php-fpm.conf ONBUILD ADD . /app ONBUILD RUN chown -R nginx:nginx /app ~
ONBUILD:父镜像在被子继承后父镜像的onbuild被触发,也就是说在构建wordpress的时候最后两条命令将会被执行
(2)构建wordpress的镜像
[root@aubin wordpress]# docker build -t zhai/20201027/wordpress:4.2 . Sending build context to Docker daemon 19.8MB Step 1/3 : from zhai/php-fpm:5.4 # Executing 2 build triggers ---> Running in 782dec5ed952 Removing intermediate container 782dec5ed952 ---> 660aa0c11c63 Step 2/3 : add init.sh /init.sh ---> 7696f83d43c0 Step 3/3 : entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"] ---> Running in 5618dda9722b Removing intermediate container 5618dda9722b ---> 73b872365fc6 Successfully built 73b872365fc6 Successfully tagged zhai/20201027/wordpress:4.2
(3)要保证php和mysql的容器都已经创建
[root@aubin ~]# docker run -d -p 80:80 --name wordpress3
-e WORDPRESS_DB_HOST=192.168.88.130
-e WORDPRESS_DB_USER=root
-e WORDPRESS_DB_PASSWORD=123456 zhai/20201027/wordpress:4.2 aa342a895f5ee66d7c9e29d7eb9792703a061a490bd72b22945c3a12c2039ef3 [root@aubin ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aa342a895f5e zhai/20201027/wordpress:4.2 "/init.sh /usr/bin/s猞" 37 seconds ago Up 34 seconds 22/tcp, 443/tcp, 0.0.0.0:80->80/tcp wordpress3 df4e5ccf9c0d zhai/php-fpm:5.4 "/usr/bin/supervisor猞" 3 minutes ago Up 3 minutes 22/tcp, 443/tcp, 0.0.0.0:1234->80/tcp zhaiphp1 d4672dec387f mysql:5.6
-e是传递参数,给脚本init.sh传递执行所需要的参数