Docker和传统的虚拟化比较


Docker核心概念
Docker安装
[root@harry-02 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --: 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --: 0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --: 0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:100 2424 100 2424 0 0 706 0 0:00:03 0:00:03 --:--:-- 706
[root@harry-02 ~]# yum -y install docker-ce
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.cn99.com
* extras: mirrors.163.com
* updates: mirrors.cn99.com
base | 3.6 kB 00:00
docker-ce-stable | 3.5 kB 00:00
extras | 3.4 kB 00:00
systemctl start docker 启动docker
[root@harry-02 ~]# systemctl start docker
[root@harry-02 ~]# ps aux | grep docker
root 5409 3.6 2.8 526696 52984 ? Ssl 13:45 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 5544 0.0 0.0 112724 988 pts/0 R+ 13:45 0:00 grep --color=auto docker
Docker镜像管理
docker pull centos//可以下载centos镜像,速度很慢
vi /etc/docker/daemon.json//加入如下内容
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
配置完加速器,重启docker服务,再次docker pull centos会快很多

[root@harry-02 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5276 [OK]
ansible/centos7-ansible Ansible on Centos7 121 [OK]

docker run -itd centos //把镜像启动为容器,要把-i -t -d 放到镜像名字前面
-i表示让容器的标准输入打开
-t表示分配一个伪终端
-d表示后台启动
docker ps //查看运行的容器,加上-a选项后可以查看所有容器,包括未运行的

Docker通过容器创建镜像

[root@a28ffe1e480b /]# yum -y install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
base
[root@harry-02 ~]# docker commit -m "install net-tools" -a "harry" a28ffe1e480b centos_with_net
sha256:2dffdc73293c935715ae883ede14cb92203897d8e8748263c04c34a5c0ec3ac2
[root@harry-02 ~]#

Docker使用模板创建镜像

[root@harry-02 ~]# cat centos-6-x86-minimal.tar.gz | docker import - centos6
sha256:6102cc73679c9c23230e27338b1591b30cf80fea2e9536d945baa3a8dabd2547
docker images查看导入的镜像


[root@harry-02 ~]# docker load < harry-centos.tar
b7473463b7f7: Loading layer [==================================================>] 83.71MB/83.71MB
Loaded image: centos_with_net:latest
[root@harry-02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 6102cc73679c About an hour ago 512MB
centos_with_net latest 2dffdc73293c About an hour ago 285MB
harry_123 latest 9f38484d220f 2 weeks ago 202MB
docker push image_name //可以把自己的镜像传到dockerhub官方网站上去,但前提是需要先注册一个用户
Docker容器管理
[root@harry-02 ~]# docker create -it centos6 bash
5934aa43bd19f4a8426e6a9ceb61916125f89168a59b3a3626ea8355da7d7983
docker start container_id //启动容器后,可以使用 docker ps -a 查看到,有start 就有stop,和restart
[root@harry-02 ~]# docker start 292061ee146e
[root@harry-02 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@harry-02 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
292061ee146e centos6 "bash" 5 seconds ago Created kind_poitras
[root@harry-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
292061ee146e centos6 "bash" 2 minutes ago Up 5 seconds kind_poitras
[root@harry-02 ~]#
docker run -it centos bash
[root@harry-02 ~]# docker run -it centos6 bash
[root@8dec1213fb55 /]# ls
bin boot dev etc fastboot home lib lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@8dec1213fb55 /]#
docker run -d 可以让容器在后台运行
[root@harry-02 ~]# docker run -d centos6 bash -c "while:; do echo "123"; sleep1; done"
81e29271fb8cb06cc95ecac6264b67b05ec5e151e5fcc581398e6abced12063c
docker run --name web -itd centos bash // --name 给容器自定义名字
docker run --rm -it centos bash -c "sleep 30" //--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出
docker logs 可以获取到容器的运行历史信息,用法如下
[root@harry-02 ~]# docker run -itd centos6 bash -c "echo 123"
18c29e6616556534cf9224f8653d1483079a2ee4e5c28c41c5086907a708fb21
[root@harry-02 ~]# docker logs 18c29e66165
123
[root@harry-02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6 latest 6102cc73679c 2 hours ago 512MB
centos_with_net latest 2dffdc73293c 2 hours ago 285MB
harry_123 latest 9f38484d220f 2 weeks ago 202MB
[root@harry-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87888b29f162 centos6 "bash" 28 minutes ago Up 10 minutes web
a28ffe1e480b 9f38484d220f "/bin/bash" 2 hours ago Up 2 hours kind_sinoussi
[root@harry-02 ~]# docker exec -it 87888b29f162 bash
[root@87888b29f162 /]#



Docker仓库管理


[root@harry-02 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}
下面我们来把其中一个镜像上传到私有仓库
docker tag centos6 192.168.31.138:5000/centos6 //标记一下tag,必须要带有私有仓库的ip:port
[root@harry-02 ~]# docker tag centos6 192.168.31.138:5000/centos6
[root@harry-02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harry_test latest a6638884e17a 23 minutes ago 512MB
<none> <none> 69b6f25ec7fa 25 minutes ago 0B
192.168.31.138:5000/centos6 latest 6102cc73679c 2 hours ago 512MB
centos6 latest 6102cc73679c 2 hours ago 512MB
centos_with_net latest 2dffdc73293c 3 hours ago 285MB
harry_123 latest 9f38484d220f 2 weeks ago 202MB
registry latest f32a97de94e1 3 weeks ago 25.8MB
[root@harry-02 ~]#
[root@harry-02 ~]# docker push 192.168.31.138:5000/centos6
The push refers to repository [192.168.31.138:5000/centos6]
Get https://192.168.31.138:5000/v2/: http: server gave HTTP response to HTTPS client
此时并不会成功,Get https://172.7.15.113:5000/v2/: http: server gave HTTP response to HTTPS client
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
{ "insecure-registries":["192.168.31.138:5000"] }
systemctl restart docker
docker ps -a //查看容器已经关闭,还需要启动
[root@harry-02 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c06361a1141a registry "/entrypoint.sh /etc…" 18 minutes ago Exited (2) 20 seconds ago cranky_nash
292061ee146e centos6 "bash" 42 minutes ago Exited (137) 10 seconds ago kind_poitras
[root@harry-02 ~]# docker start c06361a1141a
c06361a1141a
[root@harry-02 ~]# docker start 292061ee146e
292061ee146e
[root@harry-02 ~]#

[root@harry-02 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos6"]}
docker pull 192.168.31.138:5000/centos6 //从私有仓库中拉取镜像
Docker数据管理
[root@harry-02 ~]# docker run -itd -v /data/:/data centos6 bash
9e654c194234dd7d88d3940390a43b0c568b7a765ef125b50e318448d93cfe50
[root@harry-02 ~]# docker exec -it 9e654c1 bash
[root@9e654c194234 /]# ls -l /data/
total 280
drwxr-xr-x 7 root root 76 Mar 19 10:46 mongodb
drwxr-xr-x. 8 1001 1001 224 Mar 29 23:55 mysql
drwxr-xr-x 5 root root 80 Mar 8 21:59 redis_data
drwxr-xr-x 2 root root 44 Mar 8 10:38 redis_data2
drwxr-xr-x. 7 root root 85 Dec 25 05:37 wwwroot
drwxr-xr-x. 3 root root 221184 Nov 3 10:30 yumdata
[root@9e654c194234 /]#
更改容器中data 目录中的内容
[root@9e654c194234 /]# mkdir /data/course
[root@9e654c194234 /]# exit
[root@harry-02 ~]# ls /data/
course mongodb mysql redis_data redis_data2 wwwroot yumdata
[root@harry-02 ~]#
2. 挂载数据卷

[root@harry-02 ~]# docker run -itd --volumes-from vigorous_stallman centos6 bash
82e29214118e5692e5c1b2857d62290aa1974f41a934c4b226ba694637282238
[root@harry-02 ~]# docker exec -it 82e29214118e5692e bash
[root@82e29214118e /]# ls /data/
course mongodb mysql redis_data redis_data2 wwwroot yumdata
[root@82e29214118e /]#
Docker数据卷的备份与恢复
Docker网络模式
- docker使用的网络实际上和宿主机一样,在容器内看到的网卡ip是宿主机ip
- 多个容器使用共同的网络,看到的ip是一样的
- 这种模式下,不会配置任何网络
- 这种模式会为每个容器分配一个独立的Network Namespace。类似于vmware的nat网络模式。同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的。
外部访问容器:
[root@harry-02 ~]# docker images
centos_with_net latest 2dffdc73293c 23 hours ago 285MB
harry_123 latest 9f38484d220f 2 weeks ago 202MB
registry latest f32a97de94e1 3 weeks ago 25.8MB
[root@harry-02 ~]# docker create -t centos_with_net bash
ba19ca31191ca718740b3f4ed1df2d94f58b2afe25c1f28314fa8cd7b5597f87
[root@harry-02 ~]# docker start ba19ca bash
ba19ca
[root@harry-02 ~]# docker exec -it ba19ca bash
[root@ba19ca31191c /]# yum -y install epel-release
[root@ba19ca31191c /]# yum -y install nginx
• 再把该容器导成一个新的镜像(centos-httpd),然后再使用新镜像创建容器,并指定端口映射
[root@harry-02 ~]# docker commit -m "install nginx" -a "harry" ba19ca31191c centos_with_nginx
sha256:44ef3c08f922ae5333720722f889fcb763bb5168f8b301a8d94cbcc448cded2f
[root@harry-02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_nginx latest 44ef3c08f922 12 seconds ago 466MB
harry_test latest a6638884e17a 21 hours ago 512MB
<none> <none> 69b6f25ec7fa 21 hours ago 0B
192.168.31.138:5000/centos6 latest 6102cc73679c 23 hours ago 512MB
centos6 latest 6102cc73679c 23 hours ago 512MB
centos_with_net latest 2dffdc73293c 24 hours ago 285MB
harry_123 latest 9f38484d220f 2 weeks ago 202MB
registry latest f32a97de94e1 3 weeks ago 25.8MB
[root@harry-02 ~]#
docker run -itd -p 8080:80 centos_with_centos bash //-p 可以指定端口映射,本例中将容器的80端口映射为本地的8080端口
[root@harry-02 ~]# docker create centos_with_nginx bash
501b9236e2c06573aaf9526715b4c39d9f44db00fff648c14cb37cdd35538498
[root@harry-02 ~]# docker start 501b9236e2
501b9236e2
[root@harry-02 ~]# docker run -itd -p 8080:80 centos_with_nginx bash
4ee6f77ac7a3c1b9472558dd7dea7e587483ad5bddc9a03289a9455a9bbcfb8f
[root@harry-02 ~]#
docker exec -it container_id bash ,启动nginx
[root@harry-02 ~]# docker exec -it 4ee6f77ac7a3 bash
[root@4ee6f77ac7a3 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted
报错:Operation not permitted
[root@harry-02 ~]# docker create centos_with_nginx bash
365831a38f3faced644b6e9604aaf1a9662ef1f859b88d55315a3485eef0a17d
[root@harry-02 ~]# docker start 365831a
365831a
[root@harry-02 ~]# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init
93bd1375ada5f88f29001bfbbeb55f02c2cb37572c7df605aa7a78bb26d1148c
[root@harry-02 ~]# docker exec -it 93bd1375ad bash
[root@93bd1375ada5 /]# systemctl start nginx
[root@93bd1375ada5 /]# ps aux | grep nginx
root 3404 0.0 0.1 125108 2112 ? Ss 06:58 0:00 nginx: master process /usr/sbin/nginx
nginx 3405 0.0 0.1 125496 3140 ? S 06:58 0:00 nginx: worker process
root 3407 0.0 0.0 9088 672 pts/1 S+ 06:58 0:00 grep --color=auto nginx
[root@93bd1375ada5 /]#
通过crul宿主机的8088端口,证明centos_with_nginx:80端口映射成功
配置桥接网络
Dockerfile创建镜像
Dockerfile格式:
Dockerfile示例-nginx
## Set the base image to CentOS
FROM centos
# File Author / Maintainer
MAINTAINER aming aming@aminglinux.com
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar zxvf nginx-1.8.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -fv /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf
#Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd


Docker compose部署服务

version: "2"
services:
app1:
image: centos_nginx
ports:
- "8080:80"
networks:
- "net1"
volumes:
- /data/:/data
app2:
image: centos_with_nettool
networks:
- "net2"
volumes:
- /data/:/data1
entrypoint: tail -f /etc/passwd
networks:
net1:
driver: bridge
net2:
driver: bridge