1、Docker概念
Docker是一个开源的应用容器引擎,让开发者可以打包它们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上,也可以实现虚拟化。容器是完全使用沙箱机制,Docker对进程之间相互隔离,相互之间不会有任何的接口,对底层环境无任何要求
2、设计的目标
4、Docker的组成
客户端与Docker服务器的守护进程进行通信,守护进程根据请求来实现构建、运行和发布。客户机可以与服务器在同一台主机,也可以在不同网络之间进行通信
1、关闭防火墙及核心功能
[root@docker ~]# systemctl stop firewalld
[root@docker ~]# systemctl disable firewalld
[root@docker ~]# setenforce 0
[root@docker ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
2、依赖包、docker镜像源、docker安装
[root@docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker ~]# yum -y install docker-ce
3、启动docker
[root@docker ~]# systemctl restart docker
[root@docker ~]# systemctl enable docker
4、镜像加速
[root@docker ~]# cd /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
5、网络优化
[root@docker ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@docker ~]# sysctl -p
[root@docker ~]# service network restart
[root@docker ~]# systemctl restart docker
1、账户登录(登录仓库时使用)
[root@docker ~]# docker login
2、查看版本信息
[root@docker ~]# docker version
3、搜索镜像
#docker search 镜像名称
[root@docker ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12946 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1767 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 764 [OK]
docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 102
docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 81 [OK]
#星级越高,表示下载使用的人越多
4、镜像下载
#docker pull 镜像名称
[root@docker ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for docker.io/nginx:latest
5、查看所有镜像
#docker images
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest ed21b7a8aee9 8 days ago 127 MB
6、查看指定镜像信息
#docker inspect 镜像ID
[root@docker ~]# docker inspect ed21b7a8aee9
[
{
"Id": "sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291",
"RepoTags": [
"docker.io/nginx:latest"
],
"RepoDigests": [
7、添加镜像标签
#docker tag 旧名称 新名称
[root@docker ~]# docker tag docker.io/nginx:latest nginx:web
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest ed21b7a8aee9 8 days ago 127 MB
nginx web ed21b7a8aee9 8 days ago 127 MB
#相当于生成创建一个新的镜像
8、删除指定镜像
#docker rmi 镜像名
#使用镜像创建容器后,如果没有删除容器,镜像删除不了
[root@docker ~]# docker rmi nginx:web
Untagged: nginx:web
Untagged: docker.io/nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest ed21b7a8aee9 8 days ago 127 MB
9、将镜像导出到当前目录
docker save -o 文件名 镜像名
[root@docker abc]# docker save -o nginx nginx:latest
[root@docker abc]# ls -lh
总用量 125M
-rw-------. 1 root root 125M 4月 8 18:11 nginx
#文件名可以使用绝对路径
10、载入镜像
#docker load < 镜像名
11、上传镜像到仓库
#docker push 镜像名称
#先登录仓库,再进行上传
12、创建容器
#docker create -it 镜像名 /bin/bash
# -i:让容器的标准输入保持打开
# -t:让Docker分配一个伪终端
[root@docker ~]# docker create -it nginx:latest /bin/bash
de7b916f83fe7a7f5841fba1631c6345125dda760d01ae024263b864cde1f67a
13、容器查看
#docker ps -a
# -a:列出最近一次启动的容器
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de7b916f83fe nginx:latest "/bin/bash" 33 seconds ago Created optimistic_torvalds
#此时的状态为created
14、启动容器
#docker start 容器ID
[root@docker ~]# docker start de7b916f83fe
de7b916f83fe
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de7b916f83fe nginx:latest "/bin/bash" 3 minutes ago Up 12 seconds 80/tcp optimistic_torvalds
#此时显示的状态为UP,且端口为80端口
15、启动执行命令查看系统根目录
[root@docker ~]# docker run centos:7 /usr/bin/bash -c ls /
anaconda-post.log
bin
dev
etc
home
lib
#此命令会先检查本地的镜像,没有的话会从仓库下载
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f451a6ce24e0 centos:7 "/usr/bin/bash -c ..." 36 seconds ago Exited (0) 35 seconds ago awesome_shirley
de7b916f83fe nginx:latest "/bin/bash" 13 minutes ago Up 10 minutes 80/tcp optimistic_torvalds
#执行完成会直接关闭,状态是exited
#使用docker run -d 为持续执行
[root@docker ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
11ca9b188156e7216375f30506c22715af08e73cf625cd3fabb300dc0a2d3f0f
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11ca9b188156 centos:7 "/bin/bash -c 'whi..." 7 seconds ago Up 7 seconds festive_dubinsky
f451a6ce24e0 centos:7 "/usr/bin/bash -c ..." 7 minutes ago Exited (0) 7 minutes ago awesome_shirley
de7b916f83fe nginx:latest "/bin/bash" 20 minutes ago Up 17 minutes 80/tcp optimistic_t
16、终止容器
#docker stop 容器ID
#正常退出状态会变成exit0
17、进入容器
#docker exec -it 容器ID /bin/bash
#一定要是开启状态
[root@docker ~]# docker exec -it de7b916f83fe /bin/bash
root@de7b916f83fe:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@de7b916f83fe:/# exit
exit
[root@docker ~]#
18、容器导出
#docker export 镜像ID
[root@docker abc]# docker export de7b916f83fe > nginx_1
[root@docker abc]# ls
nginx_1
19、容器导入
#cat 文件名 | docker import - 镜像名
#上传导入的是镜像,不会生成容器
[root@docker abc]# cat nginx_1 | docker import - nginx
sha256:88960c2fb5f526bcfae0a747b25fcba690607fc77013338dc9189096843852c8
[root@docker abc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 88960c2fb5f5 3 seconds ago 125 MB
[root@docker abc]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20、删除容器
#docker rm 容器ID
#批量删除容器
docker ps -a | awk '{print "docker rm " $1}' | bash