容器生命周期管理 — docker [run|start|stop|restart|kill|rm|pause|unpause] 容器操做运维 — docker [ps|inspect|top|attach|events|logs|wait|export|port] 容器rootfs命令 — docker [commit|cp|diff] 镜像仓库 — docker [login|pull|push|search] 本地镜像管理 — docker [images|rmi|tag|build|history|save|import] 其余命令 — docker [info|version]
一、 yum -y remove docker 先删除以前的docker产品
yum install -y yum-utils 安装yum-config-manager管理工具
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 添加docker-ce的源。
yum -y install docker-ce 安装dockermysql
一、使用:
0一、启动:systemctl start docker
0二、docker pull ubuntu pull从网上下载一个镜像。
docker pull [OPTIONS] NAME[:TAG|@DIGEST] 使用方法
docker pull REGISRY_HOST:PORT/NAME[:TAG]linux
0三、docker run -t -i ubuntu /bin/bash docker的运行 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 使用语法。 -a stdin 指定标准输入输出内容类型,可选 STDIN/STDOUT / STDERR 三项; -d 后台运行容器,并返回容器ID; -i 以交互模式运行容器,一般与 -t 同时使用; -t 为容器从新分配一个伪输入终端,一般与 -i 同时使用; --name="nginx-lb" 为容器指定一个名称; --dns 8.8.8.8 指定容器使用的DNS服务器,默认和宿主一致; --dns-search example.com 指定容器DNS搜索域名,默认和宿主一致; -h "mars" 指定容器的hostname; -e username="ritchie" 设置环境变量; --env-file=[] 从指定文件读入环境变量; 0四、docker images 查看已下载镜像。 0五、docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] tag的使用方式,打标签。 docker tag ubuntu:latest ubuntu:lufei 0六、docker inspect 747cb2d60bbe 查看一个镜像的详细信息。 0七、docker search --no-trunc=false nginx 查找指定的镜像文件 --no-trunc=false 输出信息不截断显示。 0八、docker rmi nginx:123 删除指定镜像,名字:标记。 docker rmi 196e0ce0c9fb 用指定的Id来删除指定的镜像,可是必须是惟一的ID,不然会报错误。 -f:强制删除镜像。 0九、docker run ubuntu echo "ni hao" 运行时能够直接接命令。 0十、docker ps -a 查看已经运行的docker镜像。 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 021bd5c6baf7 ubuntu "echo 'ni hao'" 11 seconds ago Exited (0) 11 seconds ago suspicious_saha 94576603c522 ubuntu "/bin/bash" 43 hours ago Exited (127) 43 hours ago quirky_boyd c85ac6bcf9ee ubuntu "/bin/bash" 43 hours ago Exited (0) 43 hours ago lucid_torvalds 6764114b5ece 0十一、存出和载入镜像。 00一、导出镜像 docker save -o nginx-latest.tar nginx:latest 导出一个镜像。 [root@localhost ~]# ll 查看导出结果。 total 314756 -rw-r--r-- 1 root root 0 Oct 17 04:11 --add-repo -rw-r--r-- 1 root root 210291971 Nov 27 2016 centos-6-x86-minimal.tar.gz -rw------- 1 root root 112016896 Oct 19 00:27 nginx-latest.tar 00二、载入镜像 [root@localhost ~]# docker rmi nginx:latest 先删除以前导出的镜像。 Untagged: nginx:latest Untagged: nginx@sha256:004ac1d5e791e705f12a17c80d7bb1e8f7f01aa7dca7deee6e65a03465392072 Deleted: sha256:1e5ab59102ce46c277eda5ed77affaa4e3b06a59fe209fe0b05200606db3aa7a Deleted: sha256:182a54bd28aa918a440f7edd3066ea838825c3d6a08cc73858ba070dc4f27211 Deleted: sha256:a527b2a06e67cab4b15e0fd831882f9e6a6485c24f3c56f870ac550b81938771 Deleted: sha256:cec7521cdf36a6d4ad8f2e92e41e3ac1b6fa6e05be07fa53cc84a63503bc5700 [root@localhost ~]# docker images 查看是否已经删除。 REPOSITORY TAG IMAGE ID CREATED SIZE centos-6 lufei 4e05814cc000 6 minutes ago 512MB test latest f010d6ffbfea About an hour ago 122MB ubuntu latest 747cb2d60bbe 8 days ago 122MB ubuntu lufei 747cb2d60bbe 8 days ago 122MB centos latest 196e0ce0c9fb 4 weeks ago 197MB [root@localhost ~]# docker load --input nginx-latest.tar 载入镜像。方法一 cec7521cdf36: Loading layer [==================================================>] 58.44MB/58.44MB 453fc2d51e11: Loading layer [==================================================>] 53.56MB/53.56MB a1a53f8d99b5: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: nginx:latest docker load < nginx-latest.tar 导入镜像,方法2. 查看: [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-6 lufei 4e05814cc000 10 minutes ago 512MB test latest f010d6ffbfea About an hour ago 122MB ubuntu latest 747cb2d60bbe 8 days ago 122MB ubuntu lufei 747cb2d60bbe 8 days ago 122MB nginx latest 1e5ab59102ce 9 days ago 108MB centos latest 196e0ce0c9fb 4 weeks ago 197MB 建立一个镜像的三种方法: 一、基于已有镜像的容器建立: 0一、docker run -ti ubuntu /bin/bash 先运行一个镜像 Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu Digest: sha256:506e2d5852de1d7c90d538c5332bd3cc33b9cbd26f6ca653875899c505c82687 Status: Downloaded newer image for ubuntu:latest 0二、root@6764114b5ece:/# ls 在已运行的镜像中操做一些东西,而后退出。 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6764114b5ece:/# touch 123 root@6764114b5ece:/# vim 123 bash: vim: command not found root@6764114b5ece:/# vi 123 bash: vi: command not found root@6764114b5ece:/# echo "ni hao ">123 root@6764114b5ece:/# cat 123 ni hao root@6764114b5ece:/# exit 在退出前须要记住6764114b5ece ID号。 exit 0三、[root@localhost ~]# docker commit -m "lufei1" -a "xin de " 6764114b5ece test 建立镜像文件。 sha256:f010d6ffbfeae03bba5f32a497b6142ea66362f091633767775979389ee0a9e2 [root@localhost ~]# docker images 查看新建立的。 REPOSITORY TAG IMAGE ID CREATED SIZE test latest f010d6ffbfea 18 seconds ago 122MB ubuntu latest 747cb2d60bbe 8 days ago 122MB ubuntu lufei 747cb2d60bbe 8 days ago 122MB nginx 345 1e5ab59102ce 9 days ago 108MB nginx latest 1e5ab59102ce 9 days ago 108MB centos latest 196e0ce0c9fb 4 weeks ago 197MB 说明:-m:提交消息,-a: 做者信息, -p:提交时暂停容器运行。 二、基于本地模板导入一个镜像。 0一、wget http://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz 下载一个镜像模板 0二、cat centos-6-x86-minimal.tar.gz |docker import - centos-6:lufei 导入,centos-6:lufei为名字,导入命令:import - sha256:4e05814cc00015a9a2e919729fdbc5d3e8382e0ba5950333a3db023f2831310c 0三、查看: [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-6 lufei 4e05814cc000 About a minute ago 512MB test latest f010d6ffbfea 43 minutes ago 122MB ubuntu latest 747cb2d60bbe 8 days ago 122MB ubuntu lufei 747cb2d60bbe 8 days ago 122MB nginx 345 1e5ab59102ce 9 days ago 108MB nginx latest 1e5ab59102ce 9 days ago 108MB centos latest 196e0ce0c9fb 4 weeks ago 197MB
第四章:容器nginx
1、建立容器: 一、 docker create -it mysql:latest 建立一个容器,如今本地搜索,如有这一本地为模板来建立,若没有则会链接网络下载一个。 -t:分配一个伪终端,并绑定到容器的标准输入上。 -i:让容器的标准输入保持打开。 建立完成后查看 docker ps -a 查看其状态。 docker run -t -i ubuntu:latest /bin/bash 建立后运行。 exit 退出,而且终止程序运行。 二、守护状态运行: docker run -d -v /usr/local/chenxu/gdca/:/usr/local/tomcat/webapps/ -v /com/:/com/ --name gdca -p 803:8080 tomcat:7 -d:守护状态运行,后台运行 -v:指定挂载目录, 宿主机的目录:容器目录 -p:指定端口,宿主机端口:容器端口 --name 指定别名 2、终止容器 一、stop 0一、docker stop ID 在宿主机能够执行的方式 0二、exit 在容器内部退出容器时,也及终止 0三、ctrl+D 在容器内部退出容器时,也及终止 二、启动一个中止的容器 start:docker start ID 三、进入容器 0一、attach docker attach ID ID使用docker ps -a得到 0二、exec docker exec -it ID/名字 /bin/bash 0三、nsenter工具,此工具之后再作学习使用 yum -y install util-linux 先安装此工具 四、删除容器rm docker rm `docker ps -a -q` 删除全部不在运行的容器 docker rm -f `docker ps -a -q 删除全部容器。 docker rm `docker ps -a |grep Exited|awk '{print $1}'` 删除已经退出的容器 五、导入和导出容器 0一、导出:export [root@centos7-174 docker-rongqi]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 65c06cdfbb8c 196e0ce0c9fb "/bin/bash" 7 minutes ago Up 7 minutes hungry_darwin [root@centos7-174 docker-rongqi]# docker export 65c06cdfbb8c >centos_latest.tar [root@centos7-174 docker-rongqi]# ll 总用量 200408 -rw-r--r--. 1 root root 205216768 10月 31 09:24 centos_latest.tar 0二、导入:import [root@centos7-174 docker-rongqi]# cat centos_latest.tar |docker import - test/centos:v1.0 sha256:833545e673b27d8718f451f6597c1e86f64264643f951cdbc6a74157e0ef4010 [root@centos7-174 docker-rongqi]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/centos v1.0 833545e673b2 31 seconds ago 196.6 MB tomcat latest 71221ee31c0b 20 hours ago 454.3 MB docker.io/nginx latest c59f17fe53b0 3 days ago 108.4 MB docker.io/tomcat 7 e1ac7618b15d 2 weeks ago 454.3 MB docker.io/centos latest 196e0ce0c9fb 6 weeks ago 196.6 MB 第五章:仓库 5.一、docker hub 官方:https://hub.docker.com lufei0920 feilu0920@163.com lufei19810920 [root@centos7-174 docker-rongqi]# 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: lufei0920 Password: Login Succeeded docker search centos 搜索指定的镜像。 存放在 /var/lib/docker 5.二、建立私有仓库 docker run -d -p5000:5000 -v /opt/data/registry:/tmp/registry registry 建立私有仓库并在本地指定一个地址。 [root@centos7-173 ~]# docker tag centos:latest 192.168.1.173:5000/test 建立一个指定地址的镜像。 其格式:docker tag IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG] [root@centos7-174 docker-rongqi]# docker push 192.168.1.174:5000/test 上传镜像到本地仓库,报错。 The push refers to a repository [192.168.1.174:5000/test] Get https://192.168.1.174:5000/v1/_ping: http: server gave HTTP response to HTTPS client 修复以上报错方法: vim /etc/docker/daemon.json 在此文件中添加配置内容 { "bip": "192.168.0.1/24", 此行内容是修改默认docker0地址的方式。其要和下面的配置用,分割。 "insecure-registries": ["192.168.1.174:5000"] 此行内容是修复上传报错的问题。若是是其余服务器上的docker也须要配置此项。 } [root@centos7-175 ~]# docker pull 192.168.1.174:5000/tomcat-latest 从本身的私有仓库中下载镜像。 第六章:数据管理 6.一、数据卷 一、如何在容器中建立一个数据卷: 使用-v选项 docker run -d -v /usr/local/njxs:/usr/local/tomcat/webapps:ro -v /com:/com -p 8081:807 tomcat:njxs 说明:-d:后台运行 -v:挂载数据卷,可使用多个-v挂载多个数据卷 :ro/:rw:指定为只读或读写。 6.二、数据卷容器:--volumes-from [root@centos7-174 ~]# docker run -d -v /dbdata --name dbdata tomcat:latest 先建立一个数据卷容器 61f26198d4630b55c33b8d579fd8c97f06337d17cc229152bab9d74870c9b239 [root@centos7-174 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 61f26198d463 tomcat:latest "catalina.sh run" 4 seconds ago Up 3 seconds 8080/tcp dbdata db7946e42083 registry "/entrypoint.sh /etc/" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp hopeful_allen [root@centos7-174 ~]# docker run -d --volumes-from dbdata --name db1 tomcat:latest 将新建立的容器挂载到刚刚床架你的数据局卷容器上。 c3a6ac9b6f342a273d72f973aefce67974097f5e38134e124c10e0e279f3bd3f [root@centos7-174 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3a6ac9b6f34 tomcat:latest "catalina.sh run" 4 seconds ago Up 2 seconds 8080/tcp db1 61f26198d463 tomcat:latest "catalina.sh run" 43 seconds ago Up 42 seconds 8080/tcp dbdata db7946e42083 registry "/entrypoint.sh /etc/" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp hopeful_allen [root@centos7-174 ~]# docker exec -it 61f26198d463 /bin/bash 进入第一个容器的/dbdata目录建立一个文件test root@61f26198d463:/usr/local/tomcat# cd /dbdata/ root@61f26198d463:/dbdata# ls root@61f26198d463:/dbdata# touch test root@61f26198d463:/dbdata# [root@centos7-174 registry]# docker exec -it 61f26198d463 /bin/bash 再进入第二个容器的相应目录下,查看是否有刚建立的文件。 root@61f26198d463:/usr/local/tomcat# cd /dbdata/ root@61f26198d463:/dbdata# ls test 6.三、利用数据卷进行数据迁移: 第七章:网络: 7.一、端口映射实现访问容器。使用-p [root@centos7-174 ~]# docker run -d -p 808:8080 tomcat:latest 指定端口映射端口 3aa0c4ce060c945c1dacc976fb88895ece8101f4c5172928ee2e022427a4f152 [root@centos7-174 ~]# docker run -d -p 192.168.1.174:809:8080 tomcat:latest 指定宿主机固定IP的端口映射容器的端口 ab80291a2da3052aee3d4c7b587f742f9acac36b6bc586375d8093efe418ee6c [root@centos7-174 ~]# docker run -d -p 192.168.1.174::8080 tomcat:latest 指定宿主机固定IP的任意端口映射容器端口。 ce87dc5e1118ba67b02df59dbda1e97c581d0510bd304c6aa861084c9e8f8f4a [root@centos7-174 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce87dc5e1118 tomcat:latest "catalina.sh run" 7 seconds ago Up 6 seconds 192.168.1.174:32768->8080/tcp evil_mayer ab80291a2da3 tomcat:latest "catalina.sh run" 36 seconds ago Up 35 seconds 192.168.1.174:809->8080/tcp desperate_aryabhata 3aa0c4ce060c tomcat:latest "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:808->8080/tcp elated_cray db7946e42083 registry "/entrypoint.sh /etc/" 3 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp hopeful_allen [root@centos7-174 ~]# docker port ce87dc5e1118 查看指定容器映射的端口。 8080/tcp -> 192.168.1.174:32768