docker镜像使用及连接
docker镜像使用当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。下面我们来学习:1、管理和使用本地 Docker 主机镜像2、创建镜像列出镜像列表我们可以使用 docker images 来列出本地主机上的镜像。[root@localhost app]# docker images...
docker镜像使用
当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
下面我们来学习:
1、管理和使用本地 Docker 主机镜像
2、创建镜像
列出镜像列表
我们可以使用 docker images 来列出本地主机上的镜像。
[root@localhost app]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.2.1511 9aec5c5fe4ba 2 months ago 195MB
hello-world latest fce289e99eb9 5 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:
[root@localhost app]# docker run -t -i ubuntu:15.10 /bin/bash
使用版本为14.04的ubuntu系统镜像来运行容器时,命令如下:
[root@localhost app]# docker run -t -i ubuntu:14.04 /bin/bash
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。
获取一个新的镜像
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。
在这里插入代码片
查找镜像
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个httpd的镜像来作为我们的web服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。
[root@localhost app]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 2504 [OK]
centos/httpd 23 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 22
armhf/httpd The Apache HTTP Server Project 8
polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 2 [OK]
salim1983hoop/httpd24 Dockerfile running apache config 2 [OK]
lolhens/httpd Apache httpd 2 Server 2 [OK]
rgielen/httpd-image-simple Docker image for simple Apache httpd based o… 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
dockerpinata/httpd 0
trollin/httpd 0
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 0 [OK]
izdock/httpd Production ready Apache HTTPD Web Server + m… 0
amd64/httpd The Apache HTTP Server Project 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
publici/httpd httpd:latest 0 [OK]
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
buzzardev/httpd Based on the official httpd image 0 [OK]
tugboatqa/httpd The Apache HTTP Server Project 0
manasip/httpd 0
ppc64le/httpd The Apache HTTP Server Project 0
alvistack/httpd Docker Image Packaging for Apache 0 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 0 [OK]
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布
拖取镜像
我们决定使用上图中的httpd 官方版本的镜像,使用命令 docker pull 来下载镜像。
[root@localhost app]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
fc7181108d40: Pull complete
b183a5e3b6da: Pull complete
c52952f0d826: Pull complete
c8f255a56e9a: Pull complete
144c3b858b48: Pull complete
Digest: sha256:a129c3a747fe9e406bf91d4d1fb2d4ed7b51d7a1f523fcf372c18c3c35981d12
Status: Downloaded newer image for httpd:latest
创建镜像
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
(由于15.01的ubuntu镜像源出现问题遂重新创建了16.04)
root@72980a42d9b8:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [857 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:9 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1250 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [968 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]
Get:15 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]
Get:16 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]
Get:17 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [557 kB]
Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B]
Fetched 15.8 MB in 11s (1331 kB/s)
Reading package lists... Done
在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit命令来退出这个容器。
此时ID为72980a42d9b8的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit来提交容器副本。
[root@localhost app]# docker commit -m="has update" -a='root' 72980a42d9b8 root/ubuntu:v2
sha256:2103c03d422ec13ecff94235427fdd24d4f20f3fc99f829b92d7c4abd4b027cf
各个参数说明:
-m:提交的描述信息
-a:指定镜像作者
e218edb10161:容器ID
runoob/ubuntu:v2:指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2:
[root@localhost app]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root/ubuntu v2 2103c03d422e 47 seconds ago 144MB
httpd latest e77c77f17b46 5 hours ago 140MB
ubuntu 16.04 2a697363a870 3 weeks ago 119MB
centos 7.2.1511 9aec5c5fe4ba 2 months ago 195MB
hello-world latest fce289e99eb9 5 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
使用我们的新镜像 root/ubuntu 来启动一个容器
[root@localhost app]# docker run -i -t root/ubuntu:v2 /bin/bash
root@ff088e6d209e:/#
构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
FROM centos:7.2.1511
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123' |chpasswd
RUN useradd centos
RUN /bin/echo 'centos:123' |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 命令来构建一个镜像。
[root@localhost dockerFile]# docker build -t centos/centos:7.2.1511 .
Sending build context to Docker daemon 14.85kB
Step 1/9 : FROM centos:7.2.1511
---> 9aec5c5fe4ba
Step 2/9 : MAINTAINER Fisher "fisher@sudops.com"
---> Running in 229828d0d896
Removing intermediate container 229828d0d896
---> be0ba576d2f6
Step 3/9 : RUN /bin/echo 'root:123' |chpasswd
---> Running in edd5d1191f0a
Removing intermediate container edd5d1191f0a
---> 15fd3b4208a1
Step 4/9 : RUN useradd centos
---> Running in 0dc1d0a2f872
Removing intermediate container 0dc1d0a2f872
---> 46e2e51f9d5a
Step 5/9 : RUN /bin/echo 'centos:123' |chpasswd
---> Running in fc827e7d5875
Removing intermediate container fc827e7d5875
---> c0dc2ba6122f
Step 6/9 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
---> Running in c435aff0040a
Removing intermediate container c435aff0040a
---> b6d1ada2a4d0
Step 7/9 : EXPOSE 22
---> Running in 84f33d324d98
Removing intermediate container 84f33d324d98
---> 9b0fed90289b
Step 8/9 : EXPOSE 80
---> Running in 50586484c928
Removing intermediate container 50586484c928
---> 4ea9fb5edc7b
Step 9/9 : CMD /usr/sbin/sshd -D
---> Running in aa379c547c34
Removing intermediate container aa379c547c34
---> ac33c8295659
Successfully built ac33c8295659
Successfully tagged centos/centos:7.2.1511
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在 ID为ac33c8295659
[root@localhost dockerFile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/centos 7.2.1511 ac33c8295659 About a minute ago 195MB
root/ubuntu v2 2103c03d422e 20 minutes ago 144MB
httpd latest e77c77f17b46 5 hours ago 140MB
ubuntu 16.04 2a697363a870 3 weeks ago 119MB
centos 7.2.1511 9aec5c5fe4ba 2 months ago 195MB
hello-world latest fce289e99eb9 5 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
我们可以使用新的镜像来创建容器
[root@localhost dockerFile]# docker run -t -i centos/centos:7.2.1511 /bin/bash
[root@2e7139bdf5fd /]# id centos
uid=1000(centos) gid=1000(centos) groups=1000(centos)
[root@2e7139bdf5fd /]#
设置镜像标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签。
[root@localhost dockerFile]# docker tag ac33c8295659 centos/centos:dev
[root@localhost dockerFile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/centos 7.2.1511 ac33c8295659 5 minutes ago 195MB
centos/centos dev ac33c8295659 5 minutes ago 195MB
root/ubuntu v2 2103c03d422e 24 minutes ago 144MB
httpd latest e77c77f17b46 5 hours ago 140MB
ubuntu 16.04 2a697363a870 3 weeks ago 119MB
centos 7.2.1511 9aec5c5fe4ba 2 months ago 195MB
hello-world latest fce289e99eb9 5 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
容器连接
我们来实现通过端口连接到一个 docker 容器
网络端口映射
我们创建了一个 python 应用的容器。
[root@localhost dockerFile]# docker run -d -P training/webapp python app.py
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。
我们使用 -P 参数创建一个容器,使用 docker ps 可以看到容器端口 5000 绑定主机端口 32768。
[root@localhost dockerFile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dc3381de095 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp friendly_driscoll
我们也可以使用 -p 标识来指定容器端口绑定到主机端口。
两种方式的区别是:
-P :是容器内部端口随机映射到主机的高端口。
-p : 是容器内部端口绑定到指定的主机端口
[root@localhost dockerFile]# docker run -d -p 5000:5000 training/webapp python app.py
[root@localhost dockerFile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5617356c1fb training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:5000->5000/tcp gifted_yalow
8dc3381de095 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp friendly_driscoll
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。
[root@localhost dockerFile]# docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
[root@localhost dockerFile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
732e90f9cd40 training/webapp "python app.py" 34 seconds ago Up 29 seconds 127.0.0.1:5001->5000/tcp gracious_jones
a5617356c1fb training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:5000->5000/tcp gifted_yalow
8dc3381de095 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp friendly_driscoll
上面的例子中,默认都是绑定 tcp 端口,如果要绑定 UDP 端口,可以在端口后面加上 /udp。
[root@localhost dockerFile]# docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
e7abe7794727143c35d6f653af6c96b3b4520eef2c238c14470ca267dba1591f
[root@localhost dockerFile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7abe7794727 training/webapp "python app.py" 11 seconds ago Up 5 seconds 5000/tcp, 127.0.0.1:5000->5000/udp eager_nobel
732e90f9cd40 training/webapp "python app.py" 2 minutes ago Up 2 minutes 127.0.0.1:5001->5000/tcp gracious_jones
a5617356c1fb training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:5000->5000/tcp gifted_yalow
8dc3381de095 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp friendly_driscoll
docker port 命令可以让我们快捷地查看端口的绑定情况。(udp不能这么查看)
[root@localhost dockerFile]# docker port gracious_jones 5000
127.0.0.1:5001
Docker容器连接
端口映射并不是唯一把 docker 连接到另一个容器的方法。
docker 有一个连接系统允许将多个容器连接在一起,共享连接信息。
docker 连接会创建一个父子关系,其中父容器可以看到子容器的信息。
容器命名
当我们创建一个容器的时候,docker 会自动对它进行命名。另外,我们也可以使用 --name 标识来命名容器,例如:
[root@localhost dockerFile]# docker run -d -P --name centos training/webapp python app.py
c3c04fbb2757bda24a3391098eea4ffa4738f7f378ffdb58df7f1188f2608330
[root@localhost dockerFile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3c04fbb2757 training/webapp "python app.py" 7 seconds ago Up 3 seconds 0.0.0.0:32769->5000/tcp centos
e7abe7794727 training/webapp "python app.py" 5 minutes ago Up 5 minutes 5000/tcp, 127.0.0.1:5000->5000/udp eager_nobel
732e90f9cd40 training/webapp "python app.py" 7 minutes ago Up 7 minutes 127.0.0.1:5001->5000/tcp gracious_jones
a5617356c1fb training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:5000->5000/tcp gifted_yalow
8dc3381de095 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp friendly_driscoll
更多推荐
所有评论(0)