docker简介
1。
Docker是一个开源的应用容器引擎,是一个轻量级容器技术。基于Go语言并遵从Apache2.0协议开源。Docker可以让开发者打包他们的应用和依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低。
Docker的理念:Build,Ship and Run Any App,Anywhere,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP(可以是一个WEB应用或数据库应用等等)及其运行环境能够做到一次封装,到处运行。
2。对比
一、 docker的安装
在网上找资源, 或者去老吴那里去把pub/docs/docker/20 目录端下来, 放在apache的发布目录下, 然后在你的虚拟机上/etc/yum.repos.d/下 编写docker.repo, 以上均是在老李那里学的 很基础,说实话我忘的差不多了!
构建软件仓库的好处,能自动处理软件的依赖性。
[root@server8 ~]
[root@server8 ~]
[root@server8 ~]
[root@server8 ~]
[root@server8 yum.repos.d]
net.bridge.bridge-nf-call-iptables = 0
[root@server8 yum.repos.d]
[root@server8 yum.repos.d]
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
[root@server8 yum.repos.d]
[root@server8 yum.repos.d]
二、docker的使用
1。简单在网页上传一个游戏,游戏来源:老吴的阿里云
[root@server8 yum.repos.d]
[root@server8 yum.repos.d]
[root@server8 yum.repos.d]
现在可以在浏览器上 访问ip+端口
三、docker的镜像分层,讲解及其演示
1、docker的命令大概这么多,我们学了一部分
[root@server8 yum.repos.d]
[root@server8 yum.repos.d]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server8 yum.repos.d]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c068e6bf0ae3 yakexi007/mario "python3 -m http.ser…" 9 minutes ago Exited (137) 3 minutes ago demo
[root@server8 yum.repos.d]
IMAGE CREATED CREATED BY SIZE COMMENT
9a35a9e43e8c 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c apt-get install -y python3 0B
<missing> 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c
<missing> 6 years ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$… 1.9kB
<missing> 6 years ago /bin/sh -c echo '
<missing> 6 years ago /bin/sh -c
2、演示操作
[root@server8 ~]
lftp 172.25.254.250:/> cd pub/images/
lftp 172.25.254.250:/pub/images> get ubuntu.tar
[root@server8 ~]
a70daca533d0: Loading layer [==================================================>] 75.16MB/75.16MB
Loaded image: ubuntu:latest
[root@server8 ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c29284518f49 2 months ago 72.8MB
yakexi007/game2048 latest 19299002fdbe 4 years ago 55.5MB
yakexi007/mario latest 9a35a9e43e8c 6 years ago 198MB
[root@server8 ~]
IMAGE CREATED CREATED BY SIZE COMMENT
c29284518f49 2 months ago /bin/sh -c
<missing> 2 months ago /bin/sh -c
[root@server8 ~]
root@df1ddc6b402f:/
3.10.0-957.el7.x86_64
[root@server8 ~]
3.10.0-957.el7.x86_64
[root@server8 ~]
Using default tag: latest
latest: Pulling from library/busybox
24fb2886d6f6: Pull complete
Digest: sha256:f7ca5a32c10d51aeda3b4d01c61c6061f497893d7f6628b92f822f7117182a57
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@server8 ~]
/
bin dev etc home proc root sys tmp usr var
/
/
/
bin dev etc file1 file2 home proc root sys tmp usr var
[root@server8 ~]
haha
[root@server8 ~]
/
bin dev etc home proc root sys tmp usr var
/
/
/
[root@server8 ~]
sha256:1071c9ea6e1057ea55bcf221bc4e51e256cec13003124b71bf242734823d76d2
[root@server8 ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hh1 latest 1071c9ea6e10 2 minutes ago 1.24MB
busybox latest 16ea53ea7c65 2 weeks ago 1.24MB
ubuntu latest c29284518f49 2 months ago 72.8MB
yakexi007/game2048 latest 19299002fdbe 4 years ago 55.5MB
yakexi007/mario latest 9a35a9e43e8c 6 years ago 198MB
[root@server8 ~]
IMAGE CREATED CREATED BY SIZE COMMENT
1071c9ea6e10 4 minutes ago sh 39B
16ea53ea7c65 2 weeks ago /bin/sh -c
<missing> 2 weeks ago /bin/sh -c
[root@server8 ~]
IMAGE CREATED CREATED BY SIZE COMMENT
16ea53ea7c65 2 weeks ago /bin/sh -c
<missing> 2 weeks ago /bin/sh -c
[root@server8 ~]
Untagged: hh1:latest
Deleted: sha256:1071c9ea6e1057ea55bcf221bc4e51e256cec13003124b71bf242734823d76d2
Deleted: sha256:049669e8ff1a31a236fd7f5efcde4f3fd66a41f648be794ac60142c2c22d1db3
[root@server8 ~]
IMAGE CREATED CREATED BY SIZE COMMENT
16ea53ea7c65 2 weeks ago /bin/sh -c
<missing> 2 weeks ago /bin/sh -c
3.Dockerfile 详解
[root@server8 ~]
[root@server8 ~]
[root@server8 yang]
[root@server8 yang]
FROM busybox
RUN touch file1
RUN echo "hello world"
[root@server8 yang]
[root@server8 yang]
IMAGE CREATED CREATED BY SIZE COMMENT
16ea53ea7c65 2 weeks ago /bin/sh -c
<missing> 2 weeks ago /bin/sh -c
[root@server8 yang]
/
bin dev etc file1 home proc root sys tmp usr var
Dockerfile常用命令
FROM-------->指定base镜像,如果本地不存在会从远程仓库下载。
MAINTAINER-------->设置镜像的作者,比如用户邮箱等。
COPY-------->把文件从build context复制到镜像、支持两种形式:COPY src dest 和 COPY [“src”, “dest”] src必须指定build context中的文件或目录。
ADD-------->用法与COPY类似,不同的是src可以是归档压缩文件,文件会被自动解压到dest,也可以自动下载URL并拷贝到镜像。
ENV-------->设置环境变量,变量可以被后续的指令使用。
EXPOSE-------->如果容器中运行应用服务,可以把服务端口暴露出。
VOLUME--------->申明数据卷,通常指定的是应用的数据挂在点。
WORKDIR--------->为RUN、CMD、ENTRYPOINT、ADD和COPY指令设置镜像中的当前工作目录,如果目录不存在会自动创建。
RUN------>在容器中运行命令并创建新的镜像层,常用于安装软件包。
CMD 与 ENTRYPOINT------>这两个指令都是用于设置容器启动后执行的命令,但CMD会被docker run后面的命令行覆盖,而ENTRYPOINT不会被忽略,一定会被执行。#CMD可以被更改,另一个不行。
实操
[root@server8 yang]
Dockerfile dvd.repo nginx-1.21.1.tar.gz
[root@server8 yang]
[root@server8 yang]
FROM busybox
RUN touch file1
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.21.1.tar.gz /mnt
ENV hostname server-ni-cai
EXPOSE 80
VOLUME ["/data"]
WORKDIR "/mnt/nginx-1.21.1"
CMD echo "hello, $hostname"
[root@server8 yang]
[root@server8 yang]
hello, server-ni-cai
[root@server8 yang]
CMD echo "hello, $hostname" ----》 CMD ["/bin/sh","-c","echo hello,$hostname"]
[root@server8 yang]
[root@server8 yang]
hello,server-ni-cai
[root@server8 yang]
WORKDIR "/mnt/nginx-1.21.1"
ENTRYPOINT ["/bin/echo","hello"]
CMD ["world"]
[root@server8 yang]
[root@server8 yang]
hello world
[root@server8 yang]
hello haha
四、镜像优化
1。 没有优化前
接上以实验,docker rmi 之前的容器 #不演示了
[root@server8 yang]
[root@server8 yang]
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 16ea53ea7c65 2 weeks ago 1.24MB
ubuntu latest c29284518f49 2 months ago 72.8MB
yakexi007/game2048 latest 19299002fdbe 4 years ago 55.5MB
yakexi007/mario latest 9a35a9e43e8c 6 years ago 198MB
[root@server8 yang]
Dockerfile dvd.repo nginx-1.21.1.tar.gz rhel7.tar
[root@server8 yang]
[root@server8 yang]
REPOSITORY TAG IMAGE ID CREATED SIZE
rhel7 latest 0a3eb3fde7fd 7 years ago 140MB
[root@server8 yang]
FROM rhel7
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.21.1.tar.gz /mnt
WORKDIR "/mnt/nginx-1.21.1"
RUN rpmdb --rebuilddb
RUN yum install -y gcc pcre-devel zlib-devel
RUN ./configure --prefix=/usr/local/nginx
RUN yum install -y make
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@server8 yang]
[root@server8 yang]
REPOSITORY TAG IMAGE ID CREATED SIZE
web y1 4edeb5e325d7 56 seconds ago 346MB
[root@server8 yang]
[root@server8 yang]
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f8f4ffc8092c 3 days ago 133MB
2。优化之后
[root@server8 yang]
lftp 172.25.254.111:~> cd pub/images/
lftp 172.25.254.111:/pub/images> get base-debian10.tar
[root@server8 yang]
de1602ca36c9: Loading layer [==================================================>] 3.041MB/3.041MB
1d3b68b6972f: Loading layer [==================================================>] 17.77MB/17.77MB
Loaded image: gcr.io/distroless/base-debian10:latest
[root@server8 yang]
[root@server8 yang]
[root@server8 niu-bi]
[root@server8 niu-bi]
FROM nginx as base
ARG Asia/Shanghai
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpcre.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian10
COPY --from=base /opt /
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
[root@server8 niu-bi]
[root@server8 niu-bi]
REPOSITORY TAG IMAGE ID CREATED SIZE
web y2 ea168192de26 53 seconds ago 32MB
[root@server8 niu-bi]
[root@server8 niu-bi]
a5c35403de51a36adcee7a74025bc2d4dc7a86ad415de6f55c1bae9933576210
[root@server8 niu-bi]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5c35403de51 web:y2 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp, 443/tcp demo
[root@server8 niu-bi]
[root@server8 niu-bi]
镜像常用子命令
docker + 。。。+镜像名字或id
images 显示镜像列表 history 显示镜像构建历史 commit 从容器创建镜像 build 从Dockerfile构建镜像 tag 给镜像打标签 search 搜索镜像 pull 从仓库拉取镜像 push 上传镜像到仓库 rmi 删除镜像
|