1、Docker简介
Docker通过内核虚拟化技术(namespace及cgroups等)来提供容器的资源隔离与安全保障等,由于docker通过操作系统层的虚拟化实现隔离,所以docker容器在运行时,不需要类似虚拟机额外的操作系统开销,提供资源利用率。
2、docker vs kvm
3、docker组件
镜像、容器、仓库
4、docker安装
[root@docker ~]#tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/CentOS/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
[root@docker ~]# yum install docker-engine -y
5、docker基础操作
[root@docker ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@docker ~]# systemctl start docker.service
[root@docker ~]#
[root@docker ~]# docker pull centos #拉取镜像
[root@docker ~]# docker pull daocloud.io/library/nginx
有时候拉取速度很慢,采用国内源加速
root@docker ~]# vim /usr/lib/systemd/system/docker.service 增加下面这行
EnvironmentFile=/etc/sysconfig/docker
新建配置文件[root@docker ~]# vim /etc/sysconfig/docker
在https://dashboard.daocloud.io/ 注册,然后点击加速器生成加速链接
OPTIONS=--registry-mirror=curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s
[root@docker ~]# docker search nginx #搜索镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0584b3d2cf6d 2 weeks ago 196.5 MB
导出镜像
[root@docker ~]# docker save -o nginx.tar daocloud.io/library/nginx
[root@docker ~]# docker save -o cnetos.tar centos
导入镜像
[root@docker ~]# docker load --input cnetos.tar 或者 [root@docker ~]# docker load < cnetos.tar
删除镜像
[root@docker ~]# docker rmi 0584b3d2cf6d (镜像ID)
[root@docker ~]# docker run centos /bin/echo "Hello world"
Hello world
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e381e68a385 centos "/bin/echo 'Hello wor" 6 seconds ago Exited (0) 5 seconds ago clever_lamarr
[root@docker ~]# docker run --name mydocker -t -i centos /bin/bash
[root@1a67f4c92b6e /]#
[root@1a67f4c92b6e /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 18:56 ? 00:00:00 /bin/bash
root 14 1 0 18:56 ? 00:00:00 ps -ef
[root@1a67f4c92b6e /]# exit
exit
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a67f4c92b6e centos "/bin/bash" 12 minutes ago Exited (0) 6 seconds ago mydocker
5e381e68a385 centos "/bin/echo 'Hello wor" 16 minutes ago Exited (0) 16 minutes ago clever_lamarr
[root@docker ~]# docker run --name docker-demo -d centos /bin/bash -d代表放入后台执行
6c5a777467b9552714f9cd3322e677750e2b8b5b0bd2d81e79094ad560828a5e
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c5a777467b9 centos "/bin/bash" 11 seconds ago Exited (0) 11 seconds ago docker-demo
1a67f4c92b6e centos "/bin/bash" 17 minutes ago Exited (0) 4 minutes ago mydocker
5e381e68a385 centos "/bin/echo 'Hello wor" 21 minutes ago Exited (0) 21 minutes ago clever_lamarr
[root@docker ~]# docker stop mydocker 停止容器
[root@docker ~]# docker start 1a67f4c92b6e 启动容器
[root@docker ~]# docker run -d --name mynginx daocloud.io/library/nginx
225a9b0459630c62dcf2199d6244b16a74ad9412471abf0be03755768df3ae63
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
225a9b045963 daocloud.io/library/nginx "nginx -g 'daemon off" 6 seconds ago
Up 5 seconds 80/tcp, 443/tcp mynginx
进入容器脚步
[root@docker ~]# cat docker_in.sh
#!/bin/bash
docker_in(){
NAME_ID=$1
PID=$(docker inspect --format "{{ .State.Pid }}" $NAME_ID)
nsenter -t $PID -m -u -i -n -p
}
docker_in $1