1、从镜像源载入镜像到本地
[dkuser@c741 ~]$ docker pull ubuntu:14.04
[dkuser@c741 ~]$ docker pull php:5.6
[dkuser@c741 ~]$ docker pull nginx
[dkuser@c741 ~]$ docker pull mysql:5.6
[dkuser@c741 ~]$ docker pull httpd
不指定版本的话默认下载最新的镜像文件。
2、查看本地镜像
[dkuser@c741 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 cf6527af4ce6 4 hours ago 256MB
ubuntu 15.10_basis_env 6422827a9898 19 hours ago 492MB
nginx latest 27a188018e18 8 days ago 109MB
httpd latest d4a07e6ce470 3 weeks ago 132MB
ubuntu 14.04 390582d83ead 6 weeks ago 188MB
php 5.6 36c3c974e6ee 3 months ago 344MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 3 years ago 349MB
说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
3、搜索镜像源上的镜像
[dkuser@c741 ~]$ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 2446 [OK]
hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with a … 46
centos/httpd 23 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 22
tplatform/aws-linux-httpd24-php70 aws-linux-httpd24-php70 3 [OK]
.....
说明:
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布
4、通过Dockerfile方式创建centos6.7镜像
4.1、创建编辑Dockerfile文件
[dkuser@c741 ~]$ vi Dockerfile
FROM centos:6.7
MAINTAINER Fisher "sxzhou@xxx.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd dkuser
RUN /bin/echo 'dkuser: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 在镜像内执行命令,安装了什么。
4.2、构建镜像
[dkuser@c741 ~]$ docker build -t dkuser/centos:6.7 .
Sending build context to Docker daemon 6.656kB
Step 1/9 : FROM centos:6.7
---> 9f1de3c6ad53
.....
Removing intermediate container ba0982c10cfb
---> 757931986903
Successfully built 757931986903
Successfully tagged dkuser/centos:6.7
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
4.3、查看创建的镜像
[dkuser@c741 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dkuser/centos 6.7 757931986903 6 seconds ago 191MB
4.4、以此镜像运行容器
[dkuser@c741 ~]$ docker run -t -i dkuser/centos:6.7 /bin/sh
4.5、查看用户信息
sh-4.1# id dkuser
uid=500(dkuser) gid=500(dkuser) groups=500(dkuser)
4.6、设置镜像标签
[dkuser@c741 ~]$ docker tag 757931986903 dkuser/centos:dev
查看新打标签的镜像
[dkuser@c741 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dkuser/centos 6.7 757931986903 4 minutes ago 191MB
dkuser/centos dev 757931986903 4 minutes ago 191MB
参考: