docker容器官網:https://hub.docker.com/html
1、centos7.4中指定安裝docker版本node
1)默認yum源安裝的docker版本爲docker1.3。性能偏低,不支持k8s。k8s目前只支持docker1.7python
谷歌瀏覽器打開。清華大學鏡像網站:https://mirrors4.tuna.tsinghua.edu.cn/mysql
找到docker的鏡像源linux
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/nginx
2)複製該連接地址git
cd /etc/yum.repos.d/ wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo 注意該文件的源並非指向清華源,因此須要修改docker-ce.repo
3)修改源github
修改源。修改前面的linux的父目錄 vim docker-ce.repo 原來的:baseurl=https://download.docker.com/linux/centos/7/$basearch/stable 原來的:baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/$basearch/stable vim命令快速修改 :%s@https://download.docker.com/@https://mirrors.tuna.tsinghua.edu.cn/docker-ce/@
回車以後。24 次替換,共 24 行 web
4)yum安裝redis
[root@Mysql yum.repos.d]# yum repolist 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.shu.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.cn99.com 源標識 源名稱 狀態 base/7/x86_64 CentOS-7 - Base 10,019 docker-ce-stable/x86_64 Docker CE Stable - x86_64 32 extras/7/x86_64 CentOS-7 - Extras 364 updates/7/x86_64 CentOS-7 - Updates 1,067 repolist: 11,482 [root@Mysql yum.repos.d]# yum install docker-ce -y
[root@Mysql yum.repos.d]# docker -v
Docker version 18.09.2, build 6247962
5)建立配置文件
[root@Mysql yum.repos.d]# mkdir -p /etc/docker [root@Mysql yum.repos.d]# touch /etc/docker/daemon.json [root@Mysql yum.repos.d]# vim /etc/docker/daemon.json [root@Mysql yum.repos.d]# cat /etc/docker/daemon.json { "registry-mirrors":["https://registry.docker-cn.com"] }
6)docker加速器
docker cn
阿里雲加速器
中國科技大學
2、docker基礎
1)啓動docker
systemctl start docker.service 啓動服務
docker version 查看版本
docker info 更詳細的信息
2)官方尋找nginx的最小穩定鏡像文件,進行容器測試
[root@Mysql ~]# docker image pull nginx:1.14-alpine
3)最小的容器測試http網絡服務
docker image pull nginx:1.14-alpine # 下載最小鏡像 docker pull busybox docker image ls docker image ls --no-trunc docker ps == docker container ls docker network ls 顯示網絡 [root@Mysql ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.14-alpine 66952fd0a8ef 2 weeks ago 16MB busybox latest 3a093384ac30 6 weeks ago 1.2MB [root@Mysql ~]# docker run --name b1 -it busybox:latest # 進入最小化的容器,進入shell交互 / # / # mkdir /data/html -p / # vi /data/html/index.html / # cat /data/html/index.html hello world / # httpd -f -h /data/html/ docker inspect b1 # 查看運行中的容器信息,包括裏面的ip地址 [root@Mysql ~]# curl 172.17.0.2 hello world
4)容器內,使用exit,退出容器。容器處於中止狀態
/ # exit [root@Mysql ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Mysql ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3cda629bceb busybox:latest "sh" 9 minutes ago Exited (0) 7 seconds ago b1
再次啓動容器:docker start -i -a b1
5)強制中止容器,刪除容器
[root@Mysql ~]# docker kill b1 b1 [root@Mysql ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3cda629bceb busybox:latest "sh" 14 minutes ago Exited (137) 10 seconds ago b1 [root@Mysql ~]# docker rm b1 b1
6)docker最小化的運行容器,且只運行一個進程,容器內並不會有存放日誌的文件夾。docker logs 容器 查看日誌
docker run --name web1 -d nginx:1.14-alpine # -d 後臺啓動 [root@Mysql ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8eeeefc3e562 nginx:1.14-alpine "nginx -g 'daemon of…" About a minute ago Up 59 seconds 80/tcp web1 [root@Mysql ~]# curl 172.17.0.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> docker run --name kvstor1 -d redis:4-alpine # 最小化啓動redis [root@Mysql ~]# docker exec -it kvstor1 /bin/sh /data # ps PID USER TIME COMMAND 1 redis 0:00 redis-server 17 root 0:00 /bin/sh 22 root 0:00 ps [root@Mysql ~]# docker logs web1 查看web1容器的日誌 172.17.0.1 - - [16/Feb/2019:17:32:49 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
7)容器的保存,刪除
docker inspect web1 顯示容器的信息 docker commit -p web1 暫停容器,保存鏡像 [root@Mysql ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> c1d0456bb6ff About a minute ago 16MB [root@Mysql ~]# docker tag c1d0456bb6ff test/httpd:v0.1-1 爲保存的容器打標籤 [root@Mysql ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE test/httpd v0.1-1 c1d0456bb6ff 3 minutes ago 16MB [root@Mysql ~]# docker tag test/httpd:v0.1-1 test/httpd:latest 再次建立標籤 [root@Mysql ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE test/httpd latest c1d0456bb6ff 5 minutes ago 16MB test/httpd v0.1-1 c1d0456bb6ff 5 minutes ago 16MB docker image rm test/httpd:latest 刪除標籤鏡像 docker run --name t1 -it test/httpd:v0.1-1 運行保存的鏡像 docker commit -a "1257063655@qq.com" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p web1 test/httpd:v0.2
8)容器的分享,打包
傳鏡像到docker.github [root@Mysql ~]# docker login -u 1257063655 登陸https://hub.docker.com/ Password: docker push 1257063655/httpd # 將鏡像上傳到https://hub.docker.com/,須要去官網創建httpd倉庫,而且1257063655/httpd 既是本地的鏡像名,也是遠程的倉庫名 https://promotion.aliyun.com/ntms/act/kubernetes.html 阿里鏡像 docker save -o myimages.gz test/httpd:v0.2 test/httpd:v0.1-1 本地打包2個鏡像,合併爲一個鏡像 [root@Mysql ~]# ls myimages.gz myimages.gz docker load -i myimages.gz 另外一臺機器導入打包的鏡像myimages.gz
3、docker網絡
1)查看網絡(擴展)
yum install bridge-utils -y [root@Mysql ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.02423f80d8e7 no veth2a1c29a veth32a55df veth90301d9 ip link show iptables -t nat -vnL docker network inspect bridge docker container inspect web1 rpm -q iproute ip netns ip netns add r1 ip netns add r2 ip netns list r2 r1 ip netns exec r1 ifconfig ip netns exec r1 ifconfig -a
iptables -t nat -vnL
2)指定運行的容器的dns,主機文件
docker run --name t1 -it --network bridge -h t1.hostname --dns 114.114.114.114 --dns-search www.com --add-host web01:172.17.0.3 --rm busybox:latest 進入容器,退出則刪除 homename =》t1.hostname cat /etc/host 172.17.0.2 t1.hostname nslookup -type=A www.baidu.com 解析路由網絡
3)端口映射等問題。必須掌握
docker run --name myweb --rm -p 80 test/httpd:v0.2 宿組機隨機映射一個端口給80 docker inspect myweb 該內部的通訊:curl 172.17.0.2 iptables -t nat -vnL 查看被全部被隨機映射的端口 docker port myweb 查看被映射的端口 更多的映射方法 docker run --name myweb --rm -p 192.168.1.5::80 test/httpd:v0.2 docker run --name myweb --rm -p 80:80 test/httpd:v0.2 docker run --name myweb --rm -p 192.168.1.5:8080:80 test/httpd:v0.2
4)容器共享網絡,共用同一個ip
容器共享網絡的方式,b1和b2共用一個網絡。相似於同一個主機運行了2個進程 docker run --name b2 --network container:b1 -it rm busybox docker run --name b2 --network host -it rm busybox 將宿機的網絡給了容器
5) 修改docker內的網絡,網卡信息
示例,
重啓服務。啓動的容器ip網緞爲:10.0.0.1的網段的
重啓服務後,
6)額外建立新的網關
docker network create -d bridge --subnet "172.26.0.0/16" --gateway "172.26.0.1" mybr0 建立本身的網絡網關 docker network ls 查看建立的本身網絡 docker run --name t1 -it --net mybr0 busybox:latest # 運行的容器爲本身的網絡
問題:同一個宿主機的2個容器在不一樣的網斷怎麼通訊。
理論上能夠直接通訊的,防火牆規則。iptables -t nat -vnL 的阻礙
4、docker的持久化存儲
1)存儲卷的基本使用
第一種,docker自行選擇映射路徑,不建議。很差區別是誰存儲的數據 docker run --name b2 -it -v /data busybox 創建存儲卷,容器內的 /data下面的數據會被宿主機保存 docker inspect b2 查看容器信息 Source": "/var/lib/docker/volumes/25dd2c087543280b9569ff34356330cf72ee74863bb5a0028a08e2802852fa83/_data", 該位置內容與容器內的 data目錄已作了關聯 第二種,手動指定位置,即使刪除容器,數據仍在 docker run --name b2 -it -v /data/volumes/b2:/data busybox [root@Mysql ~]# docker inspect -f {{.Mounts}} b2 # 過濾查找選項 [{bind /data/volumes/b2 /data true rprivate}] [root@Mysql ~]# docker inspect -f {{.NetworkSettings.IPAddress}} b2 172.17.0.5 多個容器,能夠共享同一個存儲卷
4、dockerfile語法
1)最簡單的Dockerfile語法,製做鏡像
[root@Mysql ~]# mkdir img1 [root@Mysql ~]# cd img1/ 建立鏡像文件Dockerfile文件 [root@Mysql img1]# vim Dockerfile [root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ # 注意index.html 須要在當前目錄 [root@Mysql img1]# cat index.html <h1>Busybox httpd server</h1> <h2>Hello world</h1> [root@Mysql img1]# docker build -t tinyhttpd:v0.1-1 /root/img1/ # 啓動鏡像 [root@Mysql img1]# docker image ls |grep tinyhttpd tinyhttpd v0.1-1 080174d7d1a9 About a minute ago 1.2MB
編輯被拷貝的index.html
[root@Mysql img1]# cat index.html
<h1>Busybox httpd server</h1>
<h2>Hello world</h1>
根據製做的鏡像的啓動容器,檢驗是否有文件
[root@Mysql img1]# docker run --name tinyweb1 --rm tinyhttpd:v0.1-1 cat /data/web/html/index.html <h1>Busybox httpd server</h1> <h2>Hello world</h1>
2)鏡像文件的修改,新增不一樣路徑的拷貝內容
[root@Mysql img1]# cp -r /etc/yum.repos.d/ ./ [root@Mysql img1]# ls yum.repos.d/ CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo docker-ce.repo [root@Mysql img1]# vim Dockerfile [root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ [root@Mysql img1]# docker build -t tinyhttpd:v0.1-2 /root/img1/
檢驗是否被拷貝進去
[root@Mysql img1]# docker run --name tinyweb1 --rm tinyhttpd:v0.1-2 ls /etc/yum.repos.d/ CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo CentOS-fasttrack.repo docker-ce.repo
3)add用法
以nginx爲例
3.1)ADD後面加連接地址,經過連接地址下載
複製其連接地址 http://nginx.org/download/nginx-1.15.8.tar.gz
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
檢驗add效果
docker run --name tinyweb1 --rm tinyhttpd:v0.1-3 ls /usr/local/src
3.2)ADD加本地文件,本地文件實現拷貝效果
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ ADD nginx-1.15.8.tar.gz /usr/local/src/ [root@Mysql img1]# docker build -t tinyhttpd:v0.1-4 ./
3.3)指明工做目錄。WORKDIR
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD nginx-1.15.8.tar.gz ./src/
4)建立存儲卷。VOLUME
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD nginx-1.15.8.tar.gz ./src/ VOLUME /data/mysql
5.1)暴露端口,端口僅可被宿主機訪問
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" COPY index.html /data/web/html/ COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD nginx-1.15.8.tar.gz ./src/ VOLUME /data/mysql EXPOSE 80/tcp
檢驗端口是否能夠被外界訪問
啓動:
docker run --name tinyweb1 --rm tinyhttpd:v0.1-5 /bin/httpd -f -h /data/web/html
docker inspect tinyweb1查看ip
[root@Mysql img1]# curl 172.17.0.6 <h1>Busybox httpd server</h1> <h2>Hello world</h1>
但暴露的端口也只有宿主機能訪問,沒有作端口映射
[root@Mysql img1]# docker port tinyweb1 沒有查到端口
[root@Mysql img1]# docker kill tinyweb1
5.2)啓動時加上 -P
[root@Mysql img1]# docker run --name tinyweb1 --rm -P tinyhttpd:v0.1-5 /bin/httpd -f -h /data/web/html
檢查端口
[root@Mysql img1]# docker port tinyweb1
80/tcp -> 0.0.0.0:32768
6.1)環境變量 ENV
COPY index.html ${DOC_ROOT:-/data/web/html/} 若是定義的環境變量沒有值,則用後面的
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" ENV DOC_ROOT /data/web/html/ COPY index.html ${DOC_ROOT:-/data/web/html/} COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD nginx-1.15.8.tar.gz ./src/ VOLUME /data/mysql EXPOSE 80/tcp
6.2)定義多個環境變量
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.8" COPY index.html ${DOC_ROOT:-/data/web/html/} COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/ WORKDIR /usr/local/ ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/ VOLUME /data/mysql EXPOSE 80/tcp
6.3)容器運行後輸出環境變量
[root@Mysql img1]# docker run --name tinyweb1 --rm -P tinyhttpd:v0.1-6 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=634ff3a5d399 DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE=nginx-1.15.8 HOME=/root 運行時,再次傳變量,有的原變量會被替換 [root@Mysql img1]# docker run --name tinyweb1 --rm -P -e WEB_SERVER_PACKAGE=nginx-1.15.7 tinyhttpd:v0.1-6 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=7dc972609f71 WEB_SERVER_PACKAGE=nginx-1.15.7 DOC_ROOT=/data/web/html/ HOME=/root
7.1)製做鏡像時RUN 命令
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz" COPY index.html ${DOC_ROOT:-/data/web/html/} COPY yum.repos.d /etc/yum.repos.d/ ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/ WORKDIR /usr/local/ # ADD ${WEB_SERVER_PACKAGE} ./src/ VOLUME /data/mysql/ EXPOSE 80/tcp RUN cd /usr/local/src && \ tar xf ${WEB_SERVER_PACKAGE}
7.2)命令測試
[root@Mysql img1]# cat Dockerfile # Description:test image FROM busybox:latest MAINTAINER "Test <1257063655@qq.com>" # LABEL maintainer="1257063655@qq.com" ENV DOC_ROOT=/data/web/html/ \ WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz" COPY index.html ${DOC_ROOT:-/data/web/html/} COPY yum.repos.d /etc/yum.repos.d/ # ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/ WORKDIR /usr/local/ ADD ${WEB_SERVER_PACKAGE} ./src/ VOLUME /data/mysql/ EXPOSE 80/tcp RUN cd /usr/local/src && \ mv nginx-1.15.8 nginx [root@Mysql img1]# docker build -t tinyhttpd:v0.2.1 ./ Sending build context to Docker daemon 1.051MB Step 1/10 : FROM busybox:latest ---> 3a093384ac30 Step 2/10 : MAINTAINER "Test <1257063655@qq.com>" ---> Using cache ---> c04c090e9e40 Step 3/10 : ENV DOC_ROOT=/data/web/html/ WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz" ---> Using cache ---> 6832ee6cc92e Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/} ---> Using cache ---> 45c2e54c0d74 Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/ ---> Using cache ---> 348917f42afe Step 6/10 : WORKDIR /usr/local/ ---> Using cache ---> df3d710f5ac9 Step 7/10 : ADD ${WEB_SERVER_PACKAGE} ./src/ ---> Using cache ---> 83c0b4f691e4 Step 8/10 : VOLUME /data/mysql/ ---> Using cache ---> c13d38f9b94b Step 9/10 : EXPOSE 80/tcp ---> Using cache ---> 7bd7c1d365cf Step 10/10 : RUN cd /usr/local/src && mv nginx-1.15.8 nginx ---> Running in 35f4b3aae6a1 Removing intermediate container 35f4b3aae6a1 ---> 41413a7ce98a Successfully built 41413a7ce98a Successfully tagged tinyhttpd:v0.2.1
7.3)根據命令yum安裝nginx
FROM centos
RUN yum -y install epel-release && yum makecache && yum install nginx -y
8.1)CMD命令的使用
[root@Mysql img2]# cat Dockerfile FROM busybox LABEL maintainer="My <1257063655.qq.com>" app="httpd" ENV WEB_DOC_ROOT="/data/web/html" RUN mkdir -p $WEB_DOC_ROOT && \ echo '<h1>hello world,httpd server</h1>' > ${WEB_DOC_ROOT}/index.html CMD /bin/httpd -f -h ${WEB_DOC_ROOT} [root@Mysql img2]# docker build -t tinyhttpd:v0.2-1 ./
查看製做鏡像的詳細信息
docker image inspect tinyhttpd:v0.2-1
運行
docker run --name tinyweb2 -it --rm -P tinyhttpd:v0.2-1
查看
[root@Mysql ~]# docker exec -it tinyweb2 /bin/sh / # / # ps PID USER TIME COMMAND 1 root 0:00 /bin/httpd -f -h /data/web/html 6 root 0:00 /bin/sh 11 root 0:00 ps
9)不會被覆蓋的運行命令。ENTRYPOINT
[root@Mysql img2]# cat Dockerfile FROM busybox LABEL maintainer="My <1257063655.qq.com>" app="httpd" ENV WEB_DOC_ROOT="/data/web/html" RUN mkdir -p $WEB_DOC_ROOT && \ echo '<h1>hello world,httpd server</h1>' > ${WEB_DOC_ROOT}/index.html # CMD /bin/httpd -f -h ${WEB_DOC_ROOT} # CMD ["/bin/sh","-c","/bin/httpd","-f","-h ${WEB_DOC_ROOT}"] # 有問題,不建議用 ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
啓動容器。docker run --name tinyweb2 -it --rm -P tinyhttpd:v0.2-3 ls /data/ 後面接的命令不會覆蓋製做鏡像用的命令。後面接的命令會被當作參數傳給它
10) 腳本啓動nginx。使用傳參的方法
10.1)編輯腳本文件
[root@Mysql img3]# cat entrypoint.sh #!/bin/sh # cat > /etc/nginx/conf.d/www.conf <<EOF server { server_name ${HOSTNAME}; listen ${IP:-0.0.0.0}:${PORT:-80}; root ${NGX_DOC_ROOT:-/usr/share/nginx/html}; } EOF exec "$@"
10.2)編輯首頁文件
[root@Mysql img3]# cat index.html <h1> Hello world,study python </h1>
10.3)編輯Dockerfile文件
[root@Mysql img3]# cat Dockerfile FROM nginx:1.14-alpine LABEL maintainer="my <125.7063655@qq.com>" ENV NGX_DOC_ROOT="/data/web/html/" ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh /bin/ CMD ["/usr/sbin/nginx","-g","daemon off;"] ENTRYPOINT ["/bin/entrypoint.sh"]
10.4)啓動容器檢驗
啓動 [root@Mysql img3]# docker run --name myweb1 --rm -P myweb:v0.3-3 進入容器 [root@Mysql img3]# docker exec -it myweb1 /bin/sh / # cat /etc/nginx/conf.d/www.conf server { server_name 0e107f492212; listen 0.0.0.0:80; root /data/web/html; } / # cat /data/web/html <h1> Hello world,study python </h1> / # netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -------- / # wget -O - -q localhost 本地明 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ------------------------- / # wget -O - -q 0e107f492212 <h1> Hello world,study python </h1>
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
10.5)啓動容器時再增長端口參數,測試
添加端口 [root@Mysql img3]# docker run --name myweb1 --rm -P -e "PORT=8080" myweb:v0.3-4 / # [root@Mysql img3]# docker exec -it myweb1 /bin/sh / # netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
11)健康檢查。HEALTHCHECK 健康檢查,不太清楚。
[root@Mysql img3]# cat Dockerfile FROM nginx:1.14-alpine LABEL maintainer="my <125.7063655@qq.com>" ENV NGX_DOC_ROOT="/data/web/html/" ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh /bin/ EXPOSE 80/tcp HEALTHCHECK --start-period=3s CMD wget -o - -q http://${IP:-0.0.0.0}:${PORT:-80}/ CMD ["/usr/sbin/nginx","-g","daemon off;"] ENTRYPOINT ["/bin/entrypoint.sh"]
12.1) ARG常量傳參的用法
[root@Mysql img3]# cat Dockerfile FROM nginx:1.14-alpine ARG author="my <125.7063655@qq.com>" LABEL maintainer="${author}" ENV NGX_DOC_ROOT="/data/web/html/" ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh /bin/ EXPOSE 80/tcp HEALTHCHECK --start-period=3s CMD wget -o - -q http://${IP:-0.0.0.0}:${PORT:-80}/ CMD ["/usr/sbin/nginx","-g","daemon off;"] ENTRYPOINT ["/bin/entrypoint.sh"]
替換定義的常量
docker build -t myweb:v0.3-7 ./ 替換定義的默認值 author="pony <pony@qq.com>" docker build --build-arg author="pony <pony@qq.com>" -t myweb:v0.3-8 ./
13)ONBUILD 。根據ONBUILD 建立的鏡像,其餘dockerfile引用此鏡像建立鏡像時,會觸發執行ONBUILD 裏面的指令
FROM nginx:1.14-alpine ARG author="my <125.7063655@qq.com>" LABEL maintainer="${author}" ENV NGX_DOC_ROOT="/data/web/html/" ADD index.html ${NGX_DOC_ROOT} ADD entrypoint.sh /bin/ EXPOSE 80/tcp HEALTHCHECK --start-period=3s CMD wget -o - -q http://${IP:-0.0.0.0}:${PORT:-80}/ ONBUILD ADD http://repo.webtatic.com/yum/el6/latest.rpm /usr/local/src/ CMD ["/usr/sbin/nginx","-g","daemon off;"] ENTRYPOINT ["/bin/entrypoint.sh"]
14)GitHub裏面有很大dockerfile文件
5、resistry使用
[root@Mysql img3]# yum info docker-registry 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.shu.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 可安裝的軟件包 名稱 :docker-registry 架構 :x86_64 版本 :0.9.1 發佈 :7.el7 大小 :123 k 源 :extras/7/x86_64 簡介 : Registry server for Docker 網址 :https://github.com/docker/docker-registry 協議 : ASL 2.0 描述 : Registry server for Docker (hosting/delivering of repositories and images).
1)在服務端安裝resistry 私有倉庫
[root@Mysql img3]# yum info docker-registry [root@Mysql img3]# yum install docker-registry -y 安裝 [root@Mysql img3]# rpm -ql docker-distribution 查看安裝生成的文件 /etc/docker-distribution/registry/config.yml 主配置文件 /usr/bin/registry /usr/lib/systemd/system/docker-distribution.service /usr/share/doc/docker-distribution-2.6.2 /usr/share/doc/docker-distribution-2.6.2/AUTHORS /usr/share/doc/docker-distribution-2.6.2/CONTRIBUTING.md /usr/share/doc/docker-distribution-2.6.2/LICENSE /usr/share/doc/docker-distribution-2.6.2/MAINTAINERS /usr/share/doc/docker-distribution-2.6.2/README.md /var/lib/registry [root@Mysql registry]# systemctl start docker-distribution 啓動服務 [root@Mysql registry]# netstat -lntup|grep registry tcp6 0 0 :::5000 :::* LISTEN 22359/registry
2)推送文件,拉取文件測試
[root@Centos7pvz2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.14-alpine 8a2fb25a19f5 9 days ago 16MB [root@Centos7pvz2 ~]# docker tag nginx:1.14-alpine pvz2.test.com:5000/nginx:1.15-alpine # 打標記爲本身的倉庫鏡像 [root@Centos7pvz2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pvz2.test.com:5000/nginx 1.15-alpine 8a2fb25a19f5 9 days ago 16MB nginx 1.14-alpine 8a2fb25a19f5 9 days ago 16MB [root@Centos7pvz2 ~]# cat /etc/docker/daemon.json # 修改docker拉取服務信息 { "registry-mirrors": ["https://4mii0w1b.mirror.aliyuncs.com","https://registry.docker-cn.com"], "insecure-registries": ["pvz2.test.com:5000"] } [root@Centos7pvz2 ~]# systemctl restart docker 重啓docker [root@Centos7pvz2 ~]# cat /etc/hosts # 配置主機解析文件 192.168.10.28 pvz2.test.com [root@Centos7pvz2 ~]# docker push pvz2.test.com:5000/nginx:1.15-alpine # 將本地鏡像推向倉庫 The push refers to repository [pvz2.test.com:5000/nginx] 076c58d2644f: Pushed b2cbae4b8c15: Pushed 5ac9a5170bf2: Pushed a464c54f93a9: Pushed 1.15-alpine: digest: sha256:a3a0c4126587884f8d3090efca87f5af075d7e7ac8308cffc09a5a082d5f4760 size: 1153 另外一臺機器拉取鏡像 [root@node02 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://4mii0w1b.mirror.aliyuncs.com","https://registry.docker-cn.com"], "insecure-registries": ["pvz2.test.com:5000"] } [root@Centos7pvz2 ~]# systemctl restart docker [root@Centos7pvz2 ~]# cat /etc/hosts # 配置主機解析文件 192.168.10.28 pvz2.test.com [root@node02 ~]# docker pull pvz2.test.com:5000/nginx:1.15-alpine # 拉取鏡像 1.15-alpine: Pulling from nginx bdf0201b3a05: Pull complete 3d0a573c81ed: Pull complete 8129faeb2eb6: Pull complete 3dc99f571daf: Pull complete Digest: sha256:a3a0c4126587884f8d3090efca87f5af075d7e7ac8308cffc09a5a082d5f4760 Status: Downloaded newer image for pvz2.test.com:5000/nginx:1.15-alpine [root@node02 ~]#
3)服務端可查看客戶端推送過來的鏡像文件
[root@Centos7pvz2 ~]# ll /var/lib/registry/docker/registry/v2/repositories/ 總用量 0 drwxr-xr-x 5 root root 55 4月 19 16:50 nginx
6、單機多容器編排
7、harbor安裝(倉庫網站安裝)
1)安裝說明
1)官網 https://github.com/goharbor/harbor 2)安裝說明 https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md 3)下載安裝包 https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgz
2)修改配置文件
3)啓動服務 ./install.sh 須要docker-compose(1.7.1以上的版本)
3.1 )安裝docker-compose。注意該安裝方式版本 爲 docker-compose version 1.24.0, build 0aa5906 。通過測試,也能夠運行
./install.sh結束後,80端口和443端口被監聽
4)訪問 http://192.168.10.28/harbor/sign-in
用戶:admin
密碼:harbor12345 / Harbor12345 官方文檔有說明(harbor.cfg)
登陸進來。先建立用戶管理,倉庫管理
5)建立用戶
6)建立倉庫
7)切換帳號,新建項目
8)推送docker鏡像文件
[root@Centos7pvz2 harbor]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://4mii0w1b.mirror.aliyuncs.com","https://registry.docker-cn.com"], "insecure-registries": ["pvz2.test.com"] } [root@Centos7pvz2 harbor]# systemctl restart docker [root@Centos7pvz2 harbor]# docker tag goharbor/harbor-db:v1.7.4 pvz2.test.com/devel/harbor-db:v1.7.4 [root@Centos7pvz2 harbor]# docker tag goharbor/harbor-adminserver:v1.7.4 pvz2.test.com/devel/harbor-adminserver:v1.7.4 [root@Centos7pvz2 harbor]# docker image ls|grep pvz2.test.com/devel pvz2.test.com/devel/harbor-adminserver v1.7.4 5706c65d65dc 7 weeks ago 72.3MB pvz2.test.com/devel/harbor-db v1.7.4 08d163f732f3 7 weeks ago 136MB [root@Centos7pvz2 harbor]# docker login pvz2.test.com 登陸服務器 [root@Centos7pvz2 harbor]# docker push pvz2.test.com/devel/harbor-adminserver:v1.7.4 [root@Centos7pvz2 harbor]# docker push pvz2.test.com/devel/harbor-db:v1.7.4
查看服務端,文件存儲的路徑
暫停容器服務
繼續運行
docker資源
啓動測試,256M內存,2個進程
docke stats 能查看容器資源
8、最終環節,應用實戰
最終章。dokerfiles實戰應用
1)快速構建基礎鏡像
cat Dockerfile #Docker from CentOS # Base images FROM centos # who MAINTAINER Mr.Cao 11111qq.com # EPEL add epel.repo /etc/yum.repos.d/ # Base pkg RUN yum install -y wget mysql-devel supervisor git redis tree net-tools sudo psmisc && yum clean all docker build -t test/centos:base .
1.2)基於基礎鏡像建立python環境
cat Dockerfile FROM test/centos:base MAINTAINER Mr.Cao 11111qq.com RUN yum install -y python-devel python-pip supervisor RUN pip install --upgrade pip docker build -t test/python .
2)基於ssh管理的基礎鏡像
cat Dockerfile #Docker from CentOS # Base images FROM centos # who MAINTAINER Mr.Cao 11111qq.com # EPEL add epel.repo /etc/yum.repos.d/ # Base pkg RUN yum install -y openssh-clients openssl-devel openssh-server wget mysql-devel supervisor git redis tree net-tools sudo psmisc && yum clean all # For SSHD RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key RUN echo "root:helloworld" | chpasswd docker build -t test/centos-ssh:base .
2.1)基於ssh的Python環境
cat Dockerfile FROM test/centos-ssh MAINTAINER Mr.Cao 11111qq.com RUN yum install -y python-devel python-pip supervisor RUN pip install --upgrade pip docker build -t test/python-ssh .
3)基於Python環境運行app
supervisord.conf文件修改
3.1) 編輯文件過程
================================================ cat app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'hello world' if __name__ == '__main__': app.run(host="0.0.0.0",debug=True) ================== cat requirements.txt flask =================== cat app-supervisor.ini [program:shop-api] command=/usr/bin/python2.7 /opt/app.py process_name=%(program_name)s autostart=true user=www stdout_logfile=/tmp/app.log stderr_logfile=/tmp/app.error [program:sshd] command=/usr/sbin/sshd -D process_name=%(program_name)s autostart=true ================== cat Dockerfile FROM test/python-ssh MAINTAINER Mr.Cao 11111qq.com RUN useradd -s /sbin/nologin -M www ADD app.py /opt/app.py ADD requirements.txt /opt/ ADD supervisord.conf /etc/supervisord.conf ADD app-supervisor.ini /etc/supervisord.d/ RUN /usr/bin/pip2.7 install /opt/requirements.txt # Port EXPOSE 22 5000 # CMD CMD ["/usr/bin/supervisord","-c","/etc/supervisord.conf"] ===============================================================
3.2)製做鏡像,並啓動應用服務
docker build -t test/hello-api . docker run --name hello-api -d -p 88:5000 -p 8022:22 test/hello-api
ssh服務可這樣進入服務