Docker镜像和容器

理解镜像和容器

Docker引擎提供实现镜像和容器的核心技术。镜像是在运行时使用的文件系统和参数集,它没有状态并且永远不会改变。容器是镜像的运行实例。当运行命令时,Docker引擎:

  • 检查是否有镜像文件
  • 从Docker Hub下载映像
  • 将镜像加载到容器中并“运行”它

Docker Hub:https://hub.docker.com/

运行 whalesay镜像

Docker Hub 主页:https://hub.docker.com/r/docker/whalesay/

$ sudo docker run docker/whalesay cowsay boo
Unable to find image 'docker/whalesay:latest' locally
latest: Pulling from docker/whalesay
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
00bf65475aba: Pull complete 
c57b6bcc83e3: Pull complete 
8978f6879e2f: Pull complete 
8eed3712d2cf: Pull complete 
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
 _____ 
< boo >
 ----- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/ 
$ docker images  //显示所有镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              48b5124b2768        2 months ago        1.84 kB
docker/whalesay     latest              6b362a9f73eb        22 months ago       247 MB
$ sudo docker run docker/whalesay cowsay Hello, Joe.Ye  //修改参数重新运行镜像
 _______________ 
< Hello, Joe.Ye >
 --------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/

构建镜像

创建一个Dockerfile

A Dockerfile is a recipe which describes the files, environment, and commands that make up an image.

$ mkdir mydockerbuild
$ cd mydockerbuild
$ vim Dockerfile
    FROM docker/whalesay:latest
    MAINTAINER Joe.Ye <yezhou@yezhou.org>

    RUN apt-get -y update && apt-get install -y fortunes
    RUN git clone https://github.com/ruanyf/fortunes.git
    RUN cp fortunes/data/* /usr/share/games/fortunes/
    CMD /usr/games/fortune -a | cowsay

早在1979年,就有人写了一个叫做 fortune 的小程序。在命令行下输入fortune,就会跳出一句。

fortune中文格言库:https://github.com/ruanyf/fortunes

$ git clone https://github.com/ruanyf/fortunes.git
$ sudo cp fortunes/data/* /usr/share/games/fortunes/

从Dockerfile编译镜像

$ sudo docker build -t docker-whale .
  • -t 为镜像指定tag
  • . 表示当前目录
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-whale        latest              8fff85eaeacf        4 minutes ago       256 MB
<none>              <none>              d3ab35128e41        9 minutes ago       247 MB
hello-world         latest              48b5124b2768        2 months ago        1.84 kB
docker/whalesay     latest              6b362a9f73eb        22 months ago       247 MB

$ docker run docker-whale  //运行

创建 Docker Hub 账号和仓库并发布镜像

注册:https://hub.docker.com/register/

Tag and push the image

$ sudo docker images  //找到 docker-whale镜像的Tag
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-whale        latest              8fff85eaeacf        33 minutes ago      256 MB
<none>              <none>              d3ab35128e41        38 minutes ago      247 MB
hello-world         latest              48b5124b2768        2 months ago        1.84 kB
docker/whalesay     latest              6b362a9f73eb        22 months ago       247 MB
$ sudo docker tag 8fff85eaeacf yezhou/docker-whale:latest  //Tag the docker-whale image
$ sudo docker images  //确认 docker-whale镜像已被Tagged
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker-whale          latest              8fff85eaeacf        37 minutes ago      256 MB
yezhou/docker-whale   latest              8fff85eaeacf        37 minutes ago      256 MB
<none>                <none>              d3ab35128e41        41 minutes ago      247 MB
hello-world           latest              48b5124b2768        2 months ago        1.84 kB
docker/whalesay       latest              6b362a9f73eb        22 months ago       247 MB

登录并发布镜像

$ sudo docker login
Username: yezhou
Password: 
Login Succeeded
$ sudo docker push yezhou/docker-whale
time="2017-03-21T20:22:50+08:00" level=info msg="Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows"
The push refers to a repository [docker.io/yezhou/docker-whale]
c04df8c4e2c7: Pushed
0f52c7601381: Layer already exists
7e86c6a40f66: Pushed
5f70bf18a086: Layer already exists
d061ee1340ec: Layer already exists
d511ed9e12e1: Layer already exists
091abc5148e4: Layer already exists
b26122d57afa: Layer already exists
37ee47034d9b: Layer already exists
528c8710fd95: Layer already exists
1154ba695078: Pushed
latest: digest: sha256:090cbc4196976c9a575ccc78150f0460593c709c3e3fa5cbc32e12fa088f4ab9 size: 3036

拉取新发布的镜像

docker rmi id  //根据id删除镜像
docker rm  //删除容器

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/docker-image-and-container/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Docker镜像和容器
理解镜像和容器 Docker引擎提供实现镜像和容器的核心技术。镜像是在运行时使用的文件系统和参数集,它没有状态并且永远不会改变。容器是镜像的运行实例。当运……
<<上一篇
下一篇>>
文章目录
关闭
目 录