1docker安装
#1查询安装过的docker
[root@localhost ~]# yum list installed | grep docker
docker.x86_64 2:1.13.1-203.git0be3e21.el7.centos
docker-client.x86_64 2:1.13.1-203.git0be3e21.el7.centos
docker-common.x86_64 2:1.13.1-203.git0be3e21.el7.centos
#2.删除已经安装的docker
[root@localhost ~]# yum remove docker.x86_64
[root@localhost ~]# yum remove docker-client.x86_64
[root@localhost ~]# yum remove docker-common.x86_64
#3.安装docker,(此命令安装的不是最新版)
[root@localhost ~]# yum install docker
#4.安装最新版docker
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install docker-ce
#查看版本
[root@localhost yum.repos.d]# docker version
#5.启动docker
[root@localhost ~]# service docker start
#6.设置开机启动
[root@localhost ~]# chkconfig docker on
#7.测试
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.
#为docker设置国内阿里云的镜像加速器;
修改文件 /etc/docker/daemon.json
#添加
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
#重启docker服务
[root@localhost docker]# systemctl restart docker
2.镜像操作
#1.列出本地镜像列表
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 11 months ago 13.3kB
#2.删除镜像(删除镜像前,要先删除容器)
[root@localhost docker]# docker rmi bf756fb1ae65
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
#3.查看镜像历史
[root@localhost docker]# docker history centos
IMAGE CREATED CREATED BY SIZE COMMENT
300e315adb2f 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
#4.使用docker info 查看docker根目录
Docker Root Dir: /var/lib/docker
#查看镜像信息
view /var/lib/docker/image/overlay2/imagedb/content/sha256/300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
3.容器操作
#1.拉取镜像
[root@localhost docker]# docker pull centos
#2.启动容器,并进入容器内部交互
[root@localhost docker]# docker run -it --name cent centos:latest /bin/bash
[root@49f7be7eed56 /]#
#3.ctrl + p + q 退出容器,但不会关闭容器
#3.1退出后如何再进去
#3.1.1
[root@localhost _data]# docker attach 302e2f97b7a2
#列出当前正在运行的容器
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49f7be7eed56 centos:latest "/bin/bash" 2 minutes ago Up 2 minutes cent
#4.关闭容器
[root@localhost docker]# docker stop cent
#5.查看所有容器
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49f7be7eed56 centos:latest "/bin/bash" 5 minutes ago Exited (127) About a minute ago cent
17564814dafb hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago affectionate_proskuriakova
#6.查看容器信息 docker inspect id(或者name)
[root@localhost docker]# docker inspect 49f7be7eed56
#7.删除容器
[root@localhost docker]# docker rm cent
cent
#8.后台运行容器
[root@localhost docker]# docker run -dit --name cent centos
f66e70c81719d5474210ef49d3fe979d9f64f177f6121d0a5a6cdea5578660b6
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f66e70c81719 centos "/bin/bash" 13 seconds ago Up 12 seconds cent
#进入正在运行的容器
[root@localhost docker]# docker exec -it f66e70c81719 /bin/bash
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-p: 端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="nginx-lb": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
-m :设置容器使用内存最大值;
--net="bridge": 网络连接类型,支持 bridge/host/none/container: 四种类型;
--link=[]: 添加链接到另一个容器;
--expose=[]: 开放一个端口或一组端口;
4.tomcat
#拉取镜像
[root@localhost ~]# docker pull tomcat
#运行容器
[root@localhost ~]# docker run -dit --name tomcat -p 8080:8080 tomcat
#访问报错404,进入tomcat
[root@localhost ~]# docker exec -it b8c6a34503c6 /bin/bash
#把webapps删掉将webapps.dist重命名为webapps,再次访问
5.仓库操作
#1.创建私有仓库
[root@localhost /]# docker run -d --name reg -p 5000:5000 registry
#2.查看仓库中有哪些镜像
[root@localhost /]# curl http://192.168.52.3:5000/v2/_catalog
{"repositories":[]}
#3.为某个镜像打个tag
[root@localhost /]# docker tag hello-world 192.168.52.3:5000/hello:1.0
[root@localhost /]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest feba8d001e3f 6 days ago 649MB
registry latest 678dfa38fcfa 7 days ago 26.2MB
centos latest 300e315adb2f 2 weeks ago 209MB
192.168.52.3:5000/hello 1.0 bf756fb1ae65 11 months ago 13.3kB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
#4.推送到私有仓库
[root@localhost /]# docker push 192.168.52.3:5000/hello
Using default tag: latest
The push refers to repository [192.168.52.3:5000/hello]
Get https://192.168.52.3:5000/v2/: http: server gave HTTP response to HTTPS client
#出错,修改/etc/docker/daemon.json
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"],
"insecure-registries":["192.168.52.3:5000"]
}
#重启
[root@localhost docker]# systemctl restart docker
[root@localhost docker]# docker push 192.168.52.3:5000/hello:1.0
The push refers to repository [192.168.52.3:5000/hello]
9c27e219663c: Pushed
1.0: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525
#此时查看镜像
[root@localhost docker]# curl http://192.168.52.3:5000/v2/_catalog
{"repositories":["hello"]}
#查询镜像版本
[root@localhost docker]# curl http://192.168.52.3:5000/v2/hello/tags/list
{"name":"hello","tags":["1.0"]}
6.数据挂载
#1.运行centos容器,并挂载文件
[root@localhost docker]# docker run --name data -v /opt/data -it centos /bin/bash
[root@302e2f97b7a2 /]# cd /opt/data
[root@302e2f97b7a2 data]# echo 123 > 123.txt
#2.查看容器挂载信息
[root@localhost docker]# docker inspect data
"Mounts": [
{
"Type": "volume",
"Name": "acbf3c009446cad54f692e8190323905a5942f9b95e765a09bd3cf8305489dd9",
"Source": "/var/lib/docker/volumes/acbf3c009446cad54f692e8190323905a5942f9b95e765a09bd3cf8305489dd9/_data",
"Destination": "/opt/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
[root@localhost docker]# cd /var/lib/docker/volumes/acbf3c009446cad54f692e8190323905a5942f9b95e765a09bd3cf8305489dd9/_data
[root@localhost _data]# ls
123.txt
[root@localhost _data]# cat 123.txt
123
#3.此时在宿主机添加一个文件,容器内也会添加响应的文件
[root@localhost _data]# pwd
/var/lib/docker/volumes/acbf3c009446cad54f692e8190323905a5942f9b95e765a09bd3cf8305489dd9/_data
[root@localhost _data]# echo 456 > 456.txt
[root@localhost _data]# ls
123.txt 456.txt
[root@localhost _data]#
[root@302e2f97b7a2 data]# ls
123.txt 456.txt
#也就是宿主机和容器共享文件
7.制作镜像
#1.运行一个centos容器
[root@localhost ~]# docker run -it --name cent centos /bin/bash
#安装nginx
[root@8faf5d1a0bfa /]# yum install nginx -y
#启动nginx
[root@8faf5d1a0bfa /]# /usr/sbin/nginx
#访问nginx
[root@localhost ~]# curl 172.17.0.2
#提交镜像
[root@localhost ~]# docker commit cent cent-ng:v1
sha256:84176f3226486398bee0462185719d2326a95640566d057add453df42c3870b6
[root@localhost ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
cent-ng v1 84176f322648 6 seconds ago 285MB
tomcat latest feba8d001e3f 6 days ago 649MB
registry latest 678dfa38fcfa 7 days ago 26.2MB
centos latest 300e315adb2f 2 weeks ago 209MB
192.168.52.3:5000/hello 1.0 bf756fb1ae65 11 months ago 13.3kB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
#运行容器
[root@localhost ~]# docker run -it --name cent-ngx1 cent-ng:v1 /bin/bash
[root@e4fb003b1b90 /]#
#但此时容器内的nginx是没有启动的
#运行容器并启动nginx
[root@localhost ~]# docker run -it --name cent-ngx3 cent-ng:v1 /usr/sbin/nginx -g "daemon off;"
网友评论