2020-10-28

<meta charset="utf-8">

一.docker安装

1.1 Yum源配置(阿里云)

[root@192 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@192 yum.repos.d]#yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 [root@192 yum.repos.d]# yum clean all [root@192 yum.repos.d]# yum repolist

1.2 安装docker环境依赖

[root@192 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

1.3 安装docker-ce

[root@192 ~]# yum install docker-ce docker-ce-cli containerd.io -y

设置docker启动,开机启动

[root@192 ]# systemctl start docker [root@192 ]# systemctl enable docker

Docker版本查看

[root@192 ~]# docker version

Docker 信息

[root@192 ~]# docker info

1.4 使用阿里云docker 镜像加速器,提高速度

登录hub服务https://cr.console.aliyun.com的控制台获取加上地址

创建daemon.json文件

{ "graph": "/data/docker", #docker工作目录 "storage-driver": "overlay2", #存储驱动 overlay2 "insecure-registries": ["registry.access.redhat.com","quay.io"], "registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],#阿里云镜像加速源 "bip": "172.7.5.1/24", #docker 网络地址 "exec-opts": ["native.cgroupdriver=systemd"], "live-restore": true }

1.5 开启网络转发功能

vim /etc/sysctl.conf 添加此行 net.ipv4.ip_forward =1 # sysctl -p #生效配置 注意:生效后 重启docker服务

1.6 登录docker hub

登录:http://dockerhub.com/ --> http://hub.docker.com 账号:yankun20 密码:yankunyankun---222 登录: docker login docker.io

二. docker 常用命令:

2.1下载镜像

[root@192 ~]# docker search alpine #从docker hub 中收索符合条件的镜像 [root@192 ~]# docker pull alpine # 直接下载一个镜像 Using default tag: latest latest: Pulling from library/alpine c9b1b535fdd9: Pull complete Digest: sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d Status: Downloaded newer image for alpine:latest docker.io/library/alpine:latest [root@192 ~]# docker pull alpine:3.10.3 #下载指定版本 3.10.3: Pulling from library/alpine 89d9c30c1d48: Pull complete Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a Status: Downloaded newer image for alpine:3.10.3 docker.io/library/alpine:3.10.3

2.2 查看本地镜像

[root@192 ~]# docker images #查看本地镜像(docker image ls) REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest e7d92cdc71fe 4 weeks ago 5.59MB alpine 3.10.3 965ea09ff2eb 3 months ago 5.55MB hello-world latest fce289e99eb9 13 months ago 1.84kB [root@192 ~]#

2.3 给镜像打标签

[root@192 ~]# docker tag 965ea09ff2eb docker.io/yankun20/alpine:v3.10.3 [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest e7d92cdc71fe 4 weeks ago 5.59MB alpine 3.10.3 965ea09ff2eb 3 months ago 5.55MB yankun20/alpine v3.10.3 965ea09ff2eb 3 months ago 5.55MB hello-world latest fce289e99eb9 13 months ago 1.84kB

2.4 推送镜像

[root@192 ~]# docker push docker.io/yankun20/alpine:v3.10.3 The push refers to repository [docker.io/yankun20/alpine] 77cae8ab23bf: Layer already exists v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528

2.5 删除镜像

[root@192 ~]# docker rmi yankun20/alpine:v3.10.4 Untagged: yankun20/alpine:v3.10.4 [root@192 ~]# docker rmi -f 965ea09ff2eb Untagged: alpine:3.10.3 Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a Untagged: yankun20/alpine:v3.10.3 Untagged: yankun20/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652 Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0

三.docker 容器的基本操作

3.1查看本地的容器进程

[root@192 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a4ae6ed17961 hello-world "/hello" 4 days ago Exited (0) 4 days ago musing_moore

3.2 启动容器(运行镜像)

[图片上传失败...(image-94f000-1603874420697)]

交互式启动一个容器

[root@192 ~]# docker run -ti yankun20/alpine:latest /bin/sh WARNING: IPv4 forwarding is disabled. Networking will not work. / # cat /etc/issue Welcome to Alpine Linux 3.10 Kernel \r on an \m (\l)

非交互式启动一个后台容器

[root@192 ~]# docker run --rm yankun20/alpine:latest /bin/echo hello WARNING: IPv4 forwarding is disabled. Networking will not work. hello

[root@192 ~]# docker run -d --name myalpine1 yankun20/alpine:latest /bin/sleep 300 WARNING: IPv4 forwarding is disabled. Networking will not work. b9650e50f0be4ca4e70890b5990cb4ffa075f68640d79d6ee3eb6e5bb8a8ff4f

查看容器

[root@192 ~]# docker ps #查看正在运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b9650e50f0be yankun20/alpine:latest "/bin/sleep 300" 2 minutes ago Up 2 minutes myalpine1 [root@192 ~]# docker ps -a # 查看本地所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b9650e50f0be yankun20/alpine:latest "/bin/sleep 300" 2 minutes ago Up 2 minutes myalpine1 6de9ad9a1890 yankun20/alpine:latest "/bin/sh" 26 minutes ago Exited (0) 26 minutes ago myalpine e7dd92ed0fc4 yankun20/alpine:latest "/bin/sh" 31 minutes ago Exited (0) 29 minutes ago unruffled_agnesi f510ac0a764f hello-world "/hello" 35 minutes ago Exited (0) 35 minutes ago thirsty_knuth a4ae6ed17961 hello-world "/hello" 4 days ago Exited (0) 4 days ago musing_moore

查看宿主机进程

[root@192 ~]# ps -aux |grep sleep root 23265 0.0 0.0 1540 248 ? Ss 10:18 0:00 /bin/sleep 3000

进入容器

[root@192 ~]# docker exec -ti 61c50357818e /bin/sh / # ps PID USER TIME COMMAND 1 root 0:00 /bin/sleep 3000 6 root 0:00 /bin/sh 11 root 0:00 ps

停止/启动/重启容器

[root@192 ~]# docker stop 61c50357818e #停止容器 61c50357818e [root@192 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 61c50357818e yankun20/alpine:latest "/bin/sleep 3000" 4 minutes ago Exited (137) 18 seconds ago myalpine1 [root@192 ~]# docker start 61c50357818e #启动容器 61c50357818e [root@192 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 61c50357818e yankun20/alpine:latest "/bin/sleep 3000" 4 minutes ago Up 7 seconds myalpine1 [root@192 ~]# docker restart 61c50357818e #重启容器 61c50357818e

删除容器

[root@192 ~]# docker rm myalpine myalpine [root@192 ~]# docker rm myalpine1 Error response from daemon: You cannot remove a running container 61c50357818e9a81fedc8e17eaeeae16015256ac180d2e320d656aa2b23ef74d. Stop the container before attempting removal or force remove [root@192 ~]# docker rm -f myalpine1 # 正在运行的容器 加 -f 删除 myalpine1

过滤宿主机上已经退出的容器并且删除

for i in docker ps -a|grep -i exit|awk '{print $1}';do docker rm -f $i;done

提交/修改容器

修改容器

[root@192 ~]# docker exec -it myalpine /bin/sh / # ls / bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # cat /etc/issue Welcome to Alpine Linux 3.10 Kernel \r on an \m (\l) / # echo hello >1.txt / # cat 1.txt hello / #

提交容器

[root@192 ~]# docker commit -p myalpine yankun20/alpine:v3.10.3_with_1.txt #提交一个容器 sha256:281589c5c3bd9fb194a3bf68f3ddda5e9636d6c78af5f6ebc91a75cd87db9d2d [root@192 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2fc5ca804d39 yankun20/alpine:latest "/bin/sleep 3000" 7 minutes ago Up 7 minutes myalpine [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE yankun20/alpine v3.10.3_with_1.txt 281589c5c3bd 32 seconds ago 5.55MB alpine latest e7d92cdc71fe 4 weeks ago 5.59MB yankun20/alpine latest 965ea09ff2eb 3 months ago 5.55MB

导出/导入镜像

导出镜像 [root@192 ~]# docker save 281589c5c3bd > alpine:v3.10.3_with_1.txt.tar 导入镜像(tag标签需要重新定义) [root@192 ~]# docker load < alpine:v3.10.3_with_1.txt.tar [root@192 ~]# docker tag 281589c5c3bd yankun20/alpine:v3.10.3_with_1.txt 交互式启动进入容器 [root@192 ~]# docker run -ti --name alpinetest yankun20/alpine:v3.10.3_with_1.txt /bin/sh

查看容器的日志

[图片上传失败...(image-ed82-1603874420697)]

[root@192 ~]# docker logs -f 3d6d7f8c1c3b

四.docker 高级操作

映射端口

挂载数据卷

传递环境变量

容器内安装软件(工具)

[图片上传失败...(image-b260b5-1603874420697)]

4.1 下载NGINX 镜像

[root@192 ~]# docker pull nginx:1.12.2 [root@192 ~]# docker tag 4037a5562b03 yankun20/nginx:v1.12.2

4.2 端口映射

[root@192 ~]# docker run --rm --name mynginx -d -p81:80 yankun20/nginx:v1.12.2 WARNING: IPv4 forwarding is disabled. Networking will not work. 9e57bab85e4d1ebe70b4e3a112638afe7d8959ae96ff96f6acd6f472fd8acec4

4.3 挂载数据卷

[root@192 ~]# mkdir html [root@192 ~]# cd html/ [root@192 html]# wget www.baidu.com -O index.html [root@192 html]# docker run -d --rm --name nginx_baidu -d -p82:80 -v /root/html:/usr/share/nginx/html yankun20/nginx:v1.12.2 WARNING: IPv4 forwarding is disabled. Networking will not work. dc0d19f4b4ef3e25e17df806492e1d4885e3a2e0f5da1f94e31099c44aa0332d

检查容器信息

[root@192 ~]# docker inspect dc0d19f4b4ef

4.4 传递环境变量

[root@192 ~]# docker run --rm -e E_OPTS=abcdefg yankun20/alpine:latest printenv WARNING: IPv4 forwarding is disabled. Networking will not work. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=b3eff0dfc0c5 E_OPTS=abcdefg HOME=/root [root@192 ~]# docker run --rm -e E_OPTS=abcdefg -e name=yankun yankun20/alpine:latest printenv WARNING: IPv4 forwarding is disabled. Networking will not work. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=70ac28c684d3 E_OPTS=abcdefg name=yankun HOME=/root

4.5 容器内安装软件(工具)

[root@192 ~]# docker exec -ti 007d2094798d /bin/sh #进入容器 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 update && apt-get install curl -y #更新源并且安装 curl 工具

[root@192 ~]# docker commit -p 007d2094798d yankun20/nginx:curl #制作本地镜像 [root@192 ~]# docker push yankun20/nginx:curl #推送到公网 The push refers to repository [docker.io/yankun20/nginx] e35b87949c19: Pushed 4258832b2570: Mounted from library/nginx

[图片上传失败...(image-b6335c-1603874420697)]

五.Dockerfile

[图片上传失败...(image-ba6654-1603874420697)]

[图片上传失败...(image-b7309-1603874420697)]

5.1 USER/WORKDIR 指令

  • dockerfile

vim /data/dockerfile/dockerfile FROM yankun20/nginx:curl USER nginx WORKDIR /usr/share/nginx/html

  • 构建镜像

[root@192 dockerfile]# docker build . -t docker.io/yankun20/nginx:v1.12.2_with_user_workdir Sending build context to Docker daemon 2.048kB Step 1/3 : FROM yankun20/nginx:curl ---> c6c6cee4dfda Step 2/3 : USER nginx ---> Running in c0af23b1cef7 Removing intermediate container c0af23b1cef7 ---> c788508de220 Step 3/3 : WORKDIR /usr/share/nginx/html ---> Running in a538da65554c Removing intermediate container a538da65554c ---> ec5d3ab228e4 Successfully built ec5d3ab228e4 Successfully tagged yankun20/nginx:v1.12.2_with_user_workdir

  • 验证

[root@192 dockerfile]# docker run --rm -ti --name nginx123 yankun20/nginx:v1.12.2_with_user_workdir /bin/sh whoami nginx pwd /usr/share/nginx/html $

5.2 ADD/EXPOSE 指令

  • dockerfile

[root@192 ~]# vim /data/dockerfile/dockerfile FROM yankun20/nginx:curl ADD index.html /usr/share/nginx/html/index.html EXPOSE 80

  • 构建镜像

[root@192 ~]# docker build /data/dockerfile/ -t yankun20/nginx:v1.12.2_with_index_expose Sending build context to Docker daemon 5.12kB Step 1/3 : FROM yankun20/nginx:curl ---> c6c6cee4dfda Step 2/3 : ADD index.html /usr/share/nginx/html/index.html ---> 01d50aecad62 Step 3/3 : EXPOSE 80 ---> Running in bab726d4bd8c Removing intermediate container bab726d4bd8c ---> 453cde8484c1 Successfully built 453cde8484c1 Successfully tagged yankun20/nginx:v1.12.2_with_index_expose

  • 验证

[root@192 ~]# docker run --rm -d --name nginx123 -P yankun20/nginx:v1.12.2_with_index_expose e1ef34261ccc911b85c1cbe6380619cad7317e0eac91ec7b61a5bd23485d184a

5.3 RUN/ENV 指令

  • dockerfile

ENV :定义环境变量

RUN :

FROM centos:7 ENV VER 9.11.4 RUN yum install bind-$VER -y

  • 构建镜像

[root@192 ~]# docker build /data/dockerfile/ -t yankun20/bind:v9.11.4_with_env_run

  • 验证

[root@192 ~]# docker run -ti --rm yankun20/bind:v9.11.4_with_env_run /bin/sh sh-4.2# rpm -qa |grep bind bind-libs-lite-9.11.4-9.P2.el7.x86_64 bind-libs-9.11.4-9.P2.el7.x86_64 bind-license-9.11.4-9.P2.el7.noarch bind-9.11.4-9.P2.el7.x86_64 sh-4.2# printenv HOSTNAME=1afd8941e959 TERM=xterm PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PWD=/ SHLVL=1 HOME=/root VER=9.11.4 _=/usr/bin/printenv

5.4 CMD/ENTRYPOINT 指令

  • dockerfile

CMD

FROM centos:7 RUN yum install httpd -y CMD ["httpd","-D","FOREGROUND"]

  • 构建镜像

[root@192 ~]# docker build /data/dockerfile/ -t yankun20/httpd:test

  • 验证

[root@192 ~]# docker run -d --rm --name myhttpd -p83:80 yankun20/httpd:test eb67b5e8a0d6d76db0cf8db36085525408a05afe377df759d73b8450cf123656 [root@192 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb67b5e8a0d6 yankun20/httpd:test "httpd -D FOREGROUND" 2 minutes ago Up 2 minutes 0.0.0.0:83->80/tcp myhttpd

ENTRYPOINT

  • dockerfile

FROM centos:7 ADD entrypoint.sh /entrypoint.sh RUN yum install epel-release -q -y && yum install nginx -y ENTRYPOINT /entrypoint.sh

  • 构建镜像

[root@192 dockerfile]# docker build . -t yankun20/centos:nginx_002

  • 验证

docker run -d --rm --name mynginx -p88:80 yankun20/centos:nginx_002

六.综合实验

运行一个docker容器,在浏览器打开demo.od.com 能访问到百度首页

  • dockerfile

FROM yankun20/nginx:v1.12.2 USER root ENV WWW /usr/share/nginx/html ENV CONF /etc/nginx/conf.d RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ echo 'Asia/Shanghai' >/etc/timezone WORKDIR WWW ADD index.htmlWWW/index.html ADD demo.od.com.conf $CONF/demo.od.com.conf EXPOSE 80 CMD ["nginx","-g","daemon off;"]

  • demo.od.com.conf配置文件

[root@192 dockerfile]# vim demo.od.com.conf

server { listen 80; server_name demo.od.com; root /usr/share/nginx/html; }

  • 构建镜像

[root@192 dockerfile]# docker build . -t yankun20/nginx:baidu

  • 验证

[root@192 dockerfile]# docker run --rm -P yankun20/nginx:baidu

七.docker 网络模型

[图片上传失败...(image-d8c48b-1603874420696)]

[图片上传失败...(image-3d9db6-1603874420696)]

[图片上传失败...(image-13cbc8-1603874420696)]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350