Docker 容器云学习

1.安装
下载rpm安装包上传安装
docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm(依赖包)
先安装依赖包

[root@localhost ~]# yum install docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm -y

再安装docker

[root@localhost ~]# yum install  docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm

启动docker

[root@localhost ~]# systemctl start docker

查看docker的基本信息:docker info

[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.03.0-ce
Storage Driver: overlay
 Backing Filesystem: xfs
 Supports d_type: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.782 GiB
Name: localhost.localdomain
ID: XYR4:V4RU:VXMH:XM27:UETC:J3YD:GRDQ:MALU:JOIV:OGXE:S6PF:2HFO
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

配置加速器文件:vi /etc/docker/daemon.json

[root@localhost ~]# vi /etc/docker/daemon.json


{
"registry-mirrors":["https://dhp9bx4f.mirror.aliyuncs.com"]
}

配置完成后重启

[root@localhost ~]# systemctl restart docker

拉取镜像

[root@localhost ~]# docker pull centos

镜像列表查看

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        3 months ago        215 MB

搜索镜像

[root@localhost ~]# docker search centos
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                             The official build of CentOS.                   6300      [OK]       
ansible/centos7-ansible            Ansible on Centos7                              132                  [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   123                  [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos ...   116                  [OK]
centos/systemd                     systemd enabled base container.                 87                   [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   86                   
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                   [OK]
tutum/centos                       Simple CentOS docker image with SSH access      46                   
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relationa...   45                   
kinogmt/centos-ssh                 CentOS with SSH                                 29                   [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   13                   
guyton/centos6                     From official centos6 container with full ...   10                   [OK]
centos/tools                       Docker image that has systems administrati...   7                    [OK]
drecom/centos-ruby                 centos ruby                                     6                    [OK]
pivotaldata/centos                 Base centos, freshened up a little with a ...   5                    
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   3                    
darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   3                    
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                    [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developm...   1                    [OK]
mcnaughton/centos-base             centos base image                               1                    [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                    
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                    
smartentry/centos                  centos with smartentry                          0                    [OK]

给镜像打标签

[root@localhost ~]# docker tag 0d120b6ccaa8  aaa:v1

删除镜像

[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd

把镜像启动为容器(此处可以用名字是因为centos为最新镜像)

[root@localhost ~]# docker run -itd centos
8065a2ccd406dc1dd23a6e9c27ca6020ab8098149f8e616bf7eb3f5f547f0d0e

查看所有的容器

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
8065a2ccd406        centos              "/bin/bash"         About a minute ago   Up About a minute                       wonderful_pasteur

通过容器创建镜像
1.启动容器

[root@localhost ~]# docker run -itd centos
190e39a0c7372334bd4feae0ce3aa53147d4ad4fd967795de7a8247a9b199013

2进入容器

[root@localhost ~]# docker exec -it 8065a2ccd406 /bin/bash
[root@8065a2ccd406 /]# 

安装net-tools工具
[root@8065a2ccd406 /]# yum install -y net-tools
ctrl d 退出容器
创建了一个新的镜像

[root@localhost ~]# docker commit -m "install net-tools" -a "liu" 8065a2ccd406 centos
sha256:61bc8d40c25c4fbbcb31184a96d179dd524750c73873e5959070571ae9e8d947

使用模板创建镜像
1.下载模板
https://download.openvz.org/template/precreated/
2.导入该镜像

[root@localhost ~]# cat centos-7-x86_64-minimal\ \(1\).tar.gz |docker import - centos777
sha256:665e062d661c3d278f6cf30e01c4da01cb567558203a0487a621bee99bc2d460

3.查看导入的镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos777           latest              665e062d661c        About a minute ago   435 MB
aaa                 v1                  0d120b6ccaa8        3 months ago         215 MB
centos              latest              0d120b6ccaa8        3 months ago         215 MB

启动镜像

[root@localhost ~]# docker run -itd 665e062d661c /bin/bash
e398c8efae893820c8fb96f42271640aa35318d4f3940403e487b97a44c05a68

4.把现有的镜像,打包为一个tar包

[root@localhost ~]# docker save -o liu.tar 665e062d661c
[root@localhost ~]# 

查看

[root@localhost ~]# ll
total 596996
-rw-------. 1 root root      1419 Nov 26 01:51 anaconda-ks.cfg
-rw-r--r--. 1 root root 145639219 Dec  3  2020 centos-7-x86_64-minimal (1).tar.gz
-rw-r--r--. 1 root root  19521288 Dec  2  2020 docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root     29108 Dec  2  2020 docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
-rw-------. 1 root root 446123008 Nov 26 03:03 liu.tar

把tar包恢复为镜像
1.删除容器

[root@localhost ~]# docker rm -f $(docker ps -qa)
d730c1c97304
0802ab26894b
e398c8efae89
8065a2ccd406

2.删除镜像(转换成tar包的镜像)

[root@localhost ~]# docker rmi centos777
Untagged: centos777:latest
Deleted: sha256:665e062d661c3d278f6cf30e01c4da01cb567558203a0487a621bee99bc2d460
Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1

3.恢复镜像

[root@localhost ~]# docker load < liu.tar 
Loaded image ID: sha256:665e062d661c3d278f6cf30e01c4da01cb567558203a0487a621bee99bc2d460

4.查看

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              665e062d661c        55 minutes ago      435 MB
aaa                 v1                  0d120b6ccaa8        3 months ago        215 MB
centos              latest              0d120b6ccaa8        3 months ago        215 MB

打标签

[root@localhost ~]# docker tag 0d120b6ccaa8   bbb:v8
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              665e062d661c        About an hour ago   435 MB
bbb                 v8                  0d120b6ccaa8        3 months ago        215 MB
centos              latest              0d120b6ccaa8        3 months ago        215 MB
aaa                 v1                  0d120b6ccaa8        3 months ago        215 MB

Docker容器管理
创建容器

[root@localhost ~]# docker create -it centos /bin/bash
065cf4eff4035078abb8b6f03e4de93f56c617966c885a74c9ad09c973015f7e

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
065cf4eff403 centos “/bin/bash” 39 seconds ago zen_saha

创建容器

[root@localhost ~]# docker create -it centos /bin/bash
187d8415f059042e218dcffa2d6e31426bba6803f1aeb955f38ef73d43c8947f

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
187d8415f059 centos “/bin/bash” 20 seconds ago Created epic_carson
启动容器

[root@localhost ~]# docker start 187d8415f059
187d8415f059

查看,此时只有一个

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
187d8415f059        centos              "/bin/bash"         55 seconds ago      Up 17 seconds                           epic_carson

运行后查看,新增一个

[root@localhost ~]# docker run -it centos /bin/bash
[root@e71d45b5bff7 /]# exit
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
e71d45b5bff7        centos              "/bin/bash"         16 seconds ago      Exited (0) 9 seconds ago                       quirky_meninsky
187d8415f059        centos              "/bin/bash"         4 minutes ago       Up 3 minutes                                   epic_carson

在后台运行:docker run -d

[root@localhost ~]# docker run -d centos bash -c "while:;do echo"123";sleep 2;done"
f73d9db440ef9d61583c7864abb4e578b0bb23d53393976cf39e64ec14645051

可以用docker attach 加id 查看运行是否正常
[root@localhost ~]# docker attach 770d2576d154
123
123
123
123
给容器定义名字

[root@localhost ~]# docker run --name liu -itd centos /bin/bash
a9a04b4e944c010d0dab3867f9923d7961e700158a6cc5362fda374c93b66826
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
a9a04b4e944c        centos              "/bin/bash"              8 seconds ago       Up 8 seconds                                    liu
f73d9db440ef        centos              "bash -c 'while:;d..."   14 minutes ago      Exited (1) 14 minutes ago                       kind_wilson
e71d45b5bff7        centos              "/bin/bash"              19 minutes ago      Exited (0) 19 minutes ago                       quirky_meninsky
187d8415f059        centos              "/bin/bash"              24 minutes ago      Up 23 minutes                                   epic_carson

让容器退出后直接删除,命令执行完容器就会退出

[root@localhost ~]# docker run --rm -it centos bash -c "sleep 30"

Docker 容器管理
docker logs 可以获取到容器的运行历史消息

[root@localhost ~]# docker logs e71d45b5bff7
[root@e71d45b5bff7 /]# exit

进入后台运行的容器
[root@localhost ~]# docker attach 187d8415f059
[root@187d8415f059 /]# exit
exit
Docker仓库管理
下载registry镜像

[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete 
47112e65547d: Pull complete 
46bcb632e506: Pull complete 
c1cc712bcecd: Pull complete 
3db6272dcbfa: Pull complete 
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
[root@localhost ~]# docker run -d -p5000:5000 registry
beea320e7836e844db5c7f1f697d284e07aa0a681fa87b261fde02b77b886b2d

把镜像上传到私有仓库

[root@localhost ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}

[root@localhost ~]# docker tag wang:v1 192.168.200.40:5000/centos

把标记的镜像推送到私有仓库
[root@localhost ~]# docker push 192.168.200.40:5000/centos
The push refers to a repository [192.168.200.40:5000/centos]
Get https://192.168.200.40:5000/v1/_ping: http: server gave HTTP response to HTTPS client

更改配置文件

[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://dhp9bx4f.mirror.aliyuncs.com"],"insecure-registries":["192.168.200.40:5000"
]

重新启动docker

[root@localhost ~]# systemctl restart docker

查看发现所有容器已经关闭

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
beea320e7836        registry            "/entrypoint.sh /e..."   11 minutes ago      Exited (2) 18 seconds ago                       confident_lovelace
a9a04b4e944c        centos              "/bin/bash"              21 minutes ago      Exited (0) 18 seconds ago                       liu
f73d9db440ef        centos              "bash -c 'while:;d..."   36 minutes ago      Exited (1) 36 minutes ago                       kind_wilson
e71d45b5bff7        centos              "/bin/bash"              41 minutes ago      Exited (0) 41 minutes ago                       quirky_meninsky
187d8415f059        centos              "/bin/bash"              45 minutes ago      Exited (0) 16 minutes ago                       epic_carson

启动registry容器

[root@localhost ~]# docker start beea320e7836
beea320e7836

再次push

[root@localhost ~]# docker push 192.168.200.40:5000/centos
The push refers to a repository [192.168.200.40:5000/centos]
291f6e44771a: Pushed 
latest: digest: sha256:bde81ad8665d5e8f10aa4c91f28eb2e663c1a31f0cc851f6637003863253d168 size: 529

可以查看到推送上来的镜像

[root@localhost ~]# curl 192.168.200.40:5000/v2/_catalog
{"repositories":["centos"]}

容器导为tar包让其他虚拟机使用
主节点
容器导为tar包

[root@localhost ~]# docker export 523534bc7a54   >heihei.tar

复制到需要使用的虚拟机

[root@localhost ~]# scp heihei.tar [email protected]:/root
The authenticity of host '192.168.200.60 (192.168.200.60)' can't be established.
ECDSA key fingerprint is 2e:b0:87:50:15:73:34:e3:f1:3a:be:a4:c7:f8:56:8e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.60' (ECDSA) to the list of known hosts.
[email protected]'s password: 
heihei.tar                                                            100%  212MB 106.0MB/s   00:02 

从节点安装docker
启动docker

[root@localhost ~]# systemctl start docker

查看tar
包是否复制成功

[root@localhost ~]# ll
total 236248
-rw-------. 1 root root      1120 Nov 27 01:25 anaconda-ks.cfg
-rw-r--r--. 1 root root  19521288 Dec  2 10:23 docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root     29108 Dec  2 10:20 docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
-rw-r--r--. 1 root root 222356480 Dec  4 00:21 heihei.ta

生成heihei镜像

[root@localhost ~]# cat heihei.tar | docker import - centos
sha256:8a8ab518153fa6c5b4940a64c6560a826eb4c54c1c169c66b0bfbf45154025b8
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              8a8ab518153f        9 seconds ago       215 MB
aaa                 v1                  0d120b6ccaa8        3 months ago        215 MB
centos              <none>              0d120b6ccaa8        3 months ago        215 MB
[root@localhost ~]# docker run -itd --name ccc centos /bin/bash
69efbec0873b3e7481a038f5360c45133c3c428fd75a4a5851f4c53d14923af3
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
69efbec0873b        centos              "/bin/bash"         7 seconds ago       Up 6 seconds                                     ccc
6f3e7decee80        aaa:v1              "/bin/bash"         6 days ago          Exited (255) 2 minutes ago                       stupefied_jennings

你可能感兴趣的:(Docker 容器云学习)