docker安装及常用操作
一、安装docker
1、移除以前docker相关包
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
2、配置yum源
sudo yum install -y yum-utils sudo yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io #以下是在安装k8s的时候使用 yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
4、启动
systemctl enable docker --now
5、配置加速
这里额外添加了docker的生产环境核心配置cgroup
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF sudo systemctl daemon-reload sudo systemctl restart docker
二、docker三大核心要素
镜像(Image) 、容器(Container) 、仓库(Registry)
三、docker常用操作
1、镜像操作
#在镜像仓库查找镜像 [root@bg06 ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos DEPRECATED; The official build of CentOS. 7513 [OK] kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 33 couchbase/centos7-systemd centos7-systemd images with additional debug… 7 [OK] dokken/centos-7 CentOS 7 image for kitchen-dokken 6 dokken/centos-stream-9 4 dokken/centos-stream-8 4 continuumio/centos5_gcc5_base 3 dokken/centos-8 CentOS 8 image for kitchen-dokken 3 eclipse/centos_jdk8 CentOS, JDK8, Maven 3, git, curl, nmap, mc, … 2 [OK] ..... #从镜像仓库拉取镜像 [root@bg06 ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 72a69066d2fe: Pull complete 93619dbc5b36: Pull complete 99da31dd6142: Pull complete 626033c43d70: Pull complete 37d5d7efb64e: Pull complete ac563158d721: Pull complete d2ba16033dad: Pull complete 688ba7d5c01a: Pull complete 00e060b6d11d: Pull complete 1c04857f594f: Pull complete 4d7cfa90e6ea: Pull complete e0431212d27d: Pull complete Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest .... #查看本地镜像 [root@bg06 ~]# docker images #或者docker images centos 、 docker images centos:3.14 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 14 months ago 141MB alpine 3.14 0a97eee8041e 15 months ago 5.61MB centos latest 5d0da3dc9764 17 months ago 231MB ..... #所有镜像存储的位置 [root@bg06 sha256]# pwd /opt/module/docker/data/docker/image/overlay2/imagedb/content/sha256 #该目录下都是json文件,记录镜像和容器的关系 [root@bg06 sha256]# ll 总用量 420 -rw------- 1 root root 2711 2月 24 15:14 0369cf4303ffdb467dc219990960a9baa8512a54b0ad9283eaf55bd6c0adb934 -rw------- 1 root root 1464 2月 27 13:57 05897620e3b01576f5fa838098e260a5f39211807eac1e415d7ea27677c952d9 -rw------- 1 root root 2580 2月 27 14:08 072763fd75f3759b988d965663aa8138492e2ac84fb93e54a69cd952f452a790 -rw------- 1 root root 1905 2月 27 13:44 09adeeb8df2051eaeb0d7fa49d1997cdb64ea64088d05c4441e828d60f6d35ec -rw------- 1 root root 1471 2月 27 13:59 0a97eee8041e2b6c0e65abb2700b0705d0da5525ca69060b9e0bde8a3d17afdb -rw------- 1 root root 2635 2月 24 15:13 0d0d57e4f64c45d5a1ce7c35202373a7b1c11618f1d07c82c08198cd332161de #删除镜像 docker rmi 镜像名:版本号/镜像id
2、容器管理
#运行容器 # -d 后台运行 # --restart=always: 开机自启 # -p 端口映射 [root@bg06 ~]# docker run --name=mynginx -d -p 80:80 nginx 336a48b1984702fed3e3b7bf19123de82b580c3f7eaa2bf5a7b54932f6935022 #查看正在运行的容器 [root@bg06 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dd8570f3d25 nginx:latest "/docker-entrypoint.…" 15 hours ago Up 15 hours 80/tcp wonderful_agnesi f699660dd603 nginx "/docker-entrypoint.…" 15 hours ago Up 15 hours k8s_mynginx_mynginx-7bf8c6db65-gjwz2_gw_627bb83d-6076-4c02-83e5-377c542baa5f_1 481d07820a19 registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller "/usr/bin/dumb-init …" 20 hours ago Up 20 hours k8s_controller_ingress-nginx-controller-65bf56f7fc-gjv7v_ingress-nginx_dc173590-b684-4113-9f19-c560cbb83864_0 #-a表示查看所有状态的容器 [root@bg06 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dd8570f3d25 nginx:latest "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp wonderful_agnesi f699660dd603 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes k8s_mynginx_mynginx-7bf8c6db65-gjwz2_gw_627bb83d-6076-4c02-83e5-377c542baa5f_1 481d07820a19 registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller "/usr/bin/dumb-init …" 5 hours ago Up 5 hours k8s_controller_ingress-nginx-controller-65bf56f7fc-gjv7v_ingress-nginx_dc173590-b684-4113-9f19-c560cbb83864_0 8766f13e2860 a013daf8730d "/kube-webhook-certg…" 5 hours ago Exited (0) 5 hours ago k8s_patch_ingress-nginx-admission-patch-jh92l_ingress-nginx_975ed785-3add-4c43-8128-d9e9e3cb288b_3 ...... # 删除停止的容器 docker rm 容器id/名字 docker rm -f mynginx #强制删除正在运行中的 (删除容器后,下次再次启动那个镜像,则恢复到该镜像最原始的样子)! #停止容器 docker stop 容器id/名字 [root@bg06 ~]# docker stop f699660dd603 f699660dd603 #再次启动 docker start 容器id/名字 #应用开机自启 docker update 容器id/名字 --restart=always
3、修改容器内容
#进入到容器内部修改内容 [root@bg06 ~]# docker exec -it 336a48b19847 /bin/bash root@336a48b19847:/# cd /usr/share/nginx/html/ root@336a48b19847:/usr/share/nginx/html# ll bash: ll: command not found root@336a48b19847:/usr/share/nginx/html# ls 50x.html index.html root@336a48b19847:/usr/share/nginx/html# echo "<h1>WELCOME ABC!!!!<h1>" > index.html echo "<h1>WELCOME abclsls<h1>" > index.html #但是改变容器之后,如果删除容器,再重新运行镜像创建容器,容器就会变成最原始的样子 #如果停止容器后,然后重新启动容器,那么此时容器是上次停止时的状态 #总结,容器一旦删除,那么对容器的所有的修改都失效 [root@bg06 ~]# docker ps -a | grep mynginx 336a48b19847 nginx "/docker-entrypoint.…" 14 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx [root@bg06 ~]# docker stop 336a48b19847 336a48b19847 [root@bg06 ~]# docker rm 336a48b19847 336a48b19847 #重新根据原镜像创建出来的容器,已经恢复到最原始的样子 [root@bg06 ~]# docker exec -it 9579175498f2 /bin/bash root@9579175498f2:/usr/share/nginx/html# cat index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>
#挂载数据到外部修改 参数解释: -v 表示将内部路径挂载到外部,前面是外部路径 :ro 表示read only(只读模式),在容器内部不能修改,只能在外面修改。 :rw 表示read write(读写模式),在容器内部可以修改 docker run -d -p 88:80 -v /data/html:/usr/share/nginx/html:ro abc_nginx #这样以后修改页面只需要在容器外面修改即可
4、提交改变(提交改变后的容器为本地镜像)
#参数解释 -a 作者名 -m 备注 docker commit -a "abcd" -m "首页变化" 341d81f7504f guignginx:v1.0 [root@bg06 ~]# docker ps | grep nginx 9579175498f2 nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp beautiful_mirzakhani [root@bg06 ~]# docker commit -a "abc" -m "首页变化,测试提交改变" 9579175498f2 abc_nginx sha256:c4428506a2cc37481e0bf28bb82902e323a29dcb87fc39fbde8a27f1555d29e5 [root@bg06 ~]# docker rm -f 9579175498f2 9579175498f2 [root@bg06 ~]# docker images | grep abc_nginx abc_nginx latest c4428506a2cc 39 seconds ago 141MB [root@bg06 ~]# docker run -d abc_nginx ea3b3b3f5c09fbe9f9c51884eadc60d7aaeb8912ab53b06bf98968d328d28d5a [root@bg06 ~]# docker ps | grep abc ea3b3b3f5c09 abc_nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 80/tcp dazzling_tu [root@bg06 ~]# docker exec -it ea3b3b3f5c09 /bin/bash root@ea3b3b3f5c09:/# cd /usr/share/nginx/html/ root@ea3b3b3f5c09:/usr/share/nginx/html# cat index.html <h1>WELCOME abc<h1>
5、镜像保存
第一种方式:物理保存镜像为本地压缩文件 [root@bg06 ~]# docker save -o abc_nginx.tar abc_nginx:latest 您在 /var/spool/mail/root 中有邮件 #删除原镜像 [root@bg06 ~]# docker rmi abc_nginx Untagged: abc_nginx:latest Deleted: sha256:c4428506a2cc37481e0bf28bb82902e323a29dcb87fc39fbde8a27f1555d29e5 Deleted: sha256:b9146a3532f86cbf2b75cd6d12dd11af7ddd8a1483862b05d8eed77e28cf7035 #load物理压缩文件,产生新镜像 [root@bg06 ~]# docker load -i abc_nginx.tar dcb0cd5f310b: Loading layer [==================================================>] 17.41kB/17.41kB Loaded image: abc_nginx:latest [root@bg06 ~]# docker images | grep abc abc_nginx 第二种方式:推送镜像到docker hub # 把旧镜像的名字,改成仓库要求的新版名字 [root@bg06 ~]# docker tag abc_nginx:latest abcchuyi/abc_nginx:latest # 登录到docker hub [root@bg06 ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: abcchuyi Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded # 推送 [root@bg06 ~]# docker push abcchuyi/abc_nginx:latest The push refers to repository [docker.io/abcchuyi/abc_nginx] dcb0cd5f310b: Layer already exists d874fd2bc83b: Pushed 32ce5f6a5106: Pushed f1db227348d0: Pushed b8d6e692a25e: Pushed e379e8aedd4d: Pushed 2edcec3590a4: Pushed latest: digest: sha256:94f82e3a1a28958146a7b210eece376aaf79f8dafd93994b8ea89b9b86bff5a0 size: 1778 docker logout(推送完成镜像后退出) [root@bg06 ~]# docker logout Removing login credentials for https://index.docker.io/v1/ # 别的机器下载 docker pull other/guignginx:v1.