第七篇:Docker镜像操作
运行容器时,当本地不存在镜像时,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。1.列出本地镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 2e202f453940 11 days ago 179MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
hello-world latest f2a91732366c 2 months ago 1.85kB
training/webapp latest 6fae60ef3446 2 years ago 349MB
参数说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
2.使用镜像运行容器
# docker run -ti httpd /bin/bash
root@dd34d7458e86:/usr/local/apache2# ls
binbuildcgi-binconferrorhtdocsiconsincludelogsmodules
root@dd34d7458e86:/usr/local/apache2#
3.拉取新镜像
如果本地不存在使用的镜像,Docker就会自动到镜像仓库去下载镜像,使用docker pull
# docker pull ubuntu:16.04 ##下载指定版本镜像
16.04: Pulling from library/ubuntu
1be7f2b886e8: Pull complete
6fbc4a21b806: Pull complete
c71a6f8e1378: Pull complete
4be3072e5a37: Pull complete
06c6d2f59700: Pull complete
Digest: sha256:2b80e7fbfd1b0a3784dd0e55fb6f2750ebc33e596c2ccee5201ecf018ac5ae05
Status: Downloaded newer image for ubuntu:16.04
4.搜索镜像
可以通过docker search 命令搜索需要使用的镜像
# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 1490
hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with a … 40
centos/httpd 15
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 8
armhf/httpd The Apache HTTP Server Project 8
macadmins/netboot-httpd use in combination with bruienne/bsdpy 4
salim1983hoop/httpd24 Dockerfile running apache config 2
lolhens/httpd Apache httpd 2 Server 2
rgielen/httpd-image-php5 Docker image for Apache httpd with PHP 5 bas… 1
rgielen/httpd-image-simple Docker image for simple Apache httpd based o… 1
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1
fboaventura/dckr-httpd Small footprint http server to use with othe… 1
epflidevelop/os-wp-httpd WP httpd 1
publici/httpd httpd:latest 0
tplatform/aws-linux-httpd24-php70 aws-linux-httpd24-php70 0
dockerpinata/httpd 0
5.创建镜像
在生产环境上,一般情况下,docker镜像需要自身业务来定制,可通过两种方式来实现docker镜像的定制化
a.从已有创建的容器镜像更新
b.根据自身需求编写dockerfile创建新镜像
更新镜像前,先使用该镜像创建一个容器
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 2e202f453940 11 days ago 179MB
ubuntu 16.04 0458a4468cbc 11 days ago 112MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
hello-world latest f2a91732366c 2 months ago 1.85kB
training/webapp latest 6fae60ef3446 2 years ago 349MB
# docker run -ti ubuntu:16.04 /bin/bash
root@7e0559072ced:/#
进入镜像使用apt-get update 命令更新
# docker exec -ti 7e0559072ced bash
root@7e0559072ced:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages
Fetched 24.8 MB in 32s (772 kB/s)
Reading package lists... Done
root@7e0559072ced:/# exit
exit
# docker ps |grep ubuntu
7e0559072ced ubuntu:16.04 "/bin/bash" 9 hours ago Up 9 hours naughty_jennings
说明:
此时ubuntu的容器ID:7e0559072ced,可以通过docker commit来提交容器副本
# docker commit -m="has update" -a="liu" 7e0559072ced liu/ubuntu:v2
sha256:15caed78232629df1ec5cd077d38215271bc413d12bc1500dc52cb3bc3e12c75
[*]-m:提交的描述信息
[*]-a:指定镜像作者
[*]7e0559072ced:容器的ID
[*]liu/ubuntu:v2指定要创建的目标镜像名
使用docker images查看提交的新镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liu/ubuntu v2 15caed782326 2 minutes ago 151MB ####提交的新镜像
httpd latest 2e202f453940 11 days ago 179MB
ubuntu 16.04 0458a4468cbc 12 days ago 112MB
nginx latest 3f8a4339aadd 6 weeks ago 108MB
hello-world latest f2a91732366c 2 months ago 1.85kB
training/webapp latest 6fae60ef3446 2 years ago 349MB
使用新镜像启动一个容器
# docker run -ti liu/ubuntu:v2 /bin/bash
root@60e40fc5e5d8:/#
6.构建镜像
使用docker build 从零开始构建一个新的镜像,需要编写Dockerfile容器文件,如
# vim Dockerfile
FROM centos:6.7
##指定使用哪个镜像源
MAINTAINER Fisher "fisher@sudops.com"
##RUN告诉docker需要在镜像内执行哪些命令
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd liulei
RUN /bin/echo 'liulei:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE22
EXPOSE80
CMD /usr/sbin/sshd -D
~
~
在编写Dockerfile文件时,每一个指令都会在镜像上创建一个新的层,并且每一条指令首字母必须是大写
然后,使用编写的Dockerfile文件,通过docker build命令来构建一个镜像
# docker build -t liulei/centos:6.7 .
ERRO Can't add file /root/.gnupg/S.gpg-agent to tar: archive/tar: sockets not supported
Sending build context to Docker daemon779.4MB
Step 1/9 : FROM centos:6.7
6.7: Pulling from library/centos
cbddbc0189a0: Pull complete
Digest: sha256:7248c96de4648749c7936f203d983530e7ebdd83c3db6d47278392f18bcd7baf
Status: Downloaded newer image for centos:6.7
---> 000c5746fa52
Step 2/9 : MAINTAINER Fisher "fisher@sudops.com"
---> Running in 7de81283b6be
Removing intermediate container 7de81283b6be
---> 00e92f10a010
Step 3/9 : RUN /bin/echo 'root:123456' |chpasswd
---> Running in e2265b58e93b
Removing intermediate container e2265b58e93b
---> 0bd4ee58d0e3
Step 4/9 : RUN useradd liulei
---> Running in 846595d00078
Removing intermediate container 846595d00078
---> 600139a37c32
Step 5/9 : RUN /bin/echo 'liulei:123456' |chpasswd
---> Running in 236b3202305b
Removing intermediate container 236b3202305b
---> 984723c05b0f
Step 6/9 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
---> Running in c18bf04cfb8c
Removing intermediate container c18bf04cfb8c
---> 65c9fe71b1f9
Step 7/9 : EXPOSE22
---> Running in 93f0491bde21
Removing intermediate container 93f0491bde21
---> 14ee40f132fc
Step 8/9 : EXPOSE80
---> Running in 5be75fdead2e
Removing intermediate container 5be75fdead2e
---> f380dde783ad
Step 9/9 : CMD /usr/sbin/sshd -D
---> Running in cf4e114e9953
Removing intermediate container cf4e114e9953
---> dc64de0c25c4
Successfully built dc64de0c25c4
Successfully tagged liulei/centos:6.7
#
参数说明
[*]-t 表示指定要创建的目标镜像名
[*]. 表示Dokerfile文件在当前目录,也可以指定Dockerfile的绝对路径
使用docker images 查看通过dockerfile文件创建的镜像是否存在
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liulei/centos 6.7 dc64de0c25c4 6 minutes ago 191MB ##使用Dockerfile创建的镜像
liu/ubuntu v2 15caed782326 21 minutes ago 151MB
httpd latest 2e202f453940 11 days ago 179MB
ubuntu 16.04 0458a4468cbc 12 days ago 112MB
nginx latest 3f8a4339aadd 6 weeks ago 108MB
hello-world latest f2a91732366c 2 months ago 1.85kB
centos 6.7 000c5746fa52 3 months ago 191MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
使用Dockerfile文件创建的镜像来运行容器
# docker run -ti dc64de0c25c4 /bin/bash
#
7.设置镜像标签
使用dockertag 命令为镜像设置标签
# docker tag dc64de0c25c4 liulei/centos:new
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liulei/centos 6.7 dc64de0c25c4 13 minutes ago 191MB
liulei/centos new dc64de0c25c4 13 minutes ago 191MB
liu/ubuntu v2 15caed782326 28 minutes ago 151MB
httpd latest 2e202f453940 11 days ago 179MB
ubuntu 16.04 0458a4468cbc 12 days ago 112MB
nginx latest 3f8a4339aadd 6 weeks ago 108MB
hello-world latest f2a91732366c 2 months ago 1.85kB
centos 6.7 000c5746fa52 3 months ago 191MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
页:
[1]