Learning Docker Part 002 Docker 镜像实战

构建镜像

Docker提供了两种构建镜像的方法:

  1. docker commit命令
  2. Dockerfile 构建文件

docker commit

docker commit包含三个步骤:

  1. 运行容器
  2. 修改容器
  3. 将容器保存为新的镜像
# 1. 运行docker容器
root@fbo-virtual-machine:~# docker run -it ubuntu /bin/bash
# 2. 安装vim
root@533618aeb4a4:/# apt install vim -y
# 3. 保存为新镜像
root@fbo-virtual-machine:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
533618aeb4a4        ubuntu              "/bin/bash"         5 minutes ago       Up 5 minutes                            pensive_curran
root@fbo-virtual-machine:~# docker commit pensive_curran ubuntu-with-vim
sha256:63cdf5e493abb21ee80fe1b2509b89d1d9b27cd3d3774eaf793cfbbe6e69f998
root@fbo-virtual-machine:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-vim     latest              63cdf5e493ab        10 seconds ago      217MB
ubuntu              latest              8b72bba4485f        5 days ago          120MB
httpd               latest              6b4e03d65aa3        5 days ago          177MB
centos              latest              328edcd84f1b        6 weeks ago         193MB
# 4. 用新的镜像启动容器
root@fbo-virtual-machine:~# docker run -it ubuntu-with-vim /bin/bash
root@da4b749d2e60:/# which vim
/usr/bin/vim

虽然docker commit可以创建新的镜像,但是docker并不建议用户通过这种方式构建镜。
原因如下:

  1. 这是一种手工创建镜像的方式,容易出错,效率低且可重复性弱。
  2. 使用者并不知道镜像是如何创建的,里面是否有恶意程序。也就是说无法对镜像进行审计,存在安全隐患。

Dockerfile构造镜像

# 1. 查看Dockerfile内容及位置
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM ubuntu
RUN apt-get update && apt-get install -y vim
root@fbo-virtual-machine:~/docker-files# pwd
/root/docker-files
root@fbo-virtual-machine:~/docker-files# ls
Dockerfile
# 2. 运行docker build命令, -t 指定新的镜像的名称 .指当前目录 -f 可以指定Dockerfile文件位置
root@fbo-virtual-machine:~/docker-files# docker build -t ubuntu-with-vim .
# 3. docker将build context发送给Docker daemon。build context为镜像构建提供所需要的文件和目录。此例子中
# /root/docker-files/下的所有文件和子目录都会发送Docker Daemon。
Sending build context to Docker daemon  2.048kB
# 4. 执行from 将ubuntu做为base镜像,ubuntu镜像id为8b72bba4485f
Step 1/2 : FROM ubuntu
 ---> 8b72bba4485f
# 5. 执行RUN 安装vim
Step 2/2 : RUN apt-get update && apt-get install -y vim
# 6. 启动id为5e3a66a39881的临时容器
 ---> Running in 5e3a66a39881
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [48.9 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [445 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [208 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2930 B]
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [218 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [811 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.6 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [683 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [17.5 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5177 B]
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6236 B]
Fetched 24.4 MB in 39s (611 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal
  libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim-common
  vim-runtime
Suggested packages:
  gpm ctags vim-doc vim-scripts vim-gnome-py2 | vim-gtk-py2 | vim-gtk3-py2
  | vim-athena-py2 | vim-nox-py2
The following NEW packages will be installed:
  file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal
  libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim vim-common
  vim-runtime
0 upgraded, 14 newly installed, 0 to remove and 2 not upgraded.
Need to get 12.2 MB of archives.
After this operation, 58.3 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libgpm2 amd64 1.20.4-6.1 [16.5 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/main amd64 libmagic1 amd64 1:5.25-2ubuntu1 [216 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial/main amd64 file amd64 1:5.25-2ubuntu1 [21.2 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1 amd64 2.1.0-7ubuntu0.16.04.3 [71.2 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 libmpdec2 amd64 2.4.2-1 [82.6 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libssl1.0.0 amd64 1.0.2g-1ubuntu4.8 [1081 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-minimal amd64 3.5.2-2ubuntu0~16.04.1 [526 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/main amd64 mime-support all 3.59ubuntu1 [31.0 kB]
Get:9 http://archive.ubuntu.com/ubuntu xenial/main amd64 libsqlite3-0 amd64 3.11.0-1ubuntu1 [396 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-stdlib amd64 3.5.2-2ubuntu0~16.04.1 [2130 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim-common amd64 2:7.4.1689-3ubuntu1.2 [103 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5 amd64 3.5.2-2ubuntu0~16.04.1 [1360 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim-runtime all 2:7.4.1689-3ubuntu1.2 [5164 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim amd64 2:7.4.1689-3ubuntu1.2 [1036 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 12.2 MB in 19s (643 kB/s)
Selecting previously unselected package libgpm2:amd64.
(Reading database ... 4768 files and directories currently installed.)
Preparing to unpack .../libgpm2_1.20.4-6.1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.4-6.1) ...
Selecting previously unselected package libmagic1:amd64.
Preparing to unpack .../libmagic1_1%3a5.25-2ubuntu1_amd64.deb ...
Unpacking libmagic1:amd64 (1:5.25-2ubuntu1) ...
Selecting previously unselected package file.
Preparing to unpack .../file_1%3a5.25-2ubuntu1_amd64.deb ...
Unpacking file (1:5.25-2ubuntu1) ...
Selecting previously unselected package libexpat1:amd64.
Preparing to unpack .../libexpat1_2.1.0-7ubuntu0.16.04.3_amd64.deb ...
Unpacking libexpat1:amd64 (2.1.0-7ubuntu0.16.04.3) ...
Selecting previously unselected package libmpdec2:amd64.
Preparing to unpack .../libmpdec2_2.4.2-1_amd64.deb ...
Unpacking libmpdec2:amd64 (2.4.2-1) ...
Selecting previously unselected package libssl1.0.0:amd64.
Preparing to unpack .../libssl1.0.0_1.0.2g-1ubuntu4.8_amd64.deb ...
Unpacking libssl1.0.0:amd64 (1.0.2g-1ubuntu4.8) ...
Selecting previously unselected package libpython3.5-minimal:amd64.
Preparing to unpack .../libpython3.5-minimal_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package mime-support.
Preparing to unpack .../mime-support_3.59ubuntu1_all.deb ...
Unpacking mime-support (3.59ubuntu1) ...
Selecting previously unselected package libsqlite3-0:amd64.
Preparing to unpack .../libsqlite3-0_3.11.0-1ubuntu1_amd64.deb ...
Unpacking libsqlite3-0:amd64 (3.11.0-1ubuntu1) ...
Selecting previously unselected package libpython3.5-stdlib:amd64.
Preparing to unpack .../libpython3.5-stdlib_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a7.4.1689-3ubuntu1.2_amd64.deb ...
Unpacking vim-common (2:7.4.1689-3ubuntu1.2) ...
Selecting previously unselected package libpython3.5:amd64.
Preparing to unpack .../libpython3.5_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a7.4.1689-3ubuntu1.2_all.deb ...
Adding 'diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:7.4.1689-3ubuntu1.2) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a7.4.1689-3ubuntu1.2_amd64.deb ...
Unpacking vim (2:7.4.1689-3ubuntu1.2) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Setting up libgpm2:amd64 (1.20.4-6.1) ...
Setting up libmagic1:amd64 (1:5.25-2ubuntu1) ...
Setting up file (1:5.25-2ubuntu1) ...
Setting up libexpat1:amd64 (2.1.0-7ubuntu0.16.04.3) ...
Setting up libmpdec2:amd64 (2.4.2-1) ...
Setting up libssl1.0.0:amd64 (1.0.2g-1ubuntu4.8) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up mime-support (3.59ubuntu1) ...
Setting up libsqlite3-0:amd64 (3.11.0-1ubuntu1) ...
Setting up libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up vim-common (2:7.4.1689-3ubuntu1.2) ...
Setting up libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up vim-runtime (2:7.4.1689-3ubuntu1.2) ...
Setting up vim (2:7.4.1689-3ubuntu1.2) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu9) ...
# 7. 安装成功后,将容器保存为id 4b11a920d1d1的镜像
 ---> 4b11a920d1d1
# 8. 删除临时容器
Removing intermediate container 5e3a66a39881
# 9. 构建成功
Successfully built 4b11a920d1d1
Successfully tagged ubuntu-with-vim:latest

docker history会显示镜像的构建历史,也就是Dockerfile的执行过程。

root@fbo-virtual-machine:~/docker-files# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-vim     latest              4b11a920d1d1        22 minutes ago      217MB
ubuntu              latest              8b72bba4485f        5 days ago          120MB
httpd               latest              6b4e03d65aa3        5 days ago          177MB
centos              latest              328edcd84f1b        6 weeks ago         193MB
root@fbo-virtual-machine:~/docker-files# docker history ubuntu-with-vim
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4b11a920d1d1        22 minutes ago      /bin/sh -c apt-get update && apt-get insta...   96.5MB              
8b72bba4485f        5 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           5 days ago          /bin/sh -c mkdir -p /run/systemd && echo '...   7B                  
<missing>           5 days ago          /bin/sh -c sed -i 's/^#\s*\(deb.*universe\...   2.76kB              
<missing>           5 days ago          /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           5 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' >...   745B                
<missing>           5 days ago          /bin/sh -c #(nop) ADD file:39d3593ea220e68...   120MB     

镜像的缓存特性

Docker 会缓存已有的镜像层,构建新的镜像时,如果某层镜像已经存在,就直接使用,无需创建。
example:

# 1. 修改Dockerfile,添加一行
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM ubuntu
RUN apt-get update && apt-get install -y vim
COPY testfile /
# 2. 运行docker build,没有全部从新构建镜像,直接调用缓存镜像4b11a920d1d1
root@fbo-virtual-machine:~/docker-files# docker build -t ubuntu-with-vi-2 .
Sending build context to Docker daemon   2.56kB
Step 1/3 : FROM ubuntu
 ---> 8b72bba4485f
Step 2/3 : RUN apt-get update && apt-get install -y vim
 ---> Using cache
 ---> 4b11a920d1d1
Step 3/3 : COPY testfile /
 ---> af5a360ab7a9
Removing intermediate container 4079ecd04817
Successfully built af5a360ab7a9
Successfully tagged ubuntu-with-vi-2:latest

如果我们希望在构建镜像时不使用缓存,可以在docker build 命令中加上--no-cache参数。

Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。

也就是说,如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。如上例子,修改COPY和RUN的语句位置,会全新构建镜像。

除了在构建镜像时使用缓存,Docker在下载镜像时也会使用。example:

root@fbo-virtual-machine:~/docker-files# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
# 这里会提示base镜像已经存在,跳过下载
aa18ad1a0d33: Already exists 
2b28e4afdec2: Pull complete 
802b6cd5ed3b: Pull complete 
6f2336b7c318: Pull complete 
d7c441746c9e: Pull complete 
ba7f19f905f9: Pull complete 
5c7522be7faf: Pull complete 
Digest: sha256:a46bd62f5286321ed19875778246f3afe97df5f9e91f0dd369a6ecfa529dbe81
Status: Downloaded newer image for httpd:latest

调试Dockerfile

包括 Dockerfile 在内的任何脚本和程序都会出错。有错并不可怕,但必须有办法排查

# 1. 编写Dockerfile
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM busybox
RUN touch tmpfile
run /bin/bash -c echo "continue to build..."
copy testfile /
# 2. 执行dokcer build报错
root@fbo-virtual-machine:~/docker-files# docker build -t image-debug .
Sending build context to Docker daemon   2.56kB
Step 1/4 : FROM busybox
latest: Pulling from library/busybox
03b1be98f3f9: Pull complete 
Digest: sha256:99ccecf3da28a93c063d5dddcdf69aeed44826d0db219aabc3d5178d47649dfa
Status: Downloaded newer image for busybox:latest
 ---> 54511612f1c4
Step 2/4 : RUN touch tmpfile
 ---> Running in 07f7a9e8f1ea
 ---> 749cc37a2111
Removing intermediate container 07f7a9e8f1ea
Step 3/4 : RUN /bin/bash -c echo "continue to build..."
 ---> Running in 5b7d576cc8f6
/bin/sh: /bin/bash: not found
The command '/bin/sh -c /bin/bash -c echo "continue to build..."' returned a non-zero code: 127
# 3. 进入缓存镜像调试
root@fbo-virtual-machine:~/docker-files# docker run -it 749cc37a2111
/ # /bin/bash -c echo "continue to build..."
sh: /bin/bash: not found
/ # exit

Dockerfile常用指令

Dockerfile常用指令

  • FROM - 指定base镜像
  • MAINTAINER - 镜像作者
  • COPY - 将文件从build context复制到镜像。
    • COPY src dest
    • COPY ["src","dest"]
  • ADD - 与COPY类似,不同的是如果src是归档文件(tar、zip、tgz、xz等),文件会自动解压到dest。
  • ENV - 设置环境变量
  • EXPOSE - 指定容器中的进程会监听某个端口
  • VOLUME - 将文件或目录声明为volume
  • WORKDIR - 为后面RUN,CDM,ENTRYPOINT,ADD或COPY是指镜像中的当前工作目录。
  • RUN - 在容器中运行指定命令
  • CMD - 容器启动时运行指定命令,当有多个CMD指令时,只有最有一个生效。CMD可以被docker run之后的参数替换
  • ENTRYPOINT - 设置启动时运行的命令,当有多个ENTRYPOINT指令是,只有最后一个生效。CMD或docker run 之后的参数会被当做参数传递给ENTRYPOINT。
## 1. 创建Dockerfile
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
# my dockerfile
FROM busybox
MAINTAINER recoba01@163.com
WORKDIR /testdir
RUN touch tmpfile1
COPY ["testfile","."]
ADD ["fbo.tar.gz", "."]
ENV WELCOME "You are in my container, welcome!"
## 2. 确认工作环境
root@fbo-virtual-machine:~/docker-files# ls
Dockerfile  fbo.tar.gz  testfile
## 3. 构建my-image镜像
root@fbo-virtual-machine:~/docker-files# docker build -t my-image .
Sending build context to Docker daemon  3.584kB
Step 1/7 : FROM busybox
 ---> 54511612f1c4
Step 2/7 : MAINTAINER recoba01@163.com
 ---> Running in 345098f14ab3
 ---> 2bf416443eed
Removing intermediate container 345098f14ab3
Step 3/7 : WORKDIR /testdir
 ---> eca59be9c015
Removing intermediate container 3a343c30b0ff
Step 4/7 : RUN touch tmpfile1
 ---> Running in f9c7f78f2586
 ---> c3ebd76a7ba4
Removing intermediate container f9c7f78f2586
Step 5/7 : COPY testfile .
 ---> 8eb505c80d42
Removing intermediate container 4d22491de73a
Step 6/7 : ADD fbo.tar.gz .
 ---> 5a5e8200f7a4
Removing intermediate container bfe0b9e0d6c4
Step 7/7 : ENV WELCOME "You are in my container, welcome!"
 ---> Running in 83e9ef179ea6
 ---> 809d654b00fc
Removing intermediate container 83e9ef179ea6
Successfully built 809d654b00fc
Successfully tagged my-image:latest
## 检查镜像内容
root@fbo-virtual-machine:~/docker-files# docker run -it my-image
/testdir # pwd
/testdir
/testdir # ls
fbo       testfile  tmpfile1
/testdir # echo $WELCOME
You are in my container, welcome!

RUN & CMD & ENTRYPOINT

RUN、CMD和ENTRYPOINT功能有些相似,它们有以下区别:

  1. RUN执行命令并创建新的镜像层,RUN常用于安装软件包。
  2. CMD是指容器启动后默认执行的命令及参数,但是CMD能够被docker run后面跟的命令行参数替换。
  3. ENTRYPOINT配置容器启动时运行的命令。

使用公共镜像

## 1. 登录到docker hub
root@fbo-virtual-machine:~# docker login -u drfung
Password: 
Login Succeeded

## 2. 为镜像打上tag
root@fbo-virtual-machine:~# docker tag ubuntu drfung/ubuntu:v1
root@fbo-virtual-machine:~# docker images drfung/ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
drfung/ubuntu       v1                  8b72bba4485f        6 days ago          120MB
## 3. 上传镜像到docker hub
root@fbo-virtual-machine:~# docker push drfung/ubuntu
The push refers to a repository [docker.io/drfung/ubuntu]
3996d0debc49: Mounted from library/ubuntu 
cd1d6655b4e4: Mounted from library/ubuntu 
a76db6d8fac4: Mounted from library/ubuntu 
ebf3d6975c70: Mounted from library/ubuntu 
8aa4fcad5eeb: Mounted from library/ubuntu 
v1: digest: sha256:bb5815009271d6f80a64677b4bb60b799c483e839b4d03c2f7723baabaca03df size: 1357

登录 https://hub.docker.com,在Public Repository 中就可以看到上传的镜像,这个镜像可被其他Docker host下载使用。


搭建本地 Registry

-d 后台启动容器
-p 将容器的5000端口映射到host的5000端口
-v 将容器/var/lib/registry目录映射到host的/myregistry,用于存放镜像数据

## 1. 安装本地registry
root@fbo-virtual-machine:~# docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
90f4dba627d6: Pull complete 
b3e11d7b4f5e: Pull complete 
1f032f3c8932: Pull complete 
425585e7aedb: Pull complete 
f45f535a83d2: Pull complete 
Digest: sha256:0f8fe61fa337b8ef02217702ba979b47a7d68717d4628f31592ebff85915f3ba
Status: Downloaded newer image for registry:2
43a490f2c2ba503f02daade785772334ad9e7df6d405681af7a6844b1a74307d
## 2. 打tag
root@fbo-virtual-machine:~# docker tag drfung/ubuntu:v1 fbo-virtual-machine:5000/drfung/ubuntu:v1
## 3. 上传本地镜像
root@fbo-virtual-machine:~# docker push fbo-virtual-machine:5000/drfung/ubuntu:v1
The push refers to a repository [fbo-virtual-machine:5000/drfung/ubuntu]
3996d0debc49: Pushed 
cd1d6655b4e4: Pushed 
a76db6d8fac4: Pushed 
ebf3d6975c70: Pushed 
8aa4fcad5eeb: Pushed 
v1: digest: sha256:c166b406b66c122236d1e0e7f1afb50611cec281827c525f9bf9a67d5c8b003f size: 1357
## 4. 下载本地镜像
root@fbo-virtual-machine:~# docker pull fbo-virtual-machine:5000/drfung/ubuntu:v1
v1: Pulling from drfung/ubuntu
Digest: sha256:c166b406b66c122236d1e0e7f1afb50611cec281827c525f9bf9a67d5c8b003f
Status: Downloaded newer image for fbo-virtual-machine:5000/drfung/ubuntu:v1

镜像小结

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

推荐阅读更多精彩内容