Docker 镜像使用
使用的镜像在本地中不存在,docker 自动从 docker 镜像仓库中下载 默认从 Docker Hub 公共镜像源下载
1、管理和使用本地 Docker 主机镜像
2、创建镜像
列出镜像列表
docker images 来列出本地主机上的镜像。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
各个选项说明:
REPOSITORY 镜像的仓库源TAG 镜像的标签
IMAGE ID 镜像ID
CREATED 镜像创建时间
SIZE 镜像大小
同一仓库源可有多个 TAG,代表这个仓库源的不同个版本,用 REPOSITORY:TAG 来定义不同的镜像
要使用版本为15.10的ubuntu系统镜像来运行容器时
$ docker run -t -i ubuntu:15.10 /bin/bash
不指定镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像
docker删除镜像
docker image rm IMAGEID号 比如$ docker image rm 90d5884b1ee0
获取一个新的镜像
本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像
想预先下载这个镜像,用 docker pull 命令来下载
$ docker pull ubuntu:13.10 13.10: Pulling from library/ubuntu 6599cadaf950: Pull complete Status: Downloaded newer image for ubuntu:13.10
下载完成后直接使用这个镜像来运行容器
查找镜像
从 Docker Hub 网站来搜索镜像Docker Hub 网址 //hub.docker.com/
可用 docker search 命令搜索镜像
如httpd的镜像来作为的web服务
可以通过 docker search 命令搜索 httpd 来寻找适合的镜像
$ docker search httpd
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布
拖取镜像
命令 docker pull 下载镜像
$ docker pull httpd Using default tag: latest latest: Pulling from library/httpd 8b87079b7a06: Pulling fs layer
下载完成后,就可以使用这个镜像了。
$ docker run httpd
创建镜像
当从docker镜像仓库中下载的镜像不能满足的需求时,可以通过以下两种方式对镜像进行更改
1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前 需要使用镜像来创建一个容器$ docker run -t -i ubuntu:15.10 /bin/bash在运行的容器内使用 apt-get update 命令进行更新
完成后输入 exit命令来退出容器
此时ID为e218edb10161的容器,是按的需求更改的容器
通过命令 docker commit 提交容器副本
$ docker commit -m="has update" -a="facesoho" e218edb10161 facesoho/ubuntu:v2
参数说明:
-m:提交的描述信息
-a:指定镜像作者
e218edb10161:容器ID
facesoho/ubuntu:v2:指定要创建的目标镜像名
docker images 命令来查看的新镜像 facesoho/ubuntu:v2:
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE facesoho/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
使用的新镜像 facesoho/ubuntu 来启动容器
$ docker run -t -i facesoho/ubuntu:v2 /bin/bash
构建镜像
用命令 docker build
从零开始来创建一个新的镜像
需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建的镜像。
facesoho@facesoho:~$ cat Dockerfile FROM centos:6.7 MAINTAINER Fisher "fisher@sudops.com" RUN /bin/echo 'root:123456' |chpasswd RUN useradd facesoho RUN /bin/echo 'facesoho:123456' |chpasswd RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local EXPOSE 22 EXPOSE 80 CMD /usr/sbin/sshd -D
每一个指令都会在镜像上创建一个新的层
每一个指令的前缀都必须是大写的
第一条FROM指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令安装了什么
用 Dockerfile 文件通过 docker build 命令来构建一个镜像
$ docker build -t facesoho/centos:6.7 . Sending build context to Docker daemon 17.92 kB Step 1 : FROM centos:6.7 ---> d95b5ca17cc3 Step 2 : MAINTAINER Fisher "fisher@sudops.com" ---> Using cache ---> 0c92299c6f03 Step 3 : RUN /bin/echo 'root:123456' |chpasswd ---> Using cache ---> 0397ce2fbd0a Step 4 : RUN useradd facesoho ......
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为860c279d2fec
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE facesoho/centos 6.7 860c279d2fec About a minute ago 190.6 MB facesoho/ubuntu v2 70bf1840fd7c 17 hours ago 158.5 MB ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB php 5.6 f40e9e0f10c8 10 days ago 444.8 MB nginx latest 6f8d099c3adc 12 days ago 182.7 MB mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB hello-world latest 690ed74de00f 6 months ago 960 B centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
可以使用新的镜像来创建容器
$ docker run -t -i facesoho/centos:6.7 /bin/bash # id facesoho uid=500(facesoho) gid=500(facesoho) groups=500(facesoho)
从上面看到新镜像已经包含创建的用户facesoho
设置镜像标签
用 docker tag 命令,为镜像添加一个新的标签
$ docker tag 860c279d2fec facesoho/centos:dev
docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE facesoho/centos 6.7 860c279d2fec 5 hours ago 190.6 MB facesoho/centos dev 860c279d2fec 5 hours ago 190.6 MB facesoho/ubuntu v2 70bf1840fd7c 22 hours ago 158.5 MB ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB php 5.6 f40e9e0f10c8 10 days ago 444.8 MB nginx latest 6f8d099c3adc 13 days ago 182.7 MB mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB hello-world latest 690ed74de00f 6 months ago 960 B centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
大王叫我来巡山1998
.docker/machine/machines/default/ca.pem: no such file or directory
Was doing a docker-machine env default
where it seems to be getting stuck (waiting longer than usual) and when I open Virtualbox and look at the preview, it is already initialized and ready to accept commands but my terminal command is still waiting.
Then when I exit (e.g. Ctrl+C or Cmd+C) this error will show up.
open /Users/yourUsername/.docker/machine/machines/default/ca.pem: no such file or directory
The error could also be
Daemon not responding yet: dial tcp : xxx operation timed out
These resolved it for me.
docker-machine regenerate-certs default
docker-machine restart default
I was not using existing/custom certificates and letting docker toolbox create/manage it and willing to loose any information in that virtual/docker-machine. It should be true for most people but if you use custom certificates or don’t let docker-machine manage it for you, this will not apply to you and you need to find answers elsewhere.
There is also some references that AnyConnect (VPN) + your VirtualBox networking settings could cause it so if these do not work, try turning it off as well. Then find out more about VirtualBox networking settings. If you must be in VPN then
> you can use this script (while on VPN) – script found here – it will fix routes/rules that gets changed by AnyConnect
References
* https://github.com/docker/toolbox/issues/76
* Docker Toolbox