文章目录

  • 1、前言
  • 2、镜像制作
  • 2.1、Dcokerfile制作
  • 2.2、人工操作
  • 3、容器导出
  • 3.1、docker export
  • 3.2、docker save
  • 3.3、docker export 和 docker save 的区别
  • 4、上传 Docker Hub
  • 5、总结


1、前言

制作镜像有很多种方法,包括导出导入也有很多不同,这里总结一下。

2、镜像制作

2.1、Dcokerfile制作

以centos为例:

官方仓库下载的centos镜像,不带有"ll、wget、vim"等命令。我们制作镜像希望包括以上命令,并且在镜像内安装JDK1.8版本。

1、在/docker/centos目录下创建Dockerfile

[root@ centos]# touch Dockerfile

2、编辑Dockerfile

from centos
ADD jdk-8u271-linux-x64.tar.gz /mnt
# 安装vim
RUN yum -y install vim \
    # 安装ll
    && echo "alias ll='ls $LS_OPTIONS -l'" >> ~/.bashrc \
    && source ~/.bashrc \
    # 安装wget
    && yum install -y  wget \
    # jdk
    && mv /mnt/jdk1.8.0_271 /opt/jdk \
    && echo "JAVA_HOME=/opt/jdk/" >> /etc/profile \
    && echo "JAVA_BIN=/opt/jdk/bin" >> /etc/profile \
    && echo "JRE_HOME=/opt/jdk/jre" >> /etc/profile \
    && echo "CLASSPATH=/opt/jdk/jre/lib:/opt/jdk/lib:/opt/jdk/jre/lib/charsets.jar" >> /etc/profile \
    && echo "export  JAVA_HOME  JAVA_BIN JRE_HOME  PATH  CLASSPATH" >> /etc/profile \
    && source /etc/profile \
    # 增加全局变量
    && echo "export JAVA_HOME=/opt/jdk" >> ~/.bashrc \
    && echo "export PATH=$JAVA_HOME/bin:$PATH" >> ~/.bashrc \
    && echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> ~/.bashrc \
    && source ~/.bashrc

注意:

Dockerfile 的指令每执行一次都会在 docker 上新建一层。所以过多无意义的层,会造成镜像膨胀过大。

如上,以 && 符号连接命令,这样执行后,只会创建 1 层镜像。

3、下载jdk-8u271-linux-x64.tar.gz到当前目录

oracle官网下载JDK8

4、开始构建镜像

Dockerfile 文件的存放目录下,执行构建动作。

以下示例,通过目录下的 Dockerfile 构建一个 my/centos(镜像名称:镜像标签)。

:最后的 . 代表本次执行的上下文路径,最好不要在当前上下文中,放太多无关的内容,否则会一起打包给docker引擎,造成过程缓慢

[root@ centos]# docker build -t my/centos .
Sending build context to Docker daemon  359.7MB
Step 1/3 : from centos
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
 ---> 300e315adb2f
Step 2/3 : ADD jdk-8u271-linux-x64.tar.gz /mnt
 ---> 318b8479f4cb
Step 3/3 : RUN yum -y install vim     && echo "alias ll='ls $LS_OPTIONS -l'" >> ~/.bashrc     && source ~/.bashrc     && yum install -y  wget     && mv /mnt/jdk1.8.0_271 /opt/jdk     && echo "JAVA_HOME=/opt/jdk/" >> /etc/profile     && echo "JAVA_BIN=/opt/jdk/bin" >> /etc/profile     && echo "JRE_HOME=/opt/jdk/jre" >> /etc/profile     && echo "CLASSPATH=/opt/jdk/jre/lib:/opt/jdk/lib:/opt/jdk/jre/lib/charsets.jar" >> /etc/profile     && echo "export  JAVA_HOME  JAVA_BIN JRE_HOME  PATH  CLASSPATH" >> /etc/profile     && source /etc/profile     && echo "export JAVA_HOME=/opt/jdk" >> ~/.bashrc     && echo "export PATH=$JAVA_HOME/bin:$PATH" >> ~/.bashrc     && echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> ~/.bashrc     && source ~/.bashrc
.....................                           
.....................
Complete!
Failed to set locale, defaulting to C.UTF-8
Removing intermediate container 713e31314b97
 ---> 5834b2cce45e
Successfully built 5834b2cce45e
Successfully tagged my/centos:latest

5、运行容器

[root@ centos]# docker run -itd --name centos-test -p 9090:8080 -v /home/project:/home/project  my/centos

6、进入容器

[root@ centos]# docker exec -it centos-test /bin/bash

7、测试各类命令

[root@0db2d25726e8 /]# ll
total 0
lrwxrwxrwx   1 root root   7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x   5 root root 360 Dec 10 01:48 dev
drwxr-xr-x   1 root root  66 Dec 10 01:48 etc
.......
[root@0db2d25726e8 /]# wget
wget: missing URL
Usage: wget [OPTION]... [URL]...

Try `wget --help' for more options.
[root@0db2d25726e8 /]# vim
~
~                                                                                     VIM - Vi IMproved
~
~                                                                                     version 8.0.1763
.....
[root@0db2d25726e8 /]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

测试通过。

2.2、人工操作

一般的过程就是一步步操作以上Dockerfile中的命令;

1、拉取centos镜像,运行容器

[root@ centos]# docker pull centos
# 运行容器
[root@ centos]# docker run -itd --name centos-test centos
# 进入容器
[root@ centos]# docker exec -it centos-test /bin/bash

2、安装 ll 命令

[root@3575121a0425 mnt]# echo "alias ll='ls $LS_OPTIONS -l'" >> ~/.bashrc
[root@3575121a0425 mnt]# source ~/.bashrc

3、退出出容器后,复制jdk到容器内

[root@0db2d25726e8 /]# exit
exit
[root@ centos]# docker cp jdk-8u271-linux-x64.tar.gz centos-test:/mnt
# 进入容器
[root@ centos]# docker exec -it centos-test /bin/bash
[root@3575121a0425 /]# ll mnt/
total 139788
-rw-r--r-- 1 root root 143142634 Dec  8 11:58 jdk-8u271-linux-x64.tar.gz
# 解压
[root@3575121a0425 mnt]# tar xzf jdk-8u271-linux-x64.tar.gz
[root@3575121a0425 mnt]# ll
total 139788
-rw-r--r-- 1 root  root  143142634 Dec  8 11:58 jdk-8u271-linux-x64.tar.gz
drwxr-xr-x 8 10143 10143       273 Sep 16 17:14 jdk1.8.0_271
# 移动到/opt/jdk
[root@3575121a0425 mnt]# mv jdk1.8.0_271 /opt/jdk

4、安装Vim

[root@3575121a0425 mnt]# yum -y install vim

5、安装wget

[root@3575121a0425 mnt]# yum install -y  wget

6、编辑 /etc/profile

[root@3575121a0425 mnt]# vim /etc/profile
# 增加以下内容
JAVA_HOME=/opt/jdk/
JAVA_BIN=/opt/jdk/bin
JRE_HOME=/opt/jdk/jre
CLASSPATH=/opt/jdk/jre/lib:/opt/jdk/lib:/opt/jdk/jre/lib/charsets.jar
export  JAVA_HOME  JAVA_BIN JRE_HOME  PATH  CLASSPATH
[root@3575121a0425 mnt]# source /etc/profile

7、增加全局指令

[root@3575121a0425 mnt]# vim ~/.bashrc
# 增加以下内容
export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@3575121a0425 mnt]# source ~/.bashrc

8、测试

[root@3575121a0425 mnt]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

3、容器导出

上一个步骤的容器制作完成后,导出操作可以让这个容器直接复制到其他机器上使用,减少人力成本。

3.1、docker export

[root@ centos]# docker export --help

Usage:	docker export [OPTIONS] CONTAINER

Export a container's filesystem as a tar archive

Options:
  -o, --output string   Write to a file, instead of STDOUT

从帮助文档看,docker export 导出的是容器文件系统。

导出容器快照到本地文件

[root@ centos]# docker export centos-test > c.tar.gz
# 通过SSH发送到其他机器测试
[root@ centos]# scp c.tar.gz 192.168.66.99.*:/docker/centos
root@192.168.66.99's password:
c.tar.gz

192.168.66.99 机器

[root@2 centos]# ll
总用量 1094960
-rw-r--r-- 1 root root 1121223680 12月 10 10:56 c.tar.gz
# 导入并创建 test/centos 镜像
[root@2 centos]# docker import c.tar.gz test/centos
sha256:87b327bb365b81e5328e79df0d9bb04174a004afc4749d7caf32b756993394b2
[root@2 centos]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
test/centos          latest              87b327bb365b        42 seconds ago      1.11GB
# 运行容器
[root@2 centos]# docker run -itd --name centos-test -p 9090:8080 test/centos /bin/bash
# 进入容器
[root@2 centos]# docker exec -it centos-test /bin/bash
# 测试
[root@e3c130477d21 /]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

docker export 导出的文件目录如下

就是一个centos文件目录系统

[root@ test]# ll
总用量 8
lrwxrwxrwx  1 root root    7 11月  3 23:22 bin -> usr/bin
drwxr-xr-x  4 root root   43 12月 10 10:19 dev
drwxr-xr-x 52 root root 4096 12月 10 10:19 etc
drwxr-xr-x  3 root root   21 12月 10 10:19 home
lrwxrwxrwx  1 root root    7 11月  3 23:22 lib -> usr/lib
lrwxrwxrwx  1 root root    9 11月  3 23:22 lib64 -> usr/lib64
drwx------  2 root root    6 12月  5 01:37 lost+found
drwxr-xr-x  2 root root    6 11月  3 23:22 media
drwxr-xr-x  3 root root   60 12月 10 10:21 mnt
drwxr-xr-x  3 root root   17 12月 10 09:41 opt
drwxr-xr-x  2 root root    6 12月  5 01:37 proc
dr-xr-x---  2 root root  199 12月 10 10:26 root
drwxr-xr-x 11 root root  163 12月  5 01:37 run
lrwxrwxrwx  1 root root    8 11月  3 23:22 sbin -> usr/sbin
drwxr-xr-x  2 root root    6 11月  3 23:22 srv
drwxr-xr-x  2 root root    6 12月  5 01:37 sys
drwxrwxrwt  7 root root  145 12月 10 09:41 tmp
drwxr-xr-x 12 root root  144 12月  5 01:37 usr
drwxr-xr-x 20 root root  262 12月  5 01:37 var

3.2、docker save

[root@ centos]# docker save --help

Usage:	docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
  -o, --output string   Write to a file, instead of STDOUT

从帮助文档看,docker save是保存镜像的,似乎并不能保存容器,那么如果我们必须使用docker save,该如何保存容器呢?

这个时候我们就需要介绍下 docker commit

**docker commit : **从容器创建一个新的镜像。

[root@ docker]# docker commit --help

Usage:	docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

第一步:docker commit 从容器创建一个新的镜像

[root@ docker]# docker commit centos-test test/centos
[root@ docker]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
3575121a0425        my/centos            "/bin/bash"              About an hour ago   Up About an hour    0.0.0.0:9090->8080/tcp   centos-test
[root@ docker]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
test/centos          latest              c1ddd5c155ee        8 seconds ago       1.48GB
my/centos            latest              5834b2cce45e        2 hours ago         979MB
# docker commit 从容器创建镜像,比原始镜像my/centos大很多, 也比 c.tar.gz 大
[root@ centos]# du -h c.tar.gz
1.1G	c.tar.gz

第二步:docker save 保存镜像

[root@ centos]# docker save test/centos > d.tar.gz
[root@ centos]# du -h d.tar.gz
1.4G	d.tar.gz
# SSH 传输
[root@ centos]# scp d.tar.gz 192.168.66.99:/docker/centos

第三步:去192.168.66.99机器 引入镜像

[root@ centos]# docker load -i d.tar.gz
2653d992f4ef: Loading layer [==================================================>]  216.5MB/216.5MB
6a83843cebe0: Loading layer [==================================================>]  356.2MB/356.2MB
71916c33e3ca: Loading layer [==================================================>]  416.4MB/416.4MB
f343e1d51629: Loading layer [==================================================>]  499.3MB/499.3MB
Loaded image: test/centos:latest
[root@ centos]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
test/centos          latest              c1ddd5c155ee        2 hours ago         1.48GB

# 运行容器
[root@ centos]# docker run -itd --name centos-test -p 9090:8080 test/centos /bin/bash
# 进入容器
[root@ centos]# docker exec -it centos-test /bin/bash
# 测试
[root@bcb0cc519d9e /]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

3.3、docker export 和 docker save 的区别

docker save保存的是镜像(image),docker export保存的是容器(container);

docker load用来载入镜像包,docker import用来载入容器包,但两者都会恢复为镜像;

4、上传 Docker Hub

上述导出的方式,需要手动传输到其他机器,我们可以上传Docker hub,然后在其他机器直接拉取就好了。

上传镜像

[root@ centos]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
test/centos          latest              c1ddd5c155ee        6 hours ago         1.48GB
[root@ centos]# docker tag test/centos master/test1
[root@ centos]# docker push master/test1

拉取镜像

# 拉取镜像
[root@ centos]# docker pull master/test1
Using default tag: latest
latest: Pulling from master/test1
Digest: sha256:40b09fcee5817df0dacd09057542ddbd61ebdc393e70a1b48af1065e0dc79e97
Status: Downloaded newer image for master/test1:latest
docker.io/master/test1:latest
[root@ centos]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
master/test1         latest              c1ddd5c155ee        6 hours ago         1.48GB
# 运行容器
[root@ centos]# docker run -itd --name centos-test -p 9090:8080 master/test1 /bin/bash
# 进入容器
[root@ centos]# docker exec -it centos-test /bin/bash
# 验证
[root@d05d8c358c21 /]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

5、总结

以上就是全部内容。