dockers
介绍
一、压缩包安装docker
1、上传压缩包
[root@localhost ~]# ll
total 4425304
-rw-------. 1 root root 1390 Nov 6 16:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 4329570304 Jun 7 2019 CentOS-7-x86_64-DVD-1511.iso
-rw-r--r--. 1 root root 201935496 Nov 6 17:53 Docker.tar.gz
2、解压到/opt/下
[root@localhost ~]# tar -zxvf Docker.tar.gz -C /opt
3、配置yum源
[root@localhost ~]# mv /etc/yum.repos.d/* /media/
[root@localhost ~]# mkdir /opt/centos
[root@localhost ~]# mount -o loop CentOS-7-x86_64-DVD-1511.iso /opt/centos
mount: /dev/loop0 is write-protected, mounting read-only
[root@localhost ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[docker]
name=docker
baseurl=file:///opt/Docker
gpgcheck=0
enabled=1
~
~
~
~
~
~
~
"/etc/yum.repos.d/local.repo" [New] 10L, 138C written
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: centos docker
Cleaning up everything
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
centos | 3.6 kB 00:00
docker | 2.9 kB 00:00
(1/3): centos/group_gz | 155 kB 00:00
(2/3): centos/primary_db | 2.8 MB 00:00
(3/3): docker/primary_db | 851 kB 00:00
Determining fastest mirrors
repo id repo name status
centos centos 3,723
docker docker 463
repolist: 4,186
4、安装docker
[root@localhost ~]# yum install docker-ce -y
5、开启,并设置开机自启
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
docker镜像管理
1、镜像的获取
(1)拉取镜像
首先,配置一下仓库文件,配置加速器,
[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://dhq9bx4f.mirror.aliyuncs.com"]
}
~
~
~
~
~
~
~
~
~
~
~
"/etc/docker/daemon.json" [New] 3L, 64C written
[root@localhost ~]# systemctl restart docker
拉取镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
(2)将tar包解压成镜像(需要本地有镜像tar包,我的就有跟多)
首先,看我有的镜像tar包
[root@localhost images]# ll
total 1239652
-rwxrwxrwx. 1 root root 1446400 Oct 31 2019 busybox_latest.tar
-rwxrwxrwx. 1 root root 227455488 Oct 31 2019 centos_latest.tar
-rwxrwxrwx. 1 root root 170482176 Oct 31 2019 httpd_latest.tar
-rwxrwxrwx. 1 root root 130066944 Oct 31 2019 nginx_latest.tar
-rwxrwxrwx. 1 root root 128326656 Oct 31 2019 nginx.tar
然后使用不同方法导成镜像
### docker load < tar包
[root@localhost images]# docker load < nginx_latest.tar
b67d19e65ef6: Loading layer 72.5MB/72.5MB
6eaad811af02: Loading layer 57.54MB/57.54MB
a89b8f05da3a: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
### docker load -i tar包 (-i 就是 -input 和 < 意思是一样的)
[root@localhost images]# docker load -i registry_latest.tar
d9ff549177a9: Loading layer 4.671MB/4.671MB
f641ef7a37ad: Loading layer 1.587MB/1.587MB
d5974ddb5a45: Loading layer 20.08MB/20.08MB
5bbc5831d696: Loading layer 3.584kB/3.584kB
73d61bf022fd: Loading layer 2.048kB/2.048kB
Loaded image: registry:latest
### cat tar 包 | docker import - 镜像名字
[root@localhost images]# cat busybox_latest.tar | docker import - busybox
sha256:6e0417bef529384eb8ca0bffb122d29a0aaa88882255d0d66973b997308ecd83
最后查看一下
[root@localhost images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
busybox latest 6e0417bef529 7 seconds ago 1.44MB
httpd latest d3017f59d5e2 12 months ago 165MB
nginx latest 540a289bab6c 12 months ago 126MB
镜像太多,删几个,留两个有用的
[root@localhost images]# docker rmi busybox httpd nginx
Untagged: busybox:latest
Deleted: sha256:6e0417bef529384eb8ca0bffb122d29a0aaa88882255d0d66973b997308ecd83
Deleted: sha256:13ffd3d57b5d03f3f3b56b015424ee136ae623d8a260b1fb6d43c57da6e20bf9
Untagged: httpd:latest
Deleted: sha256:d3017f59d5e25daba517ac35eaf4b862dce70d2af5f27bf40bef5f936c8b2e1f
Deleted: sha256:c015bdd664253fc2ccdff3a425ba085e94b99ce801d6c9c5219ffaad279362b1
Deleted: sha256:c79505e64684e42a92353a6e3430969d8a801f327d24bdde11bd99d41cbef2a0
Deleted: sha256:87158971c3545200bc870118cfa31bf5470204eea10da0b79531388b5f91cea2
Deleted: sha256:0105db3a8b98aea80771956b067b64a26bd630718141bacd6feafdc5b5c0caee
Untagged: nginx:latest
Deleted: sha256:540a289bab6cb1bf880086a9b803cf0c4cefe38cbb5cdefa199b69614525199f
Deleted: sha256:ab18af7cee69bfb22c1771e54d5e0e68b1a1bf57bb46516142da0380b1771f4a
Deleted: sha256:02f7daf1e14541cd61a3dda1a61cc0f78fee8de2984d488b8ba5bbd3cbad9b57
Deleted: sha256:b67d19e65ef653823ed62a5835399c610a40e8205c16f839c5cc567954fcf594
[root@localhost images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
(3)本地仓库上传镜像后拉取
首先要创建一个本地仓库
[root@localhost ~]# docker run -itd -p 5000:5000 registry
52cc7ff29bb9d8230c01cc3f5d696a1a5c05bea19b690fd5e8cabbb8611c9eb6
[root@localhost ~]# curl http://192.168.200.70:5000/v2/_catalog
{"repositories":[]}
[root@localhost ~]# docker tag centos:latest 192.168.200.70:5000/centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.70:5000/centos latest 300e315adb2f Less than a second ago 209MB
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
[root@localhost ~]# vi /etc/docker/daemon.json
{
"insecure-registries":["192.168.200.70:5000"]
}
~
~
~
~
~
~
~
[root@localhost ~]# systemctl restart docker
[root@localhost ~]#
上传镜像并删除那个镜像
[root@localhost ~]# docker push 192.168.200.70:5000/centos
The push refers to repository [192.168.200.70:5000/centos]
2653d992f4ef: Pushed
latest: digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 size: 529
[root@localhost ~]# docker rmi 192.168.200.70:5000/centos
Untagged: 192.168.200.70:5000/centos:latest
Untagged: 192.168.200.70:5000/centos@sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
然后从本地仓库中拉取
## 先看一下本地仓库
[root@localhost ~]# curl http://192.168.200.70:5000/v2/_catalog
{"repositories":["centos"]}
### 拉取
[root@localhost ~]# docker pull 192.168.200.70:5000/centos
Using default tag: latest
latest: Pulling from centos
Digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875
Status: Downloaded newer image for 192.168.200.70:5000/centos:latest
## 查看
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.70:5000/centos latest 300e315adb2f Less than a second ago 209MB
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
2、管理镜像
(1)删除镜像
[root@localhost ~]# docker rmi 192.168.200.70:5000/centos
Untagged: 192.168.200.70:5000/centos:latest
(2)查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
(3)给镜像打标签
[root@localhost ~]# docker tag centos:latest 192.168.200.70:5000/centos
(4)运行镜像
[root@localhost ~]# docker run -itd centos
bf418b34cdbc95e7fbf82db40272d86fd27fd908db6ed283ff0bccf4291ab796
三、容器管理
1、进入已经启动的容器
2、创建容器
[root@localhost ~]# docker run -itd --name registry -p5000:5000 registry:latest
be56cb15e2c780eee4bc63e084dc8955585ed6c12a870407b07f8ef7e0fa0661
3、查看容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7adfb6a0d69b centos "/bin/bash" 2 minutes ago Up 2 minutes epic_jones
bf418b34cdbc centos "/bin/bash" 15 minutes ago Up 15 minutes heuristic_lamarr
52cc7ff29bb9 registry "/entrypoint.sh /etc…" 5 hours ago Exited (2) 5 hours ago admiring_ptolemy
4、删除容器
删除一个
[root@localhost ~]# docker rm -f 7adfb6a0d69b
7adfb6a0d69b
全部删除
[root@localhost ~]# docker rm -f $(docker ps -qa)
bf418b34cdbc
52cc7ff29bb9
5、开启容器
[root@localhost ~]# docker restart be56cb15e2c7
be56cb15e2c7
[root@localhost ~]# docker push 192.168.200.70:5000/centos
The push refers to repository [192.168.200.70:5000/centos]
2653d992f4ef: Pushed
latest: digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 size: 529
6、将容器打包成容器
7、不进入容器直接执行命令
[root@localhost ~]# docker exec -it ad720ce49c80 ls
bin dev home lib64 media opt root sbin sys usr
data etc lib lost+found mnt proc run srv tmp var
failed to resize tty, using default size