Docker使用-镜像篇

版权声明:作者-傲娇天子 博文主页地址:https://blog.csdn.net/qq_41116956 欢迎转载,转载请在文章页面明显位置给出原文链接,谢谢 https://blog.csdn.net/qq_41116956/article/details/84838113

前面说了docker的安装docker容器的使用后,本文在列一下常见的docker_images的使用方法


docker镜像的使用,在docker的安装和容器篇都有实例,本文主要概况一下images的使用方法和创建image以及设置image的标签。

查询本地images

[root@www /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              568c4670fa80        7 days ago          109MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB
名称                标签                 镜像id              下拉时间             存储

查询网上可用images

1:查询nginx可用镜像

[root@www /]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10461               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1480                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   657                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   449                                     [OK]
...

2:查询mysql可用镜像

[root@www /]# docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7474                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2410                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   557                                     [OK]
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       150                                     [OK]
...

pull镜像(下载镜像)

这里就以上面的mysql镜像为例

[root@www /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
a5a6f2f73cd8: Already exists 
936836019e67: Pull complete 
283fa4c95fb4: Pull complete 
1f212fb371f9: Pull complete 
e2ae0d063e89: Pull complete 
5ed0ae805b65: Pull complete 
0283dc49ef4e: Pull complete 
a7e1170b4fdb: Pull complete 
88918a9e4742: Pull complete 
241282fa67c2: Pull complete 
b0fecf619210: Pull complete 
bebf9f901dcc: Pull complete 
Digest: sha256:b7f7479f0a2e7a3f4ce008329572f3497075dc000d8b89bac3134b0fb0288de8
Status: Downloaded newer image for mysql:latest
[root@www /]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              568c4670fa80        7 days ago          109MB
mysql               latest              f991c20cb508        2 weeks ago         485MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB

创建image:一个属于自己的镜像

1:更新image(在原有的基础上创建image)

①:创建容器mysql_1.0

[root@www /]# docker run -i -t --name Centos_1.0 centos /bin/bash
...
root@b8bc689a97b0:/# 

在Centos_1.0中安装一个mysql服务(这里就不做详细过程了)

安装好后exit

[root@www /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                   NAMES
b8bc689a97b0        mysql               "docker-entrypoint.s…"   6 minutes ago       Exited (127) 2 minutes ago                           Centos_1.0

②:提交容器到新的image

[root@www /]# docker commit -m="this is Centos_1.0 install mysql server" -a="God" b8bc689a97b0 mysql/centos:v1
sha256:46e6044e776894af153c6744c2aa23810e0066e8abe81eacbb9a9dfc1ad2bb88
[root@www /]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql/centos        v1                  46e6044e7768        13 seconds ago      485MB
nginx               latest              568c4670fa80        7 days ago          109MB
mysql               latest              f991c20cb508        2 weeks ago         485MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB

-m:image的描述信息

-a:image的作者

然后是安装mysql后的容器id和需要给image命名的名字:标签(mysql/centos:v1)

2:构建image(一个全新的image)

需要使用dockerfile来创建image

[root@www ~]# vim Dockerfile

FROM    centos:6.7
MAINTAINER      Fisher "[email protected]"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob: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 bulid调用Dockerfile文件

[root@www ~]# docker build -t centos:6.7 .

后面有个点,注意不要写掉了

[root@www ~]# docker build -t centos:6.7 .
Sending build context to Docker daemon  227.5MB
Step 1/9 : FROM    centos:6.7
6.7: Pulling from library/centos
cbddbc0189a0: Pull complete 
...
Removing intermediate container 5ba438193ea1
 ---> 9c34f3105b8a
Step 8/9 : EXPOSE  80
 ---> Running in 8124de73f09d
Removing intermediate container 8124de73f09d
 ---> 9949844420c2
Step 9/9 : CMD     /usr/sbin/sshd -D
 ---> Running in 3528928bf8cf
Removing intermediate container 3528928bf8cf
 ---> 24c8dcf32058
Successfully built 24c8dcf32058
Successfully tagged centos:6.7

创建成功:

[root@www ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              6.7                 24c8dcf32058        2 minutes ago       191MB
mysql/centos        v1                  46e6044e7768        14 minutes ago      485MB
nginx               latest              568c4670fa80        7 days ago          109MB
mysql               latest              f991c20cb508        2 weeks ago         485MB
centos              <none>              192ad0341c8b        8 weeks ago         191MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB

设置镜像标签

[root@www ~]# docker tag 24c8dcf32058 centos:v1
[root@www ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              6.7                 24c8dcf32058        4 minutes ago       191MB
centos              v1                  24c8dcf32058        4 minutes ago       191MB
mysql/centos        v1                  46e6044e7768        16 minutes ago      485MB
nginx               latest              568c4670fa80        7 days ago          109MB
mysql               latest              f991c20cb508        2 weeks ago         485MB
centos              <none>              192ad0341c8b        8 weeks ago         191MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB

镜像标签可以在docker镜像过多时用 docker rmi 删掉多余的images。另外,想要将本地镜像发布到网上,(以docker hub网站为例)需要注册docker hub并且使用docker push命令

完!

猜你喜欢

转载自blog.csdn.net/qq_41116956/article/details/84838113
今日推荐