ps:有點相似於java代碼,一次構建處處運行php
相關說明:
image: 和虛擬機的鏡像相似
container: 用鏡像建立的實例
repository: 相似於yum倉庫
docker client: 命令行輸入的docker命令
docker server: 啓動的docker進程java
1.虛擬機須要hypervisor這個中間層來進行支持,上面跑的每個虛擬機他們之間都是相互隔離的,都有獨立的操做系統。
2.docker 須要docker engine來進行支持,每一個container之間 , 用lxc技術來進行隔離。
python
yum -y install docker systemctl start docker systemctl enable docker
搜索鏡像mysql
docker search centos
導出鏡像linux
docker save -o centos.tar centos
導入鏡像nginx
docker load -i centos.tar
查看導入的imagesgit
[root@hr-vm-11 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 8140d0c64310 7 weeks ago 192.5 MB docker.io/nginx latest 3448f27c273f 7 weeks ago 109.4 MB
刪除鏡像github
ps: 若是鏡像有已建立的容器, 鏡像是沒法被刪除的.web
[root@hr-vm-11 ~]# docker rmi centos Untagged: centos:latest Deleted: sha256:8140d0c64310d4e290bf3938757837dbb8f806acba0cb3f6a852558074345348 Deleted: sha256:b51149973e6a6c4fb1091ef34ff70002ee307e971b9982075cf226004a93c9b7 [root@hr-vm-11 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 3448f27c273f 7 weeks ago 109.4 MB
啓動docker容器redis
格式: docker run 參數 容器名 執行的命令
[root@hr-vm-11 ~]# docker run --rm -i -t centos hostname 7d7e0e228d24
進入到docker
[root@hr-vm-11 ~]# docker run --rm -i -t centos /bin/bash [root@c8efa61caf0e /]#
查看docker全部建立的容器
[root@hr-vm-11 ~]# docker run -i -t centos ps -a PID TTY TIME CMD [root@hr-vm-11 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 53c158c4560b centos "ps -a" 6 seconds ago Exited (1) 4 seconds ago admiring_roentgen
查看正在運行的容器
ps: 只有將程序啓動在前臺, 在這裏才能看到.
docker ps
用帶標籤的方式啓動docker
ps: 若是有修改容器裏文件內容的需求, 須要從新的建立容器,由於docker的理念是不可變基礎設施.
[root@hr-vm-11 ~]# docker run --name centosv1 -i -t centos /bin/bash [root@4ddd206ef3ce /]# exit [root@hr-vm-11 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ddd206ef3ce centos "/bin/bash" 8 seconds ago Exited (0) 4 seconds ago centosv1 53c158c4560b centos "ps -a" 5 minutes ago Exited (1) 5 minutes ago admiring_roentgen
用標籤啓動
ps: 此時這個container沒有運行, 由於沒有程序運行在前臺.
docker start centosv1 docker stop centosv1
只運行容器,運行事後就刪除
(ps:container執行過命令以後就會自動的刪除掉)
[root@hr-vm-11 ~]# docker run --rm -i -t centos echo hehe hehe [root@hr-vm-11 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ddd206ef3ce centos "/bin/bash" 6 minutes ago Exited (0) 6 minutes ago centosv1 53c158c4560b centos "ps -a" 12 minutes ago Exited (1) 11 minutes ago admiring_roentgen
刪除容器
ps: 若是容器在運行,沒法被刪除.
docker rm '容器名'
-d
運行一個容器在後臺,並打印出容器id
[root@hr-vm-11 ~]# docker run -d --name nginx2 nginx d609da66283da14b6b4f07d7adf801640270904aebd274054914c1000a0b912f [root@hr-vm-11 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d609da66283d nginx "nginx -g 'daemon off" 30 seconds ago Up 28 seconds 80/tcp nginx2
查看容器的日誌
docker logs nginx2
方法一: 使用attach
進入容器,退出後,容器也退出了, 生產環境裏不用.
[root@hr-vm-11 ~]# docker run --name centosv1 -i -t centos /bin/bash [root@47d5b101d62c /]# exit [root@hr-vm-11 ~]# docker start centosv1 centosv1 [root@hr-vm-11 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 47d5b101d62c centos "/bin/bash" 31 seconds ago Up 19 seconds centosv1 [root@hr-vm-11 ~]# docker attach centosv1 [root@47d5b101d62c /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.1 0.1 11768 1872 ? Ss 16:39 0:00 /bin/bash root 15 0.0 0.0 47440 1676 ? R+ 16:39 0:00 ps aux [root@47d5b101d62c /]# exit [root@hr-vm-11 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@hr-vm-11 ~]#
方法二:使用nsenter
進入容器後,ps aux 發現是單獨的開了一個進程進去的.
[root@hr-vm-11 ~]# yum -y install util-linux 獲取到容器pid [root@hr-vm-11 ~]# docker inspect -f '{{.State.Pid}}' nginx2 5730 進入容器 [root@hr-vm-11 ~]# nsenter -t 5730 -m -u -i -n -p ps aux發現是利用nsentor進入container 是單獨的開了一個進程的. [root@47d5b101d62c /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11768 1680 ? Ss+ 16:47 0:00 /bin/bash root 29 0.0 0.1 15200 1992 ? S 16:53 0:00 -bash root 42 0.0 0.0 50872 1820 ? R+ 16:53 0:00 ps aux [root@47d5b101d62c /]# logout 退出以後centosv1依然運行在後臺 [root@hr-vm-11 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 47d5b101d62c centos "/bin/bash" 14 minutes ago Up 5 minutes centosv1
ps: 生產環境應該寫成一個腳本,利用腳本進入
[root@hr-vm-10 ~]# cat docker.sh #!/bin/bash # auth: fei.wang@hrfax.cn # des: login in docker function docker_in(){ Name=$1 [ -z "$Name" ] && echo 'error' && exit DockerPid=$(docker inspect -f '{{.State.Pid}}' $Name) nsenter -t ${DockerPid} -m -u -i -n -p } docker_in $1 [root@hr-vm-10 ~]# chmod +x docker.sh [root@hr-vm-10 ~]# ./docker.sh centosv1 [root@47d5b101d62c /]#
方法三:
exec 運行一個命令在一個已經運行的容器裏.(生產環境不用,有坑)
[root@hr-vm-10 ~]# docker exec centosv1 date Wed Jul 5 10:24:15 UTC 2017 [root@hr-vm-10 ~]# docker exec -it centosv1 /bin/bash [root@47d5b101d62c /]#
-P 隨機映射
[root@hr-vm-10-docker ~]# docker run -d --name nginx-v1 -P nginx 59a85323c8afab3bb9c2e450cf96d6448b075bdf5e361d0bf141ccfe660c243e
查看映射的端口
方法一: [root@hr-vm-10-docker ~]# docker port nginx-v1 80/tcp -> 0.0.0.0:32770 方法二: [root@hr-vm-10-docker ~]# iptables -t nat -vnL|grep DNAT 0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32770 to:172.17.0.9:80
格式:
-p hostPort:containerPort
docker run -d --name nginx-v2 -p 8088:80 nginx
-p ip:hostPort:containerPort
docker run -d --name nginx-v3 -p 192.168.1.11:8089:80 nginx
-p ip::containerPort
docker run -d --name nginx-v4 -p 192.168.1.11::80 nginx
docker run -d --name nginx-v5 -p 8088:80:udp nginx
ps: 可使用-p來綁定多個端口.
docker run -d --name nginx-v6 -p 81:80 -p 83:22 nginx
docker鏡像是分層設計的,容器也能夠封裝成一個image
使用數據卷能夠提升性能, 相似與mount.
-v /data 隨機掛載系統目錄
ps: 使用此方式掛載後,你也不知道掛載的是哪一個目錄,不建議使用
[root@hr-vm-10 ~]# docker run -d --name nginxv20 -v /data/ nginx 7e8f0744eb3fed1b402b35dba0c7ca3cdd743edbced67b47781ab0fcbcbdee23 [root@hr-vm-10 ~]# ./docker.sh nginxv20 mesg: ttyname failed: No such file or directory df -h 也看不見掛載的是系統的那個目錄 root@7e8f0744eb3f:/# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/docker-253:1-665852-39abc775a9c003ffeca984adf975e8fc43fb4727803daa6a923c1c72a1934e74 10G 148M 9.9G 2% / tmpfs 912M 0 912M 0% /dev tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/mapper/cl-root 17G 2.2G 15G 13% /data shm
查看掛載的系統目錄是哪一個物理目錄
[root@hr-vm-10 ~]# docker inspect -f {{.Mounts}} nginxv20 [{844b66331c243b7a1914eb65ec68f83da35d360e14789f216fded8527c0f8f76 /var/lib/docker/volumes/844b66331c243b7a1914eb65ec68f83da35d360e14789f216fded8527c0f8f76/_data /data local true }]
-v src:dst 掛載指定的目錄
ps: 可移植性差,換個機器就不必定有你掛載的這個目錄.
[root@hr-vm-10 ~]# docker run -d --name nginxv100 -v /data/images:/data nginx c9358e2ce458293cad9a5f4fae95172b5d6609b1126f607b661e30a6d87dd21b [root@hr-vm-10 ~]# ./docker.sh nginxv100 root@c9358e2ce458:/# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/docker-253:1-665852-0de2894a933e80bdcabd8364266e0094272889a7a76d312b98ee2eaf4f60da7c 10G 147M 9.9G 2% / tmpfs 912M 0 912M 0% /dev tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/mapper/cl-root 17G 2.2G 15G 13% /data shm 64M 0 64M 0% /dev/shm root@c9358e2ce458:/# [root@hr-vm-10 data]# docker inspect -f {{.Mounts}} nginxv100 [{ /data/images /data true rprivate}]
--volumes-from
一個容器能夠訪問另一個容器的卷,適用於在多個容器中共享.
ps: 另一個容器卷若是被使用, 是沒法刪除的,當使用了數據卷容器.
[root@hr-vm-10 ~]# docker run -d --name nginxv101 --volumes-from nginxv100 nginx 4455e9ad2bc6947009e831b21923c06c85a32680aaafab74b7d4f71b003dcc51 [root@hr-vm-10 ~]# ./docker.sh nginxv101 mesg: ttyname failed: No such file or directory root@4455e9ad2bc6:/# cd /data/ root@4455e9ad2bc6:/data# ls hehe.jpg [root@hr-vm-10 ~]# docker inspect -f {{.Mounts}} nginxv101 [{ /data/images /data true rprivate}] [root@hr-vm-10 ~]#
啓動一個容器,進入容器裏安裝所須要的軟件.
docker run --name mynginx -it centos ./docker.sh mynginx yum -y install nginx echo 'deamon off;' >>/etc/nginx/nginx.conf
提交鏡像
[root@hr-vm-10 ~]# docker commit -m 'my nginx' 4916e87a3e1f wangfei/mynginx:v1 sha256:9a18e628ede9cb1245d1c144eb8ac389aa85c2f296d1b3366c858c094ab859ba [root@hr-vm-10 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE wangfei/mynginx v1 9a18e628ede9 26 seconds ago 381.6 MB docker.io/centos latest 8140d0c64310 7 weeks ago 192.5 MB docker.io/nginx latest 3448f27c273f 8 weeks ago 109.4 MB
目標: 用dockerfile構建一個nginx容器
[root@hr-vm-10 ~]# mkdir -p /opt/dockerfile/nginx/ # Description: build nginx container # Base images # 除註釋的第一行,必須是FROM From centos # Maintainer MAINTAINER fei.wang@foxmail.cn # Run commands RUN yum -y install wget RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo RUN yum -y install nginx RUN echo 'daemon off;'>>/etc/nginx/nginx.conf # EXPOSE EXPOSE 80 # CMD CMD ["nginx"]
ps: Dockerfile文件名,D必須大寫.
build構建
[root@hr-vm-10 ~]# docker build -t mynginx:v2 /opt/dockerfile/nginx/ [root@hr-vm-10 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mynginx v2 a03c3c8678c2 8 minutes ago 401.3 MB wangfei/mynginx v1 9a18e628ede9 44 minutes ago 381.6 MB docker.io/centos latest 8140d0c64310 7 weeks ago 192.5 MB docker.io/nginx latest 3448f27c273f 8 weeks ago 109.4 MB
用構建的鏡像啓動一個容器
[root@hr-vm-10 ~]# docker run -d --name Mynginx -p 18080:80 nginx
docker 鏡像文件是分層設計, 在編寫dockerfile文件時,將dockerfile按照層次細分紅多個組件, 而後將常常須要變更的放在下面.dockerfile當發生變更時,都會從新構建.
[root@hr-vm-10 ~]# mkdir -p /opt/docker/{runtime,app,system} [root@hr-vm-10 ~]# mkdir -p /opt/docker/runtime/{php,java,python} [root@hr-vm-10 ~]# mkdir -p /opt/docker/system/{centos,centos-ssh,ubuntu} [root@hr-vm-10 ~]# tree /opt/docker /opt/docker ├── app ├── runtime 運行環境層 │ ├── java │ ├── php │ └── python └── system 系統層 ├── centos ├── centos-ssh └── ubuntu 9 directories, 0 files
[root@hr-vm-10 centos]# ll total 8 -rw-r--r--. 1 root root 254 Jul 7 00:00 Dockerfile -rw-r--r--. 1 root root 1084 Jul 7 00:00 epel.repo [root@hr-vm-10 centos]# cat Dockerfile # Base images for centos # Base images FROM centos # Maintainer MAINTAINER fei.wang@qq.com # Copy file COPY epel.repo /etc/yum.repos.d/ #Base Pkg RUN yum clean all RUN yum -y install wget mysql-devel supervisor git redis tree net-tools sudo psmisc [root@hr-vm-10 centos]# docker build -t centos:v1 . [root@hr-vm-10 centos]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos v1 011d5c16b681 16 seconds ago 404.7 MB mynginx v2 a03c3c8678c2 4 hours ago 401.3 MB wangfei/mynginx v1 9a18e628ede9 5 hours ago 381.6 MB docker.io/centos latest 8140d0c64310 7 weeks ago 192.5 MB docker.io/nginx latest 3448f27c273f 8 weeks ago 109.4 MB
環境說明
管理程序:supervisord
應用環境:python
須要啓動的應用:python程序 ssh服務
系統環境: centos
技巧
1.先在測試環境將環境部署好,而後將其配置文件copy過來,再寫dockerfile文件
2.編寫dockfile文件的時候,將常常變更的操做放在下面(docker images 是分層設計的,上面一動,下面的就都得從新構建,速度很慢。)
目錄分層
[root@hr-vm-10 docker]# tree . ├── app │ └── shop-api │ ├── app.py │ ├── Dockerfile │ ├── requirements.txt │ ├── supervisord.conf │ └── supervisord.ini ├── runtime │ ├── java │ ├── php │ └── python └── system ├── centos │ ├── Dockerfile │ └── epel.repo ├── centos-ssh │ └── Dockerfile └── ubuntu 10 directories, 8 files
構建基礎鏡像的dockerfile文件
[root@hr-vm-10 dockeri]# cat system/centos/Dockerfile # Base images for centos # Base images FROM centos # Maintainer MAINTAINER fei.wang@qq.com # Copy file COPY epel.repo /etc/yum.repos.d/ #Base Pkg RUN yum clean all RUN yum -y install wget mysql-devel supervisor git redis tree net-tools sudo psmisc python-devel python-pip supervisor vim openssh-server openssh-clients openssh RUN pip install --upgrade pip [root@hr-vm-10 docker]# docker build -t centos:v3 system/centos/
生產環境dockerfile文件
[root@hr-vm-10 docker]# cat app/shop-api/Dockerfile # Description: shop-api # Base images FROM centos:v3 # Maintainer MAINTAINER fei.wang@hrfax.cn # Copy file COPY app.py /opt/ COPY requirements.txt /opt/ COPY supervisord.conf /etc/supervisord.conf COPY supervisord.ini /etc/supervisord.d/ # Add user RUN useradd -u 1000 -s /sbin/nologin www # Install pip dep RUN pip install -r /opt/requirements.txt # clear ssh cer RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key RUN echo "root:abc#123"|chpasswd # Expose EXPOSE 80 5000 # CMD CMD ["supervisord","-c","/etc/supervisord.conf"]
app python程序
[root@hr-vm-10 docker]# cat app/shop-api/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)
pip 安裝的軟件
[root@hr-vm-10 docker]# cat app/shop-api/requirements.txt flask
supervisord 管理的程序
[root@hr-vm-10 docker]# cat app/shop-api/supervisord.ini [program:shop-api] command=/usr/bin/python2.7 /opt/app.py process_name=%(program_name)s autostart=true user=www stdout_logfile=/tmp/api.log stderr_logfile=/tmp/api.error [program:sshd] command=/usr/sbin/sshd -D process_name=%(program_name)s autostart=true stderr_logfile=/tmp/ssh.error
supervisord配置文件
[root@hr-vm-10 docker]# grep daemon app/shop-api/supervisord.conf nodaemon=true ; (start in foreground if true;default false) ps: 關鍵配置.
進行構建生產環境鏡像
[root@hr-vm-10 docker]# docker build -t shopapi:v3 app/shop-api/ [root@hr-vm-10 docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myshopapi v3 609cd450ae28 30 minutes ago 447.3 MB shopapi v2 6781bc1869d6 About an hour ago 447.3 MB shopapi v1 b757b7e691a4 About an hour ago 447 MB centos v3 19aa069dff1a About an hour ago 441.4 MB centos v2 316718447fdc About an hour ago 433.2 MB centos v1 011d5c16b681 2 hours ago 404.7 MB mynginx v2 a03c3c8678c2 6 hours ago 401.3 MB wangfei/mynginx v1 9a18e628ede9 7 hours ago 381.6 MB docker.io/centos latest 8140d0c64310 7 weeks ago 192.5 MB docker.io/nginx latest 3448f27c273f 8 weeks ago 109.4 MB
運行這個容器
[root@hr-vm-10 docker]# dockr run --name myshopapi01 -d -p 8088:5000 -p 8022:22 shopapi:v3
[root@hr-vm-14-docker tomcat]# tree . ├── apache-tomcat-7.0.64.tar.gz ├── Dockerfile ├── jdk1.8.0_60.tar.gz ├── supervisord.conf └── supervisord.ini 0 directories, 5 files [root@hr-vm-14-docker tomcat]# cat Dockerfile # build tomcat # Base images FROM wf/centos:base # Maintainer MAINTAINER wangfei@hrfax.cn # Create New Diretory RUN mkdir -p /application/ # Add file ADD apache-tomcat-7.0.64.tar.gz /application/ # Add 的用法須要注意一下, 當是壓縮文件的時候,會自動解壓, 若是隻是單純的複製,用copy ADD jdk1.8.0_60.tar.gz /application/ ADD supervisord.ini /etc/supervisord.d/ ADD supervisord.conf /etc/supervisord.conf # Build tomcat and java # 這裏有一個坑,原先我使用寫到文件裏的方式,而後source,發現不能生效. ENV JAVA_HOME /application/jdk ENV CLASSPATH $CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /application/tomcat_8080 ENV PATH $CATALINA_HOME:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH RUN mv /application/apache-tomcat-7.0.64 /application/tomcat_8080 RUN useradd -s /sbin/nologin -M www RUN chown -R www.www /application/tomcat_8080 RUN ln -s /application/jdk1.8.0_60 /application/jdk # EXPOSE EXPOSE 8080 # volume VOLUME ["/root/ROOT","/application/tomcat_8080/webapps/ROOT/"] # CMD CMD ["/usr/bin/supervisord","-c","/etc/supervisord.conf"] [root@hr-vm-14-docker tomcat]# cat supervisord.ini [program:tomcat] # 這裏有一個梗,根據我查到的資料,tomcat放前臺啓動有二種方式,另一種是startup.sh && tailf /tomcat_homg/log/catilina.log command=/application/tomcat_8080/bin/catalina.sh run process_name=%(program_name)s autostart=true user=www # 日誌輸出很重要 stdout_logfile=/tmp/tomcat.log stderr_logfile=/tmp/tomcat.error
參考連接:
https://docs.docker.com/registry/#tldr
注意事項:
參考連接:
https://github.com/vmware/harbor
安裝文檔:
https://github.com/vmware/harbor/blob/master/docs/installation_guide.md
問題總結:
http://blog.csdn.net/jiangshouzhuang/article/details/53267094