容器和虚拟化技术的对比
# 安装docker
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
列出所有docker-ce版本
默认安装最新的
[root@server01 ~]# yum install -y docker-ce
[root@server01 ~]# cat /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.7.5.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
查看docker的基本信息
# docker info
Docker最重要的三个概念
容器、镜像、仓库
镜像的基本操作:
查找镜像
[root@server01 ~]# docker search alpine
拉取镜像
[root@server01 ~]# docker pull alpine
打标签
[root@server01 ~]# docker tag 965ea09ff2eb docker.io/dockerstudy007/alpine:v3.10.3
[root@server01 ~]# docker images
登录公共docker仓库推送镜像
# docker login docker.io
[root@server01 ~]# docker push dockerstudy007/alpine:v3.10.3
看到已经推送到了仓库中
推送打标签的latest镜像
删除镜像
# docker rmi docker.io/dockerstudy007/alpine:latest
镜像的分层
容器的基本操作
运行一次就退出的容器
[root@server01 ~]# docker run --rm alpine:latest /bin/echo hello
Hello
后台启动容器
[root@server01 ~]# docker run -d --name myalpine01 dockerstudy007/alpine:latest /bin/sleep 300
进入容器
# docker exec -it myalpine01 /bin/sh
删除容器
[root@server01 ~]# for i in `docker ps -a|grep -i exit|awk '{print $1}'`;do docker rm -f $i;done
# 进入myalpine01容器,然后写入文件,并提交为新的镜像
[root@server01 ~]# docker exec -it myalpine01 /bin/sh
/ # echo 'hello' >1.txt
/ # cat 1.txt
hello
[root@server01 ~]# docker commit -p myalpine01 dockerstudy007/alpine:v3.10.3_with_1.txt
# 启动新容器可以看到1.txt文件已经固化到了容器中
[root@server01 ~]# docker run -it dockerstudy007/alpine:v3.10.3_with_1.txt /bin/sh
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
Hello
常用命令
Docker的高级命令
容器的高级用法
# cd /data/html
[root@server01 html]# wget www.baidu.com -O index.html
# 把容器的80端口映射到82端口,/data/html目录映射到容器的/usr/share/nginx/html目录
[root@server01 html]# docker run --rm --name nginx_with_baidu -d -p82:80 -v /data/html:/usr/share/nginx/html dockerstudy007/nginx:v1.12.2
传递环境变量
# 打印环境变量
[root@server01 ~]# docker run --rm -e E_OPTS=abcdefg dockerstudy007/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=bdcfab5c7def
E_OPTS=abcdefg
HOME=/root
# 不传递环境变量
[root@server01 ~]# docker run --rm dockerstudy007/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=c318854e4d6e
HOME=/root
# 传递多个环境变量
[root@server01 ~]# docker run --rm -e E_OPTS=abcdefg,C_OPTS=123456 dockerstudy007/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=19728fa9a405
E_OPTS=abcdefg,C_OPTS=123456
HOME=/root
# 运行容器,并更新容器的apt源
[root@server01 ~]# docker exec -it nginx_with_baidu /bin/bash
# 更新nginx的apt源
tee /etc/apt/sources.list << EOF
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
EOF
设置apt-get的代理
cd /etc/apt
echo 'Acquire::http::Proxy "http://10.11.0.148:808";' >> apt.conf
echo 'Acquire::https::proxy "http://10.11.0.148:808";' >> apt.conf
安装curl命令
root@815c912ddd9f:/# apt-get update && apt-get install curl -y
中途因为apt-get经常无法update 到95%暂停一直如此,可能是nginx依赖的容器底层的debian系统太老,更换nginx版本为最新
E: Some index files failed to download. They have been ignored, or old ones used instead
rm /var/lib/apt/lists/* -vrf
下载最新的nginx容器
[root@server01 ~]# docker run -d --name nginx_with_baidu -p82:80 -v /data/html:/usr/share/nginx/html nginx
[root@server01 ~]# docker commit -p 1ef966643542 dockerstudy007/nginx:curl
把带curl的nginx推送到公网
容器的生命周期