镜像是 Docker 的三大组件之一。
Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker 会从镜像仓库下载(默认是
Docker Hub 公共注册服务器中的仓库)。
=====================
1、将容器变为镜像
docker commit <container> [repo:tag]
当我们在制作自己的镜像的时候,会在container中安装一些工具、修改配置,如果不做commit保存起来,那么container停止以后在启动,这些更改就消失了?
docker create --name myjava3 -it java /bin/bash
docker start myjava3
docker ps
docker exec -it ce36c9815ea6 /bin/bash
[root@ip-10-249-100-205 ~]# docker create --name myjava3 -it java /bin/bash
ce36c9815ea6fe1302b8237bbfd0a219c1a3b0c4bb3ad467d99c7ef9e8d14662
[root@ip-10-249-100-205 ~]# docker start myjava3
myjava3
[root@ip-10-249-100-205 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce36c9815ea6 java "/bin/bash" 18 seconds ago Up 6 seconds myjava3
1549052941a1 mysql "docker-entrypoint.sh" 3 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp mysqlsrv1
[root@ip-10-249-100-205 ~]# docker exec -it ce36c9815ea6 /bin/bash
root@ce36c9815ea6:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@ce36c9815ea6:/# mkdir wolf
root@ce36c9815ea6:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var wolf
root@ce36c9815ea6:/#
docker commit ce36c9815ea6 myjava
docker images
docker run -it myjava ls
[root@ip-10-249-100-205 ~]# docker commit ce36c9815ea6 myjava
[root@ip-10-249-100-205 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myjava latest 2e503320c91d 27 seconds ago 643.1 MB
docker.io/mysql latest f008d8ff927d 3 weeks ago 408.5 MB
docker.io/java latest d23bdf5b1b1b 12 months ago 643.1 MB
sha256:2e503320c91da2198453a2684dea4049ac2cd6e6de38f5dd7d97c2c750f4a111
[root@ip-10-249-100-205 ~]# docker run -it myjava ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr wolf
这里发现很快就创建了一个新的容器
这种做法的优点:最方便、最快速
这种做法的缺点:不规范、无法自动化(完全手工)
=====================
buildfile语法和案例
在Dockerfile中用到的命令有
FROM
FROM指定一个基础镜像, 一般情况下一个可用的 Dockerfile一定是 FROM 为第一个指令。至于image则可以是任何合理存在的image镜像。
FROM 一定是首个非注释指令 Dockerfile.
FROM 可以在一个 Dockerfile 中出现多次,以便于创建混合的images。
如果没有指定 tag ,latest 将会被指定为要使用的基础镜像版本。
MAINTAINER
这里是用于指定镜像制作者的信息
RUN
RUN命令将在当前image中执行任意合法命令并提交执行结果。命令执行提交后,就会自动执行Dockerfile中的下一个指令。
层级 RUN 指令和生成提交是符合Docker核心理念的做法。它允许像版本控制那样,在任意一个点,对image 镜像进行定制化构建。
RUN 指令缓存不会在下个命令执行时自动失效。比如 RUN apt-get dist-upgrade -y 的缓存就可能被用于下一个指令. --no-cache 标志可以被用于强制取消缓存使用。
ENV
ENV指令可以用于为docker容器设置环境变量
ENV设置的环境变量,可以使用 docker inspect命令来查看。同时还可以使用docker run --env <key>=<value>来修改环境变量。
USER
USER 用来切换运行属主身份的。Docker 默认是使用 root,但若不需要,建议切换使用者身分,毕竟 root 权限太大了,使用上有安全的风险。
WORKDIR
WORKDIR 用来切换工作目录的。Docker 默认的工作目录是/,只有 RUN 能执行 cd 命令切换目录,而且还只作用在当下下的 RUN,也就是说每一个 RUN 都是独立进行的。如果想让其他指令在指定的目录下执行,就得靠 WORKDIR。WORKDIR 动作的目录改变是持久的,不用每个指令前都使用一次 WORKDIR。
COPY
COPY 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url,<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID 。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
ADD
ADD 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url。<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
VOLUME
创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。
EXPOSE
EXPOSE 指令指定在docker允许时指定的端口进行转发。
CMD
Dockerfile.中只能有一个CMD指令。 如果你指定了多个,那么最后个CMD指令是生效的。
CMD指令的主要作用是提供默认的执行容器。这些默认值可以包括可执行文件,也可以省略可执行文件。
当你使用shell或exec格式时, CMD 会自动执行这个命令。
ONBUILD
ONBUILD 的作用就是让指令延迟執行,延迟到下一个使用 FROM 的 Dockerfile 在建立 image 时执行,只限延迟一次。
ONBUILD 的使用情景是在建立镜像时取得最新的源码 (搭配 RUN) 与限定系统框架。
ARG
ARG是Docker1.9 版本才新加入的指令。
ARG 定义的变量只在建立 image 时有效,建立完成后变量就失效消失
LABEL
定义一个 image 标签 Owner,并赋值,其值为变量 Name 的值。(LABEL Owner=$Name )
ENTRYPOINT
是指定 Docker image 运行成 instance (也就是 Docker container) 时,要执行的命令或者文件。
----------------------------
下面是一个java镜像的buildfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
RUN ./hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t leader/java .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
Trying to pull repository docker.io/nimmis/ubuntu ...
14.04: Pulling from docker.io/nimmis/ubuntu
c954d15f947c: Pull complete
c3688624ef2b: Pull complete
848fe4263b3b: Pull complete
23b4459d3b04: Pull complete
36ab3b56c8f1: Pull complete
a846ffa8449f: Pull complete
616db5a385dd: Pull complete
Digest: sha256:79abf8801761e9ed573fd5d62a2d934b39b688053776a3e49a0ecce326ddaf9c
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Running in 78f2d5a33afa
---> ae9e9e54813e
Removing intermediate container 78f2d5a33afa
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Running in d19abcfab952
---> 6102ee29b5c9
Removing intermediate container d19abcfab952
Step 4 : ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
---> Running in 958a3f6abb4a
---> ccb9aec8aab7
Removing intermediate container 958a3f6abb4a
Step 5 : RUN apt-get install -y software-properties-common && add-apt-repository ppa:openjdk-r/ppa -y && apt-get update && apt-get install -y --no-install-recommends openjdk-8-jre && rm -rf /var/lib/apt/lists/*
---> Running in ce5ac07cdb7b
Reading package lists...
Building dependency tree...
[root@ip-10-249-100-205 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
leader/java latest dd51ecfc4015 10 minutes ago 509.3 MB
myjava latest 2e503320c91d 44 minutes ago 643.1 MB
docker.io/nimmis/ubuntu 14.04 b7242adec142 13 days ago 346.4 MB
docker.io/mysql latest f008d8ff927d 3 weeks ago 408.5 MB
docker.io/java latest d23bdf5b1b1b 12 months ago 643.1 MB
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
RUN ./hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : RUN ./hello.sh
---> Running in 12d20b190c16
/bin/sh: 1: ./hello.sh: not found
The command '/bin/sh -c ./hello.sh' returned a non-zero code: 127
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
ADD hello.sh /bin/hello.sh
RUN /bin/hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
~
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : ADD hello.sh /bin/hello.sh
---> 66a47c44cc7d
Removing intermediate container 39f6eaa88e25
Step 5 : RUN /bin/hello.sh
---> Running in 0681f5d4e06d
hello world
---> 814f69156baa
Removing intermediate container 0681f5d4e06d
Step 6 : ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
---> Running in 910b9e1163ba
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
RUN curl http://baidu.com
# set default java environment variable
ADD hello.sh /bin/hello.sh
RUN /bin/hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : RUN curl http://baidu.com
---> Running in b9ec969e052b
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 81 100 81 0 0 4142 0 --:--:-- --:--:-- --:--:-- 4263
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
---> f92c8fde20db
Removing intermediate container b9ec969e052b
=====================
镜像制作中的常见问题
1、把ssh server是否应该包含到竞相里?密码是放到变量中,不安全,一些观点认为不适合把ssh server打包到镜像中。
2、一个容器究竟运行几个程序?一个容器运行一个程序比较好,当然这个没有绝对答案
3、程序参数和配置文件的问题?程序设计的问题。
4、程序log输出的问题?log通过vlom绑定的到本地磁盘写出到宿主主机,这样就可以监控log了。
5、docker友好程序架构
Etcd/zookeeper
docker image docker container docker container docker container
======================
1、查看images默认从官方下载
[root@i-r2irzkkd ~]# docker search hello-world
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/hello-world Hello World! (an example of minimal Docker... 159 [OK]
docker.io docker.io/tutum/hello-world Image to test docker deployments. Has Apac... 27 [OK]
docker.io docker.io/dockercloud/hello-world Hello World! 4 [OK]
docker.io docker.io/marcells/aspnet-hello-world ASP.NET vNext - Hello World 4 [OK]
docker.io docker.io/bonomat/nodejs-hello-world a simple nodejs hello world container 2 [OK]
docker.io docker.io/carinamarina/hello-world-app This is a sample Python web application, r... 1 [OK]
docker.io docker.io/carinamarina/hello-world-web A Python web app, running on port 5000, wh... 1 [OK]
docker.io docker.io/mikespuborg/dockercloud-hello-world Fix dockercloud-hello-world example for al... 1 [OK]
docker.io docker.io/neoxsys/hello-world hello-world container based on Ubuntu 16.0... 1 [OK]
docker.io docker.io/vegasbrianc/docker-hello-world 1 [OK]
docker.io docker.io/yesimages/hello-world exercise java hello-world app 1 [OK]
docker.io docker.io/brogency/hello-world simple docker hello world? app powered b... 0 [OK]
docker.io docker.io/crccheck/hello-world Hello World web server in under 2.5 MB 0 [OK]
docker.io docker.io/gscrivano/hello-world hello world example system container 0 [OK]
docker.io docker.io/helloworld314/hello-world-docker Hello world docker 0 [OK]
docker.io docker.io/hirotakakato/hello-world hello-world 0 [OK]
docker.io docker.io/mdzhang/docker-sinatra-hello-world (WIP) Hello World web app using Docker, Si... 0 [OK]
docker.io docker.io/n8io/hello-world A simple hello world node.js app to test d... 0 [OK]
docker.io docker.io/navycloud/hello-world Navy hello world 0 [OK]
docker.io docker.io/nirmata/hello-world 0 [OK]
docker.io docker.io/rcarun/hello-world 0 [OK]
docker.io docker.io/scottsbaldwin/docker-hello-world A simple NGINX image, serving a single HTM... 0 [OK]
docker.io docker.io/waleedsamy/hello-world-expressjs-docker Hello world express sample, return `Hello ... 0 [OK]
docker.io docker.io/wodge/docker-hello-world Hello World test for auto update to Docker... 0 [OK]
docker.io docker.io/wowgroup/hello-world Minimal web app for testing purposes 0 [OK]
[root@i-r2irzkkd ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 2608 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 28 [OK]
docker.io docker.io/jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64 / Apache / PHP / PHP M... 18 [OK]
docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 14 [OK]
docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK]
docker.io docker.io/jdeathe/centos-ssh-mysql CentOS-6 6.8 x86_64 / MySQL. 8 [OK]
docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
docker.io docker.io/nickistre/centos-lamp LAMP on centos setup 4 [OK]
docker.io docker.io/centos/mariadb55-centos7 3 [OK]
docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK]
docker.io docker.io/consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK]
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 1 [OK]
docker.io docker.io/harisekhon/centos-java Java on CentOS (OpenJDK, tags jre/jdk7-8) 1 [OK]
docker.io docker.io/timhughes/centos Centos with systemd installed and running 1 [OK]
docker.io docker.io/dmglab/centos CentOS with some extras - This is for the ... 0 [OK]
docker.io docker.io/grayzone/centos auto build for centos. 0 [OK]
docker.io docker.io/grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK]
docker.io docker.io/harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK]
docker.io docker.io/januswel/centos yum update-ed CentOS image 0 [OK]
docker.io docker.io/kz8s/centos Official CentOS plus epel-release 0 [OK]
docker.io docker.io/labengine/centos Centos image base 0 [OK]
docker.io docker.io/repositoryjp/centos Docker Image for CentOS. 0 [OK]
docker.io docker.io/smartentry/centos CentOS with smartentry 0 [OK]
docker.io docker.io/ustclug/centos USTC centos 0 [OK]
2、下载指定hello-world镜像
[root@i-r2irzkkd ~]# docker pull docker.io/hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for docker.io/hello-world:latest
docker pull docker.io/hello-world,仓库地址,docker.io,没有指明tag,默认就是latest.运行这个镜像
3、查看images
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
docker.io/hello-world latest c54a2cc56cbb 9 weeks ago 1.848 kB
4、运行镜像
docker run docker.io/hello-world
docker run docker.io/centos
[root@i-r2irzkkd ~]# docker run docker.io/hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
5、查看镜像信息
docker inspect 镜像名
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
docker.io/hello-world latest c54a2cc56cbb 9 weeks ago 1.848 kB
docker inspect docker.io/centos
docker inspect docker.io/hello-world
如下
[root@i-r2irzkkd ~]# docker inspect docker.io/centos
[
{
"Id": "sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c",
"RepoTags": [
"docker.io/centos:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2016-07-29T20:59:10.01570692Z",
"Container": "53ef97a96c409a033e214be92d67d0682ecace7ae5155c647a80aea20fb8e375",
"ContainerConfig": {
"Hostname": "66388f647a9e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) CMD [\"/bin/bash\"]"
],
"Image": "sha256:f9cad40b5c05cdea07181c7404b12d5704a943e7cd8285ec791bb6962c07d5b8",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "1.10.3",
"Author": "https://github.com/CentOS/sig-cloud-instance-images",
"Config": {
"Hostname": "66388f647a9e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:f9cad40b5c05cdea07181c7404b12d5704a943e7cd8285ec791bb6962c07d5b8",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 196734860,
"VirtualSize": 196734860,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "2",
"DeviceName": "docker-8:1-394772-d4653020ef51d99c9503416250a5cd9be7c12692a1c449a58a1ed6bf7cb8c330",
"DeviceSize": "10737418240"
}
}
}
]
[root@i-r2irzkkd ~]# docker inspect docker.io/hello-world
[
{
"Id": "sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc",
"RepoTags": [
"docker.io/hello-world:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2016-07-01T19:39:27.532838486Z",
"Container": "562cadb4d17bbf30b58ab0f6a870be60c8d36d2e401bf637f1d2d7f8afbef666",
"ContainerConfig": {
"Hostname": "c65bc554a4b7",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) CMD [\"/hello\"]"
],
"Image": "sha256:0f9bb7da10de694b2babd0c1a3b75582a0db3395625cae5ab0fe537ce1cd831e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.10.3",
"Author": "",
"Config": {
"Hostname": "c65bc554a4b7",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/hello"
],
"Image": "sha256:0f9bb7da10de694b2babd0c1a3b75582a0db3395625cae5ab0fe537ce1cd831e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1848,
"VirtualSize": 1848,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "3",
"DeviceName": "docker-8:1-394772-54b3ee6cc6589c1cda18e5c460e347510522c963aad5927d9c82ad05d3ddad65",
"DeviceSize": "10737418240"
}
}
}
]
6、删除本地镜像
docker rm <IMAGE_ID>:删除image
注意:删除镜像时,必须要先删除所依赖它的容器,容器就是运行镜像时产生的。用docker ps -l列出最后运行的容器,docker ps -a列出所有的容器,然后删除docker rm <容器id>
[root@i-r2irzkkd ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
023c9cb85f58 docker.io/hello-world "/hello" 3 seconds ago Exited (0) 2 seconds ago tiny_mccarthy
[root@i-r2irzkkd ~]# docker rm 023c9cb85f58
023c9cb85f58
[root@i-r2irzkkd ~]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Deleted: sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc
Deleted: sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
7、删除镜像常用命令
杀死所有正在运行的容器
docker kill $(docker ps -a -q)
删除所有已经停止的容器
docker rm $(docker ps -a -q)
删除所有未打 dangling 标签的镜像
docker rmi $(docker images -q -f dangling=true)
删除所有镜像
docker rmi $(docker images -q)
[root@i-r2irzkkd ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92a03ad57200 docker.io/centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago berserk_kalam
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
[root@i-r2irzkkd ~]# docker ps -a -q
92a03ad57200
[root@i-r2irzkkd ~]# docker kill $(docker ps -a -q)
Failed to kill container (92a03ad57200): Error response from daemon: Cannot kill container 92a03ad57200: Container 92a03ad572005c45ec7181ecff7c0510e83cca0b6ec6944a2aaa1d51c1a7f143 is not running
[root@i-r2irzkkd ~]# docker rmi $(docker images -q)
Untagged: docker.io/centos:latest
Deleted: sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c
Deleted: sha256:f59b7e59ceaafc8c2c7e340f5831b7e4cf36203e3aeb59317942b9dec9557ac5
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
8、创建镜像
从官方仓库中下载一个镜像,修改后,通过docker commit命令来生成一个新镜像。
从官方下载centOS的latest镜像,接近200M,下载有点慢
[root@i-r2irzkkd ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
3d8673bd162a: Pull complete
Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4
Status: Downloaded newer image for docker.io/centos:latest
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
centos就是一个操作系统,我们运行这个镜像,并进入这个镜像产生的容器,在容器的/opt下创建一个文件夹soft,然后保存为一个新镜像,
[root@i-r2irzkkd ~]# docker run -i -t docker.io/centos /bin/bash
退出后再进入
[root@i-r2irzkkd ~]# docker exec -it 83bddfe23ae7 /bin/bash
[root@1a606cbec84e /]# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1a606cbec84e /]# cd /opt
[root@1a606cbec84e opt]# ls
[root@1a606cbec84e opt]# mkdir soft
[root@1a606cbec84e opt]# exit
exit
[root@i-r2irzkkd ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a606cbec84e docker.io/centos "/bin/bash" About a minute ago Exited (0) 8 seconds ago pedantic_swirles
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 8ad522cbb1c1 6 seconds ago 196.7 MB
[root@i-r2irzkkd ~]# docker commit -m "test commit" -a "wolf" 1a606cbec84e docker.io/mycentos:0.0.1
sha256:ebfe84ea0343d85d733779518158bc09e5a0b37c79475f3df6d293d6c3c4e9fc
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mycentos 0.0.1 ebfe84ea0343 1 seconds ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 40 seconds ago 196.7 MB
查看新创建镜像的信息
[root@i-r2irzkkd ~]# docker inspect docker.io/mycentos:0.0.1
[
{
"Id": "sha256:ebfe84ea0343d85d733779518158bc09e5a0b37c79475f3df6d293d6c3c4e9fc",
"RepoTags": [
"docker.io/mycentos:0.0.1"
],
"RepoDigests": [],
"Parent": "sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c",
"Comment": "test commit",
"Created": "2016-09-05T03:00:43.326346315Z",
"Container": "1a606cbec84e4415b28c135c51f9e85f7892e10d4a09d823c2c4f67a69202607",
"ContainerConfig": {
"Hostname": "1a606cbec84e",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "docker.io/centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "1.10.3",
"Author": "wolf",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 196734890,
"VirtualSize": 196734890,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "13",
"DeviceName": "docker-8:1-394772-850654f6c1dbaaf5b6d375d11be835e0224242273911386e7d7b8288279e95bb",
"DeviceSize": "10737418240"
}
}
}
]
9、导入容器快照
docker export导出一个容器快照
[root@i-r2irzkkd ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a606cbec84e 970633036444 "/bin/bash" 10 minutes ago Exited (0) 9 minutes ago pedantic_swirles
[root@i-r2irzkkd ~]# docker export 1a606cbec84e >mycentos.tar
[root@i-r2irzkkd ~]# ls
mycentos.tar
[root@i-r2irzkkd ~]# pwd
/root
[root@i-r2irzkkd ~]# ls -l
total 199548
-rw-r--r-- 1 root root 204336128 Sep 5 11:08 mycentos.tar
[root@i-r2irzkkd ~]# ls -lh
total 195M
-rw-r--r-- 1 root root 195M Sep 5 11:08 mycentos.tar
docker import导入容器快照,生成镜像
[root@i-r2irzkkd ~]# cat mycentos.tar | docker import - test/mycentos:v1.0
sha256:dc0b220a093b5b2dd2dc6072b633963acd7a266abaad88aa65c60412b91aca9b
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 4 seconds ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 10 minutes ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 10 minutes ago 196.7 MB
使用此外也可以使用url来 docker import http://xxx.xxx.xxx/导入也可以
*注:用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。
这两者的区别在于容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,
体积也要大。此外,从容器快照文件导入时可以重新指定标签等元数据信息
10、上传镜像到私人的docker hub
在https://hub.docker.com/注册一个账号,
[root@i-r2irzkkd ~]# docker login
Username (yujin2010good): yujin2010good
Password:
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 6 hours ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 6 hours ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 6 hours ago 196.7 MB
root@i-r2irzkkd ~]# docker tag test/mycentos:v1.0 yujin2010good/mycentos
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 7 hours ago 196.7 MB
wolf/mycentos latest dc0b220a093b 7 hours ago 196.7 MB
yujin2010good/mycentos latest dc0b220a093b 7 hours ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 7 hours ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 7 hours ago 196.7 MB
[root@i-r2irzkkd ~]# docker push yujin2010good/mycentos:latest
The push refers to a repository [docker.io/yujin2010good/mycentos]
7ca4b49ba045: Pushed
latest: digest: sha256:f431af9c04df11e8182131aa886ae851d7b6a4dfc27920a1104c95b6169a2e35 size: 506
必须要先登录docke login,然后docker tag或新生成一个镜像,repository是username/xxx形式的,否则会报错,看截图,上传完毕后,docker hub上就可以看到了
如下:
[root@i-r2irzkkd ~]# docker push wolf/mycentos:latest
The push refers to a repository [docker.io/wolf/mycentos]
7ca4b49ba045: Preparing
unauthorized: authentication required
登录https://hub.docker.com/查看创建了一个镜像。
Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker 会从镜像仓库下载(默认是
Docker Hub 公共注册服务器中的仓库)。
=====================
1、将容器变为镜像
docker commit <container> [repo:tag]
当我们在制作自己的镜像的时候,会在container中安装一些工具、修改配置,如果不做commit保存起来,那么container停止以后在启动,这些更改就消失了?
docker create --name myjava3 -it java /bin/bash
docker start myjava3
docker ps
docker exec -it ce36c9815ea6 /bin/bash
[root@ip-10-249-100-205 ~]# docker create --name myjava3 -it java /bin/bash
ce36c9815ea6fe1302b8237bbfd0a219c1a3b0c4bb3ad467d99c7ef9e8d14662
[root@ip-10-249-100-205 ~]# docker start myjava3
myjava3
[root@ip-10-249-100-205 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce36c9815ea6 java "/bin/bash" 18 seconds ago Up 6 seconds myjava3
1549052941a1 mysql "docker-entrypoint.sh" 3 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp mysqlsrv1
[root@ip-10-249-100-205 ~]# docker exec -it ce36c9815ea6 /bin/bash
root@ce36c9815ea6:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@ce36c9815ea6:/# mkdir wolf
root@ce36c9815ea6:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var wolf
root@ce36c9815ea6:/#
docker commit ce36c9815ea6 myjava
docker images
docker run -it myjava ls
[root@ip-10-249-100-205 ~]# docker commit ce36c9815ea6 myjava
[root@ip-10-249-100-205 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myjava latest 2e503320c91d 27 seconds ago 643.1 MB
docker.io/mysql latest f008d8ff927d 3 weeks ago 408.5 MB
docker.io/java latest d23bdf5b1b1b 12 months ago 643.1 MB
sha256:2e503320c91da2198453a2684dea4049ac2cd6e6de38f5dd7d97c2c750f4a111
[root@ip-10-249-100-205 ~]# docker run -it myjava ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr wolf
这里发现很快就创建了一个新的容器
这种做法的优点:最方便、最快速
这种做法的缺点:不规范、无法自动化(完全手工)
=====================
buildfile语法和案例
在Dockerfile中用到的命令有
FROM
FROM指定一个基础镜像, 一般情况下一个可用的 Dockerfile一定是 FROM 为第一个指令。至于image则可以是任何合理存在的image镜像。
FROM 一定是首个非注释指令 Dockerfile.
FROM 可以在一个 Dockerfile 中出现多次,以便于创建混合的images。
如果没有指定 tag ,latest 将会被指定为要使用的基础镜像版本。
MAINTAINER
这里是用于指定镜像制作者的信息
RUN
RUN命令将在当前image中执行任意合法命令并提交执行结果。命令执行提交后,就会自动执行Dockerfile中的下一个指令。
层级 RUN 指令和生成提交是符合Docker核心理念的做法。它允许像版本控制那样,在任意一个点,对image 镜像进行定制化构建。
RUN 指令缓存不会在下个命令执行时自动失效。比如 RUN apt-get dist-upgrade -y 的缓存就可能被用于下一个指令. --no-cache 标志可以被用于强制取消缓存使用。
ENV
ENV指令可以用于为docker容器设置环境变量
ENV设置的环境变量,可以使用 docker inspect命令来查看。同时还可以使用docker run --env <key>=<value>来修改环境变量。
USER
USER 用来切换运行属主身份的。Docker 默认是使用 root,但若不需要,建议切换使用者身分,毕竟 root 权限太大了,使用上有安全的风险。
WORKDIR
WORKDIR 用来切换工作目录的。Docker 默认的工作目录是/,只有 RUN 能执行 cd 命令切换目录,而且还只作用在当下下的 RUN,也就是说每一个 RUN 都是独立进行的。如果想让其他指令在指定的目录下执行,就得靠 WORKDIR。WORKDIR 动作的目录改变是持久的,不用每个指令前都使用一次 WORKDIR。
COPY
COPY 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url,<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID 。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
ADD
ADD 将文件从路径 <src> 复制添加到容器内部路径 <dest>。
<src> 必须是想对于源文件夹的一个文件或目录,也可以是一个远程的url。<dest> 是目标容器中的绝对路径。
所有的新文件和文件夹都会创建UID 和 GID。事实上如果 <src> 是一个远程文件URL,那么目标文件的权限将会是600。
VOLUME
创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。
EXPOSE
EXPOSE 指令指定在docker允许时指定的端口进行转发。
CMD
Dockerfile.中只能有一个CMD指令。 如果你指定了多个,那么最后个CMD指令是生效的。
CMD指令的主要作用是提供默认的执行容器。这些默认值可以包括可执行文件,也可以省略可执行文件。
当你使用shell或exec格式时, CMD 会自动执行这个命令。
ONBUILD
ONBUILD 的作用就是让指令延迟執行,延迟到下一个使用 FROM 的 Dockerfile 在建立 image 时执行,只限延迟一次。
ONBUILD 的使用情景是在建立镜像时取得最新的源码 (搭配 RUN) 与限定系统框架。
ARG
ARG是Docker1.9 版本才新加入的指令。
ARG 定义的变量只在建立 image 时有效,建立完成后变量就失效消失
LABEL
定义一个 image 标签 Owner,并赋值,其值为变量 Name 的值。(LABEL Owner=$Name )
ENTRYPOINT
是指定 Docker image 运行成 instance (也就是 Docker container) 时,要执行的命令或者文件。
----------------------------
下面是一个java镜像的buildfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
RUN ./hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t leader/java .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
Trying to pull repository docker.io/nimmis/ubuntu ...
14.04: Pulling from docker.io/nimmis/ubuntu
c954d15f947c: Pull complete
c3688624ef2b: Pull complete
848fe4263b3b: Pull complete
23b4459d3b04: Pull complete
36ab3b56c8f1: Pull complete
a846ffa8449f: Pull complete
616db5a385dd: Pull complete
Digest: sha256:79abf8801761e9ed573fd5d62a2d934b39b688053776a3e49a0ecce326ddaf9c
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Running in 78f2d5a33afa
---> ae9e9e54813e
Removing intermediate container 78f2d5a33afa
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Running in d19abcfab952
---> 6102ee29b5c9
Removing intermediate container d19abcfab952
Step 4 : ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
---> Running in 958a3f6abb4a
---> ccb9aec8aab7
Removing intermediate container 958a3f6abb4a
Step 5 : RUN apt-get install -y software-properties-common && add-apt-repository ppa:openjdk-r/ppa -y && apt-get update && apt-get install -y --no-install-recommends openjdk-8-jre && rm -rf /var/lib/apt/lists/*
---> Running in ce5ac07cdb7b
Reading package lists...
Building dependency tree...
[root@ip-10-249-100-205 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
leader/java latest dd51ecfc4015 10 minutes ago 509.3 MB
myjava latest 2e503320c91d 44 minutes ago 643.1 MB
docker.io/nimmis/ubuntu 14.04 b7242adec142 13 days ago 346.4 MB
docker.io/mysql latest f008d8ff927d 3 weeks ago 408.5 MB
docker.io/java latest d23bdf5b1b1b 12 months ago 643.1 MB
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
RUN ./hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : RUN ./hello.sh
---> Running in 12d20b190c16
/bin/sh: 1: ./hello.sh: not found
The command '/bin/sh -c ./hello.sh' returned a non-zero code: 127
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
# set default java environment variable
ADD hello.sh /bin/hello.sh
RUN /bin/hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
~
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : ADD hello.sh /bin/hello.sh
---> 66a47c44cc7d
Removing intermediate container 39f6eaa88e25
Step 5 : RUN /bin/hello.sh
---> Running in 0681f5d4e06d
hello world
---> 814f69156baa
Removing intermediate container 0681f5d4e06d
Step 6 : ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
---> Running in 910b9e1163ba
[root@ip-10-249-100-205 ~]# vi Dockerfile
FROM nimmis/ubuntu:14.04
MAINTAINER nimmis <1098331428@qq.com>
# disable interactive functions
ENV DEBIAN_FRONTEND noninteractive
RUN curl http://baidu.com
# set default java environment variable
ADD hello.sh /bin/hello.sh
RUN /bin/hello.sh
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get install -y software-properties-common && \
add-apt-repository ppa:openjdk-r/ppa -y && \
apt-get update && \
apt-get install -y --no-install-recommends openjdk-8-jre && \
rm -rf /var/lib/apt/lists/*
[root@ip-10-249-100-205 ~]# docker build -t wolf .
Sending build context to Docker daemon 671.8 MB
Step 1 : FROM nimmis/ubuntu:14.04
---> b7242adec142
Step 2 : MAINTAINER nimmis <1098331428@qq.com>
---> Using cache
---> ae9e9e54813e
Step 3 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> 6102ee29b5c9
Step 4 : RUN curl http://baidu.com
---> Running in b9ec969e052b
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 81 100 81 0 0 4142 0 --:--:-- --:--:-- --:--:-- 4263
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
---> f92c8fde20db
Removing intermediate container b9ec969e052b
=====================
镜像制作中的常见问题
1、把ssh server是否应该包含到竞相里?密码是放到变量中,不安全,一些观点认为不适合把ssh server打包到镜像中。
2、一个容器究竟运行几个程序?一个容器运行一个程序比较好,当然这个没有绝对答案
3、程序参数和配置文件的问题?程序设计的问题。
4、程序log输出的问题?log通过vlom绑定的到本地磁盘写出到宿主主机,这样就可以监控log了。
5、docker友好程序架构
Etcd/zookeeper
docker image docker container docker container docker container
======================
1、查看images默认从官方下载
[root@i-r2irzkkd ~]# docker search hello-world
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/hello-world Hello World! (an example of minimal Docker... 159 [OK]
docker.io docker.io/tutum/hello-world Image to test docker deployments. Has Apac... 27 [OK]
docker.io docker.io/dockercloud/hello-world Hello World! 4 [OK]
docker.io docker.io/marcells/aspnet-hello-world ASP.NET vNext - Hello World 4 [OK]
docker.io docker.io/bonomat/nodejs-hello-world a simple nodejs hello world container 2 [OK]
docker.io docker.io/carinamarina/hello-world-app This is a sample Python web application, r... 1 [OK]
docker.io docker.io/carinamarina/hello-world-web A Python web app, running on port 5000, wh... 1 [OK]
docker.io docker.io/mikespuborg/dockercloud-hello-world Fix dockercloud-hello-world example for al... 1 [OK]
docker.io docker.io/neoxsys/hello-world hello-world container based on Ubuntu 16.0... 1 [OK]
docker.io docker.io/vegasbrianc/docker-hello-world 1 [OK]
docker.io docker.io/yesimages/hello-world exercise java hello-world app 1 [OK]
docker.io docker.io/brogency/hello-world simple docker hello world? app powered b... 0 [OK]
docker.io docker.io/crccheck/hello-world Hello World web server in under 2.5 MB 0 [OK]
docker.io docker.io/gscrivano/hello-world hello world example system container 0 [OK]
docker.io docker.io/helloworld314/hello-world-docker Hello world docker 0 [OK]
docker.io docker.io/hirotakakato/hello-world hello-world 0 [OK]
docker.io docker.io/mdzhang/docker-sinatra-hello-world (WIP) Hello World web app using Docker, Si... 0 [OK]
docker.io docker.io/n8io/hello-world A simple hello world node.js app to test d... 0 [OK]
docker.io docker.io/navycloud/hello-world Navy hello world 0 [OK]
docker.io docker.io/nirmata/hello-world 0 [OK]
docker.io docker.io/rcarun/hello-world 0 [OK]
docker.io docker.io/scottsbaldwin/docker-hello-world A simple NGINX image, serving a single HTM... 0 [OK]
docker.io docker.io/waleedsamy/hello-world-expressjs-docker Hello world express sample, return `Hello ... 0 [OK]
docker.io docker.io/wodge/docker-hello-world Hello World test for auto update to Docker... 0 [OK]
docker.io docker.io/wowgroup/hello-world Minimal web app for testing purposes 0 [OK]
[root@i-r2irzkkd ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 2608 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 28 [OK]
docker.io docker.io/jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64 / Apache / PHP / PHP M... 18 [OK]
docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 14 [OK]
docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK]
docker.io docker.io/jdeathe/centos-ssh-mysql CentOS-6 6.8 x86_64 / MySQL. 8 [OK]
docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
docker.io docker.io/nickistre/centos-lamp LAMP on centos setup 4 [OK]
docker.io docker.io/centos/mariadb55-centos7 3 [OK]
docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK]
docker.io docker.io/consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK]
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 1 [OK]
docker.io docker.io/harisekhon/centos-java Java on CentOS (OpenJDK, tags jre/jdk7-8) 1 [OK]
docker.io docker.io/timhughes/centos Centos with systemd installed and running 1 [OK]
docker.io docker.io/dmglab/centos CentOS with some extras - This is for the ... 0 [OK]
docker.io docker.io/grayzone/centos auto build for centos. 0 [OK]
docker.io docker.io/grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK]
docker.io docker.io/harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK]
docker.io docker.io/januswel/centos yum update-ed CentOS image 0 [OK]
docker.io docker.io/kz8s/centos Official CentOS plus epel-release 0 [OK]
docker.io docker.io/labengine/centos Centos image base 0 [OK]
docker.io docker.io/repositoryjp/centos Docker Image for CentOS. 0 [OK]
docker.io docker.io/smartentry/centos CentOS with smartentry 0 [OK]
docker.io docker.io/ustclug/centos USTC centos 0 [OK]
2、下载指定hello-world镜像
[root@i-r2irzkkd ~]# docker pull docker.io/hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for docker.io/hello-world:latest
docker pull docker.io/hello-world,仓库地址,docker.io,没有指明tag,默认就是latest.运行这个镜像
3、查看images
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
docker.io/hello-world latest c54a2cc56cbb 9 weeks ago 1.848 kB
4、运行镜像
docker run docker.io/hello-world
docker run docker.io/centos
[root@i-r2irzkkd ~]# docker run docker.io/hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
5、查看镜像信息
docker inspect 镜像名
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
docker.io/hello-world latest c54a2cc56cbb 9 weeks ago 1.848 kB
docker inspect docker.io/centos
docker inspect docker.io/hello-world
如下
[root@i-r2irzkkd ~]# docker inspect docker.io/centos
[
{
"Id": "sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c",
"RepoTags": [
"docker.io/centos:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2016-07-29T20:59:10.01570692Z",
"Container": "53ef97a96c409a033e214be92d67d0682ecace7ae5155c647a80aea20fb8e375",
"ContainerConfig": {
"Hostname": "66388f647a9e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) CMD [\"/bin/bash\"]"
],
"Image": "sha256:f9cad40b5c05cdea07181c7404b12d5704a943e7cd8285ec791bb6962c07d5b8",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "1.10.3",
"Author": "https://github.com/CentOS/sig-cloud-instance-images",
"Config": {
"Hostname": "66388f647a9e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:f9cad40b5c05cdea07181c7404b12d5704a943e7cd8285ec791bb6962c07d5b8",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 196734860,
"VirtualSize": 196734860,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "2",
"DeviceName": "docker-8:1-394772-d4653020ef51d99c9503416250a5cd9be7c12692a1c449a58a1ed6bf7cb8c330",
"DeviceSize": "10737418240"
}
}
}
]
[root@i-r2irzkkd ~]# docker inspect docker.io/hello-world
[
{
"Id": "sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc",
"RepoTags": [
"docker.io/hello-world:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2016-07-01T19:39:27.532838486Z",
"Container": "562cadb4d17bbf30b58ab0f6a870be60c8d36d2e401bf637f1d2d7f8afbef666",
"ContainerConfig": {
"Hostname": "c65bc554a4b7",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) CMD [\"/hello\"]"
],
"Image": "sha256:0f9bb7da10de694b2babd0c1a3b75582a0db3395625cae5ab0fe537ce1cd831e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.10.3",
"Author": "",
"Config": {
"Hostname": "c65bc554a4b7",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/hello"
],
"Image": "sha256:0f9bb7da10de694b2babd0c1a3b75582a0db3395625cae5ab0fe537ce1cd831e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1848,
"VirtualSize": 1848,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "3",
"DeviceName": "docker-8:1-394772-54b3ee6cc6589c1cda18e5c460e347510522c963aad5927d9c82ad05d3ddad65",
"DeviceSize": "10737418240"
}
}
}
]
6、删除本地镜像
docker rm <IMAGE_ID>:删除image
注意:删除镜像时,必须要先删除所依赖它的容器,容器就是运行镜像时产生的。用docker ps -l列出最后运行的容器,docker ps -a列出所有的容器,然后删除docker rm <容器id>
[root@i-r2irzkkd ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
023c9cb85f58 docker.io/hello-world "/hello" 3 seconds ago Exited (0) 2 seconds ago tiny_mccarthy
[root@i-r2irzkkd ~]# docker rm 023c9cb85f58
023c9cb85f58
[root@i-r2irzkkd ~]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Deleted: sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc
Deleted: sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
7、删除镜像常用命令
杀死所有正在运行的容器
docker kill $(docker ps -a -q)
删除所有已经停止的容器
docker rm $(docker ps -a -q)
删除所有未打 dangling 标签的镜像
docker rmi $(docker images -q -f dangling=true)
删除所有镜像
docker rmi $(docker images -q)
[root@i-r2irzkkd ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92a03ad57200 docker.io/centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago berserk_kalam
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
[root@i-r2irzkkd ~]# docker ps -a -q
92a03ad57200
[root@i-r2irzkkd ~]# docker kill $(docker ps -a -q)
Failed to kill container (92a03ad57200): Error response from daemon: Cannot kill container 92a03ad57200: Container 92a03ad572005c45ec7181ecff7c0510e83cca0b6ec6944a2aaa1d51c1a7f143 is not running
[root@i-r2irzkkd ~]# docker rmi $(docker images -q)
Untagged: docker.io/centos:latest
Deleted: sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c
Deleted: sha256:f59b7e59ceaafc8c2c7e340f5831b7e4cf36203e3aeb59317942b9dec9557ac5
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
8、创建镜像
从官方仓库中下载一个镜像,修改后,通过docker commit命令来生成一个新镜像。
从官方下载centOS的latest镜像,接近200M,下载有点慢
[root@i-r2irzkkd ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
3d8673bd162a: Pull complete
Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4
Status: Downloaded newer image for docker.io/centos:latest
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
centos就是一个操作系统,我们运行这个镜像,并进入这个镜像产生的容器,在容器的/opt下创建一个文件夹soft,然后保存为一个新镜像,
[root@i-r2irzkkd ~]# docker run -i -t docker.io/centos /bin/bash
退出后再进入
[root@i-r2irzkkd ~]# docker exec -it 83bddfe23ae7 /bin/bash
[root@1a606cbec84e /]# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1a606cbec84e /]# cd /opt
[root@1a606cbec84e opt]# ls
[root@1a606cbec84e opt]# mkdir soft
[root@1a606cbec84e opt]# exit
exit
[root@i-r2irzkkd ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a606cbec84e docker.io/centos "/bin/bash" About a minute ago Exited (0) 8 seconds ago pedantic_swirles
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 8ad522cbb1c1 6 seconds ago 196.7 MB
[root@i-r2irzkkd ~]# docker commit -m "test commit" -a "wolf" 1a606cbec84e docker.io/mycentos:0.0.1
sha256:ebfe84ea0343d85d733779518158bc09e5a0b37c79475f3df6d293d6c3c4e9fc
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mycentos 0.0.1 ebfe84ea0343 1 seconds ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 40 seconds ago 196.7 MB
查看新创建镜像的信息
[root@i-r2irzkkd ~]# docker inspect docker.io/mycentos:0.0.1
[
{
"Id": "sha256:ebfe84ea0343d85d733779518158bc09e5a0b37c79475f3df6d293d6c3c4e9fc",
"RepoTags": [
"docker.io/mycentos:0.0.1"
],
"RepoDigests": [],
"Parent": "sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c",
"Comment": "test commit",
"Created": "2016-09-05T03:00:43.326346315Z",
"Container": "1a606cbec84e4415b28c135c51f9e85f7892e10d4a09d823c2c4f67a69202607",
"ContainerConfig": {
"Hostname": "1a606cbec84e",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "docker.io/centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "1.10.3",
"Author": "wolf",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20160729",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 196734890,
"VirtualSize": 196734890,
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "13",
"DeviceName": "docker-8:1-394772-850654f6c1dbaaf5b6d375d11be835e0224242273911386e7d7b8288279e95bb",
"DeviceSize": "10737418240"
}
}
}
]
9、导入容器快照
docker export导出一个容器快照
[root@i-r2irzkkd ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a606cbec84e 970633036444 "/bin/bash" 10 minutes ago Exited (0) 9 minutes ago pedantic_swirles
[root@i-r2irzkkd ~]# docker export 1a606cbec84e >mycentos.tar
[root@i-r2irzkkd ~]# ls
mycentos.tar
[root@i-r2irzkkd ~]# pwd
/root
[root@i-r2irzkkd ~]# ls -l
total 199548
-rw-r--r-- 1 root root 204336128 Sep 5 11:08 mycentos.tar
[root@i-r2irzkkd ~]# ls -lh
total 195M
-rw-r--r-- 1 root root 195M Sep 5 11:08 mycentos.tar
docker import导入容器快照,生成镜像
[root@i-r2irzkkd ~]# cat mycentos.tar | docker import - test/mycentos:v1.0
sha256:dc0b220a093b5b2dd2dc6072b633963acd7a266abaad88aa65c60412b91aca9b
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 4 seconds ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 10 minutes ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 10 minutes ago 196.7 MB
使用此外也可以使用url来 docker import http://xxx.xxx.xxx/导入也可以
*注:用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。
这两者的区别在于容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,
体积也要大。此外,从容器快照文件导入时可以重新指定标签等元数据信息
10、上传镜像到私人的docker hub
在https://hub.docker.com/注册一个账号,
[root@i-r2irzkkd ~]# docker login
Username (yujin2010good): yujin2010good
Password:
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 6 hours ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 6 hours ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 6 hours ago 196.7 MB
root@i-r2irzkkd ~]# docker tag test/mycentos:v1.0 yujin2010good/mycentos
[root@i-r2irzkkd ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/mycentos v1.0 dc0b220a093b 7 hours ago 196.7 MB
wolf/mycentos latest dc0b220a093b 7 hours ago 196.7 MB
yujin2010good/mycentos latest dc0b220a093b 7 hours ago 196.7 MB
docker.io/mycentos 0.0.1 ebfe84ea0343 7 hours ago 196.7 MB
docker.io/centos latest 8ad522cbb1c1 7 hours ago 196.7 MB
[root@i-r2irzkkd ~]# docker push yujin2010good/mycentos:latest
The push refers to a repository [docker.io/yujin2010good/mycentos]
7ca4b49ba045: Pushed
latest: digest: sha256:f431af9c04df11e8182131aa886ae851d7b6a4dfc27920a1104c95b6169a2e35 size: 506
必须要先登录docke login,然后docker tag或新生成一个镜像,repository是username/xxx形式的,否则会报错,看截图,上传完毕后,docker hub上就可以看到了
如下:
[root@i-r2irzkkd ~]# docker push wolf/mycentos:latest
The push refers to a repository [docker.io/wolf/mycentos]
7ca4b49ba045: Preparing
unauthorized: authentication required
登录https://hub.docker.com/查看创建了一个镜像。