9.docker镜像

Docker Image

镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码、运行时库、环境变量和配置文件

Docker Hub

https://hub.docker.com/search?q=nginx Docker Hub拥有大量打包好的Docker镜像。第一选项是官方镜像,不带有仓库名。

浏览 https://hub.docker.com/_/nginx 的文档部分。

拉取镜像时可以指定tag。默认tag是latest

docker image ls

docker image pull nginx

docker image pull nginx:1

alpine版本基于alpine镜像,体积小。默认nginx镜像使用debian基础镜像

docker pull nginx:alpine

docker image ls
# REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
# nginx        1         a99a39d070bf   3 days ago   142MB
# nginx        latest    a99a39d070bf   3 days ago   142MB
# nginx        alpine    c433c51bbd66   5 days ago   40.7MB

注意image id,不同tag可以指向同一个image

Image层

Image层在使用时对用户是透明的,但是我们可以使用一些指令进行查看。

docker image history nginx:latest
# IMAGE          CREATED      CREATED BY                                      SIZE      COMMENT
# a99a39d070bf   3 days ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
#       3 days ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
#       3 days ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
#       3 days ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
#       3 days ago   /bin/sh -c #(nop) COPY file:e57eef017a414ca7…   4.62kB    
#       3 days ago   /bin/sh -c #(nop) COPY file:abbcbf84dc17ee44…   1.27kB    
#       3 days ago   /bin/sh -c #(nop) COPY file:5c18272734349488…   2.12kB    
#       3 days ago   /bin/sh -c #(nop) COPY file:7b307b62e82255f0…   1.62kB    
#       3 days ago   /bin/sh -c set -x     && addgroup --system -…   61.3MB    
#       3 days ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B        
#       3 days ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.9        0B        
#       3 days ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.23.3     0B        
#       3 days ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
#       3 days ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
#       3 days ago   /bin/sh -c #(nop) ADD file:e2398d0bf516084b2…   80.5MB    

docker image history = docker history
对于docker而言,每条指令会对应转化为一层镜像。有些镜像层并不对文件系统产生变化。

docker image pull memcached

docker image history memcached
# IMAGE          CREATED      CREATED BY                                      SIZE      COMMENT
# 381c6822efbc   3 days ago   /bin/sh -c #(nop)  CMD ["memcached"]            0B        
#       3 days ago   /bin/sh -c #(nop)  EXPOSE 11211                 0B        
#       3 days ago   /bin/sh -c #(nop)  USER memcache                0B        
#       3 days ago   /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B        
#       3 days ago   /bin/sh -c ln -s usr/local/bin/docker-entryp…   34B       
#       3 days ago   /bin/sh -c #(nop) COPY file:bf641b13ea5b37f5…   124B      
#       3 days ago   /bin/sh -c set -x   && savedAptMark="$(apt-m…   3.96MB    
#       3 days ago   /bin/sh -c #(nop)  ENV MEMCACHED_SHA1=be1690…   0B        
#       3 days ago   /bin/sh -c #(nop)  ENV MEMCACHED_VERSION=1.6…   0B        
#       4 days ago   /bin/sh -c set -eux;  apt-get update;  apt-g…   1.15MB    
#       4 days ago   /bin/sh -c groupadd --system --gid 11211 mem…   3.64MB    
#       4 days ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
#       4 days ago   /bin/sh -c #(nop) ADD file:e2398d0bf516084b2…   80.5MB 

每一层layer独立,新的镜像可以利用之前创建好的层。同样,向docker hub上传时,也只需要上传新的层。

使用image inspect查看

docker image inspect nginx

docker image inspect = docker inspect
inspect显示元数据,比如port,env,cmd,architecture…

Tag

https://hub.docker.com/search?q=mariadb 搜索mariadb,会发现除了存在多个官方镜像:

  • mariadb https://hub.docker.com/_/mariadb
  • mariadb/server https://hub.docker.com/r/mariadb/server
docker image pull mariadb/server

docker image pull mariadb

docker image ls

一个完整的docker image命名需要三个部分/:,只有docker官方的镜像可以不需要account

我们可以通过image tag给指定的镜像加上tag

docker image tag nginx s09g/nginx:testing

docker image ls

docker image push s09g/nginx:testing

docker login

样例 Dockerfile

https://github.com/nginxinc/docker-nginx/blob/master/mainline/debian/Dockerfile

#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM debian:bullseye-slim

ENV NGINX_VERSION   1.23.3

RUN set -x \
# create nginx user/group first, to be consistent throughout docker variants
    && addgroup --system --gid 101 nginx \
    && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx \
    && apt-get update \
    && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates \
    && apt-get update \
    && apt-get build-dep -y $nginxPackages \
    && apt-get install --no-install-recommends --no-install-suggests -y \
                        $nginxPackages \
                        gettext-base \
                        curl \
    && apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \
# forward request and error logs to docker log collector
    && ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log \
# create a docker-entrypoint.d directory
    && mkdir /docker-entrypoint.d

COPY docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

清理无用的docker image

docker image prune

你可能感兴趣的:(docker,java,容器)