docker构建微服务 镜像使用

镜像列表

使用命令:docker images列出本地主机上的镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              e74c56fbba84        4 months ago        196.6 MB
training/webapp     latest              02a8815912ca        2 years ago         348.7 MB
  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

查找镜像

使用命令:docker search nginx

[root@localhost ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                                  Official build of Nginx.                        8068      [OK]       
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1284                 [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   526                  [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as...   321                  [OK]
kong                                                   Open-source Microservice & API Management ...   164       [OK]       
webdevops/php-nginx                                    Nginx with PHP-FPM                              97                   [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demons...   95                   
bitnami/nginx                                          Bitnami nginx Docker Image                      45                   [OK]
linuxserver/nginx                                      An Nginx container, brought to you by Linu...   33                   
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          26                   [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   18                   [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                9                    [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          8                    [OK]
nginxdemos/nginx-ingress                               NGINX Ingress Controller for Kubernetes         8                    
webdevops/nginx                                        Nginx container                                 8                    [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building...   6                    
nginxdemos/hello                                       NGINX webserver that serves a simple page ...   4                    [OK]
1science/nginx                                         Nginx Docker images that include Consul Te...   4                    [OK]
pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ng...   2                    [OK]
behance/docker-nginx                                   Provides base OS, patches and stable nginx...   2                    [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4      ubuntu-16-nginx-php-5.6-wordpress-4             2                    [OK]
travix/nginx                                           NGinx reverse proxy                             1                    [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on sa...   1                    [OK]
goodguide/nginx-application-proxy                      No-configuration Nginx reverse proxy for a...   0                    [OK]
mailu/nginx                                            Mailu nginx frontend                            0                    [OK]

获取镜像

使用命令:docker pull nginx

[root@localhost ~]# docker pull nginx
latest: Pulling from nginx
3b530f2a9c24: Pull complete 
409bdabda854: Pull complete 
d6d4180fd933: Pull complete 
92d462650f4b: Pull complete 
ee7b94dfef73: Pull complete 
7a2c6f1ea023: Pull complete 
01908f5aff63: Pull complete 
f2f739b51090: Pull complete 
d8e491b2e723: Pull complete 
b7dd2c8cb8a8: Pull complete 
Digest: sha256:c8db985772160427261dc443e62cab3e07212f7d52a18093d29f561b767bccb2
Status: Downloaded newer image for nginx:latest

启动镜像
在启动nginx镜像之前,需要创建nginx的目录

  • 1.创建nginx的应用目录
mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
  • 2.在~/nginx/conf 目录中创建配置文件:nginx.conf
  • 3.启动nginx服务
[root@localhost nginx]# docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:$PWD/nginx/conf/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx 

--name 参数指定启动的镜像名称
-p 参数指定端口映射

  • 4.查看启动的nginx服务
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                NAMES
91d8abc1043c        nginx               "nginx -g 'daemon of   5 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp   mynginx

创建镜像的方式

当docker镜像仓库中的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像更改

  • 1.从已经创建的容器中更新镜像,并且提交这个镜像
  • 2.使用Dockerfile 命令创建一个新的镜像

更新镜像

更新镜像之前,需要使用镜像来创建一个容器

[root@localhost nginx]# docker run -i -t centos
  • 更新容器

在容器内,可以对当前容器进行更新,完成后使用exit命令退出当前容器

  • 提交更新的容器
docker commit -m="update my image" -a=“maxbin” c6359ab747b9 maxbin/centos7:test

-m:提交的描述信息
-a:指定镜像作者
c6359ab747b9:容器ID
maxbin/centos7:test:指定要创建的目标镜像名

  • 查看更提交的镜像

命令:docker images

[root@localhost nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
maxbin/centos7      test                fecfb9de4488        16 seconds ago      196.6 MB
nginx               latest              b7dd2c8cb8a8        2 weeks ago         108.6 MB
centos              latest              e74c56fbba84        4 months ago        196.6 MB
training/webapp     latest              02a8815912ca        2 years ago         348.7 MB

看到的第一行就是我们刚提交的自定义更新的镜像

创建新镜像

使用docker build命令构建一个新的镜像

  1. 创建Dockerfile文件,Dockerfile文件包含一组指定,告诉Docker改如何构建我们的镜像。
vim Dockerfile # 创建文件

然后输入i命令进入编辑状态,粘贴如下内容

FROM    centos
MAINTAINER      Fisher "binbin721@gmail.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd maxbin
RUN     /bin/echo 'maxbin:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D
  1. 使用build构建镜像
[root@localhost ~]# docker build -t maxbin/centos7.3 .
Sending build context to Docker daemon  3.24 GB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> e74c56fbba84
Step 1 : MAINTAINER Fisher "binbin721@gmail.com"
 ---> Running in df20fa3419ab
 ---> 4dc644e9414d
Removing intermediate container df20fa3419ab
Step 2 : RUN /bin/echo 'root:123456' |chpasswd
 ---> Running in f662fce1bb05
 ---> b6da84798d08
Removing intermediate container f662fce1bb05
Step 3 : RUN useradd maxbin
 ---> Running in 0d07934175f0
 ---> 4179857901dc
Removing intermediate container 0d07934175f0
Step 4 : RUN /bin/echo 'maxbin:123456' |chpasswd
 ---> Running in 235eba11a6b8
 ---> af077891d0a5
Removing intermediate container 235eba11a6b8
Step 5 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
 ---> Running in d1a60c90a489
 ---> 3ed422bf1426
Removing intermediate container d1a60c90a489
Step 6 : EXPOSE 22
 ---> Running in 3120c72e74c3
 ---> 82d08c25839d
Removing intermediate container 3120c72e74c3
Step 7 : EXPOSE 80
 ---> Running in ceee7c9983a6
 ---> 3eff7822abc3
Removing intermediate container ceee7c9983a6
Step 8 : CMD /usr/sbin/sshd -D
 ---> Running in 988bd79d1aed
 ---> 7cbc3dfef48e
Removing intermediate container 988bd79d1aed
Successfully built 7cbc3dfef48e

-t:指定要创建的目标镜像名
.:Dockerfile文件的所在目录,也可以指定Dockerfile的绝对路径

  • Successfully built 7cbc3dfef48e 说明镜像构建成功了

  • 查看镜像:docker images

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
maxbin/centos7.3    latest              7cbc3dfef48e        About a minute ago   196.9 MB
maxbin/centos7      test                fecfb9de4488        23 minutes ago       196.6 MB
nginx               latest              b7dd2c8cb8a8        2 weeks ago          108.6 MB
centos              latest              e74c56fbba84        4 months ago         196.6 MB
training/webapp     latest              02a8815912ca        2 years ago          348.7 MB
  • 设置镜像的TAG

使用命令:docker tag

 docker tag 7cbc3dfef48e maxbin/centos:dev

设置tag后,会发现ID:7cbc3dfef48e的镜像有多个TAG

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
maxbin/centos7.3    latest              7cbc3dfef48e        4 minutes ago       196.9 MB
maxbin/centos       dev                 7cbc3dfef48e        4 minutes ago       196.9 MB
maxbin/centos7      test                fecfb9de4488        27 minutes ago      196.6 MB
nginx               latest              b7dd2c8cb8a8        2 weeks ago         108.6 MB
centos              latest              e74c56fbba84        4 months ago        196.6 MB
training/webapp     latest              02a8815912ca        2 years ago         348.7 MB
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载自 http://blog.opskumu.com/docker.html 一、Docker 简介 Docke...
    极客圈阅读 10,544评论 0 120
  • Docker — 云时代的程序分发方式 要说最近一年云计算业界有什么大事件?Google Compute Engi...
    ahohoho阅读 15,636评论 15 147
  • 0. 前言 docker是什么?docker是用GO语言开发的应用容器引擎,基于容器化,沙箱机制的应用部署技术。可...
    sessionboy阅读 3,884评论 2 49
  • 《Docker从入门到实践》阅读笔记 原书地址: https://yeasy.gitbooks.io/docker...
    GuoYuebo阅读 11,424评论 1 39
  • 趁着她卸货之前,我来沾沾“运”气。想想当初来北京的同学中,如今只有我们两还坚守着,转眼也有14年了。跟所有的北漂一...
    依诺2008阅读 130评论 0 0