• Docker容器技术


    一、docker部署

    仓库部署:

    1. cd /etc/yum.repos.d/
    2. vim docker.repo
    3. [docker]
    4. name=docker-ce
    5. baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
    6. gpgcheck=0
    7. [update]
    8. name=centos
    9. baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/
    10. gpgcheck=0

    安装docker-ce

    1. yum install -y docker-ce
    2. systemctl enable --now docker
    3. docker info

    激活内核选项

    1. vim /etc/sysctl.d/docker.conf
    2. net.bridge.bridge-nf-call-iptables = 1
    3. net.bridge.bridge-nf-call-ip6tables = 1
    4. net.ipv4.ip_forward = 1
    5. sysctl --system
    6. systemctl restart docker

    启动容器

    1. docker run -d --name web1 -p 80:80 nginx
    2. docker ps

    删除容器 查询镜像 拉取镜像到本地 启动容器 

    1. docker rm -f web1
    2. docker search yakexi007
    3. docker pull yakexi007/game2048
    4. docker run -d --name web1 -p 80:80 yakexi007/game2048
    5. docker rm -f web1

    二、docker镜像

    1、镜像分层结构

    删除全部镜像
    docker rmi `docker images | grep -v REPOSITORY | awk '{print $1":"$2}'`

    2、镜像构建

    docker commit

    这种方式不利于安全审计

    1. docker run -it --name demo busybox
    2. touch file1
    3. touch file2
    4. ctrl+d: 退出容器后容器自动关闭
    5. ctrl+pq: 退出容器后继续在后台运行
    6. docker ps -a
    7. docker start demo
    8. docker attach demo
    9. docker commit -m "add files" demo demo:v1 提交容器变更到新的镜像
    10. docker history demo:v1
    11. docker rmi demo:v1

    Dockerfile

    1. mkdir docker
    2. cd docker/
    3. cp ~/nginx-1.23.3.tar.gz .
    4. vim Dockerfile
    5. FROM centos:7
    6. ADD nginx-1.23.3.tar.gz /mnt
    7. WORKDIR /mnt/nginx-1.23.3
    8. RUN yum install -y gcc make pcre-devel openssl-devel
    9. RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
    10. RUN ./configure --with-http_ssl_module --with-http_stub_status_module
    11. RUN make
    12. RUN make install
    13. EXPOSE 80
    14. VOLUME ["/usr/local/nginx/html"]
    15. CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

    构建镜像

    1. docker build -t webserver:v1 .
    2. docker images webserver
    3. docker history webserver:v1

    启动容器等

    1. docker run -d --name web1 webserver:v1
    2. docker inspect web1

    数据卷挂载

    自动分配的ip

    访问容器

    curl 172.17.0.2

    3、镜像优化

    缩减镜像层

    1. vim Dockerfile
    2. FROM centos:7 as build
    3. ADD nginx-1.23.3.tar.gz /mnt
    4. WORKDIR /mnt/nginx-1.23.3
    5. RUN yum install -y gcc make pcre-devel openssl-devel && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && cd .. && rm -fr nginx-1.23.3 && yum clean all
    6. EXPOSE 80
    7. VOLUME ["/usr/local/nginx/html"]
    8. CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
    9. docker build -t webserver:v2 .
    10. docker images webserver

    多阶段构建

    1. vim Dockerfile
    2. FROM centos:7 as build
    3. ADD nginx-1.23.3.tar.gz /mnt
    4. WORKDIR /mnt/nginx-1.23.3
    5. RUN yum install -y gcc make pcre-devel openssl-devel && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && cd .. && rm -fr nginx-1.23.3 && yum clean all
    6. FROM centos:7
    7. COPY --from=build /usr/local/nginx /usr/local/nginx
    8. EXPOSE 80
    9. VOLUME ["/usr/local/nginx/html"]
    10. CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
    11. docker build -t webserver:v3 .
    12. docker images webserver

    使用最精简的基础镜像

    1. docker load -i base-debian11.tar
    2. mkdir new
    3. cd new/
    4. vim Dockerfile
    5. FROM nginx:1.23.3 as base
    6. # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
    7. ARG TIME_ZONE
    8. RUN mkdir -p /opt/var/cache/nginx && \
    9. cp -a --parents /usr/lib/nginx /opt && \
    10. cp -a --parents /usr/share/nginx /opt && \
    11. cp -a --parents /var/log/nginx /opt && \
    12. cp -aL --parents /var/run /opt && \
    13. cp -a --parents /etc/nginx /opt && \
    14. cp -a --parents /etc/passwd /opt && \
    15. cp -a --parents /etc/group /opt && \
    16. cp -a --parents /usr/sbin/nginx /opt && \
    17. cp -a --parents /usr/sbin/nginx-debug /opt && \
    18. cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
    19. cp -a --parents /usr/lib/x86_64-linux-gnu/libpcre* /opt && \
    20. cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
    21. cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
    22. cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
    23. cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
    24. cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
    25. cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
    26. cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
    27. cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
    28. FROM gcr.io/distroless/base-debian11
    29. COPY --from=base /opt /
    30. EXPOSE 80 443
    31. ENTRYPOINT ["nginx", "-g", "daemon off;"]
    32. docker build -t webserver:v4 .
    33. docker images webserver
    34. docker run -d --name web1 webserver:v4
    35. curl 172.17.0.2

  • 相关阅读:
    三、python基础——六大基本数据类型
    C++类的默认成员函数
    Go SSE Demo
    LVS-NAT之VMNET环境搭建
    语音识别与自然语言处理(NLP):技术前沿与未来趋势
    vue自动跳转模拟登陆
    CSS media属性的使用-兼容不同设备不同屏幕宽度的写法
    PostgreSQL配置主从备份(docker)
    电脑换cpu要重装系统吗
    陪诊系统|陪诊软件开发|陪诊系统搭建功能
  • 原文地址:https://blog.csdn.net/weixin_56744753/article/details/134084565