一、docker image是docker的三大组件之一。 docker把下载的images存储到docker主机上,如果一个image不在主机上,docker会从一个镜像仓库下载,默认的仓库是docker hub公共仓库
1.使用docker images查看本机上的images
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
REPOSITORY:来自于哪个仓库:比如docker.io/centos
TAG:TAG的标记,比如 latest
IMAGE ID:表示镜像的id号
CREATED:创建的时间
SIZE:镜像的SIZE
2.这里我们看到我们目前本机上没有任何的镜像,怎么获取镜像呢???
使用docker search 搜索合适的images,例如我们搜索centos镜像
[[email protected] ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 3596 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 100 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8... 81 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 33
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 30 [OK]
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 19 [OK]
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 16 [OK]
docker.io docker.io/centos/php-56-centos7 PHP 5.6 platform for building and running ... 9
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 7
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/centos/python-35-centos7 Python 3.5 platform for building and runni... 5
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x i... 3
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 3
docker.io docker.io/openshift/ruby-20-centos7 DEPRECATED: A Centos7 based Ruby v2.0 imag... 3
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developm... 1 [OK]
docker.io docker.io/openshift/php-55-centos7 DEPRECATED: A Centos7 based PHP v5.5 image... 1
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 1
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 1
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/miko2u/centos6 CentOS6 日本語環境 0 [OK]
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 0
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]
3.下载images
我们可以使用docker pull命令来预先下载我们需要的image比如我们下载一个centos镜像
[[email protected] ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
74f0853ba93b: Downloading [====> ] 5.946 MB/72.25 MB
4.下载完成之后我们查看images信息这边呢,我已经有下载好的 因为安装这个过程有点痛苦,比较慢。。。
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 50dae1ee8677 13 months ago 196.7 MB
查看docker.io/centos的详细信息
[[email protected] ~]# docker inspect docker.io/centos
二、创建我们自己的images
别人的镜像虽然很好,但是不一定就适合我们,我们可以对他们做一些改变,有两个方法
1.第一个方法使用docker commit来扩展一个image,先使用image容器,更新后提交结果到新的image
[[email protected] ~]# docker run -it docker.io/centos /bin/bash
[[email protected]da131594c82b /]#
注意:一定要记住容器的id以上红色标识
在容器中添加:mariadb-server应用
[[email protected] /]# yum -y install mariadb-server
2.当结束后,我们使用exit退出。现在我们的容器已经被我们改变了。使用dockercommint命令来提交相应的副本
[[email protected] ~]# docker commit -m "added mariadb app" -a "docker then" 2fcb38a4f8b3 centos:mariadb
sha256:f427f957e96c4187ebb85fd887d11ae0ef75725f2752b897e88b81db27d8b261
-m 来指定提交的说明信息,跟我们使用的版本控制一样 -a 可以指定更新的用户信息;之后是用来创建镜像容器的id 最后指定目标镜像仓库名和tag信息
3.使用docker images 查看新的镜像
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos mariadb f427f957e96c 4 minutes ago 453 MB
docker.io/centos latest 50dae1ee8677 13 months ago 196.7 MB
之后可以使用新的镜像来启动容器
[[email protected] ~]# docker run -it centos:mariadb /bin/bash
[[email protected] /]# uname -r
3.10.0-514.el7.x86_64
(2)第二个方法:从dockerfile来创建image
使用docker commit来扩展一个image简单,但它不容易在一个团队中共享它。我们使用docker build 来创建一个新的image。所以我们需要创建一个dockerfile,包含一些如何我们创建我们的image的指令,好的。接下来我们创建一个dockerfile
[[email protected] ~]# mkdir -p /docker/httpd
[[email protected] ~]# cd /docker/httpd/
[[email protected] httpd]# vim Dockerfile
MAINIAINER:是维护者信息
RUN开头指令会在创建中运行,比如安装一个软件包,在这里使用yum来安装一个软件
编写完成dockerfile后可以使用docker build来生成镜像。
-t是标记来添加的tag,指定新的镜像的用户信息。是dockerfile所在的路径(当前目录)也可以替换为一个具体的dockerfile路径。
(3)查看新生成的镜像
[[email protected] httpd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd fc33b28bf292 27 seconds ago 520.6 MB
centos mariadb 69d50d1f1869 4 minutes ago 453 MB
docker.io/centos latest 50dae1ee8677 13 months ago 196.7 MB
从我们新建的image开启容器
[[email protected] httpd]# docker run -it centos:httpd /bin/bash
[[email protected] /]# ls
anaconda-post.log bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
三、从本地载入images
要从本地文件导入一个镜像很简单,例如我下载了一个centos6的镜像如何从本地文件导入呢?
1)我们先把下载好的镜像通过远程终端RZ先导入到linux
2)我们先查看我们目前的images
3)随后我们把centos6镜像载入
[[email protected] ~]# docker load < centos6.tar
2714f4a6cdee: Loading layer [==================================================>] 202.3 MB/202.3 MB
导入后我们查看images
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd fc33b28bf292 9 minutes ago 520.6 MB
centos mariadb 69d50d1f1869 13 minutes ago 453 MB
docker.io/centos latest 50dae1ee8677 13 months ago 196.7 MB
docker.io/centos centos6 cf2c3ece5e41 14 months ago 194.6 MB
四、上传镜像
用户可以通过docker push命令来把自己创建的镜像上传到仓库中来共享,比如,用户在dockerhub上,完成注册后可以通过推送自己的镜像到仓库中,这里有两种方法可以注册一个docker hub账户 这里我们直接通过网站https://hub.docker.com 接下来就是注册的步骤。
1.通过浏览器打开https://hub.docker.com
2.然后注册完成后我们点击右上方的sign in来登陆。
这里就不再演示了。。
如何上传自己的镜像到docker hub呢?
1)docker login 输入自己的用户名-密码。
2)使用docker push命令上传
[[email protected] ~]# docker push bpcyh1/httpd:v1
The push refers to a repository [docker.io/bpcyh1/httpd]
802b9e516f89: Preparing
4ce561d10290: Preparing
b73bcd88057a: Preparing
0fe55794a0f7: Preparing
删除本地images
docker rmi命令 比如我们要删除centos6镜像
docker ps命令
docker ps -a
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c400a08888e centos:httpd "/bin/bash" 31 minutes ago Up 31 minutes jovial_wozniak
c4f9b15385cb docker.io/centos "/bin/bash" 50 minutes ago Exited (0) 35 minutes ago sharp_pasteur
a45310e161c5 docker.io/centos "/bin/bash" 58 minutes ago Exited (0) 55 minutes ago suspicious_bartik
a77bf0adbc0c docker.io/centos "/bin/bash" About an hour ago Exited (127) 58 minutes ago sharp_borg
2480c0a733bf docker.io/centos "/bin/bash" About an hour ago Exited (2) About an hour ago determined_mayer
f88bf6870452 docker.io/centos "/bin/bash" About an hour ago Exited (137) 55 minutes ago evil_colden
48a0da59e6e4 docker.io/centos "/bin/bash" About an hour ago Exited (137) 55 minutes ago angry_poitras
dd4518d54b25 docker.io/centos "/bin/bash" About an hour ago Exited (2) About an hour ago big_swanson
28edd82fb3a8 docker.io/centos "/bin/bahs" About an hour ago Created sick_perlman
67d0369ebd41 docker.io/centos "/bin/bash" About an hour ago Exited (0) About an hour ago trusting_sammet
83befbe01bf3 docker.io/centos "/bin/bash" About an hour ago Exited (137) 55 minutes ago big_lalande
a33c1b53566f docker.io/centos "/bin/bash" About an hour ago Exited (0) About an hour ago condescending_elion
b4d5d73c3e81 docker.io/centos "/bin/bas" About an hour ago Created admiring_lichterman
b9197c4818dc docker.io/centos "/bin/bash" About an hour ago Exited (0) About an hour ago evil_saha
49edcdbaed74 docker.io/centos "/bin/bash" About an hour ago Exited (1) About an hour ago dreamy_mcclintock
docker ps -h查看帮助
[[email protected] ~]# docker ps -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter value Filter output based on conditions provided (default [])
--format string Pretty-print containers using a Go template
--help Print usage
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
利用docker start命令 可以将一个已经终止的容器启动运行也可以使用stop停止。重启容器restart这里就不做演示啦。
用docker inspect来查看容器的详细信息
谢谢观看,希望对您有所帮助