Docker从入门到精通<3>-日常管理

docker日常管理

镜像管理的命令

1. 从Docker Hub搜索镜像

[root@vm1 ~]# docker search --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

  

[root@vm1 ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15121     [OK]
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2039                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   816                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   208
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   147
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   132                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        121                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   101                  [OK]
jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   91                   [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   70                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   56                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  41
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   23                   [OK]
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15
raulr/nginx-wordpress             Nginx front-end for the official wordpress:f…   13                   [OK]
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13
flashspys/nginx-static            Super Lightweight Nginx Image                   10                   [OK]
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
devilbox/nginx-stable             Devilbox's Nginx stable (based on official N…   4
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]

参数说明:

NAME: 镜像仓库源的名称

DESCRIPTION: 镜像的描述

OFFICIAL: 是否 docker 官方发布

stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。

AUTOMATED: 自动构建。

 一般我们会选择星星最多的镜像来使用,如果我们不知道有哪些版本,这里我们可以查询docker镜像所有的版本:https://hub.docker.com/u/library

 

[root@vm1 ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile           # 制作docker镜像
  history     Show the history of an image              # 查看镜像的更变记录
  import      Import the contents from a tarball to create a filesystem image # 把一个容器快照导入到本地镜像库
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN  # 把镜像库存储文件导入到本地镜像库
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry # 下载镜像,如果不加版本号,默认下载最新版本
  push        Push an image or a repository to a registry # 把本地镜像推送到镜像仓库
  rm          Remove one or more images   # 删除镜像
  save        Save one or more images to a tar archive (streamed to STDOUT by default)  # 把一个或者多个镜像合并导出
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE  # 给镜像打标签

Run 'docker image COMMAND --help' for more information on a command.        


import 和 load的区别

  • docker import creates one image from one tarball which is not even an image (just a filesystem you want to import as an image)

Create an empty filesystem image and import the contents of the tarball

  • docker load creates potentially multiple images from a tarred repository (since docker save can save multiple images in a tarball).

Loads a tarred repository from a file or the standard input stream

  

docker容器的管理命令

 

先看下容器的生命周期管理

 运行容器

docker run -d -p 81:80 -v /mnt:/usr/share/nginx/html/ nginx:1.20

docker run 参数:


-d    表示在后台运行

-p    把容器的端口映射到主机上面,这里把容器的80端口映射到主机上的81端口

-v    表示把主机的目录挂载到容器的指定目录下

-e    指定容器的环境变量

-it    分配交互式的虚拟终端

-w    指定容器的工作目录

--name   指定容器的名称,默认为系统随机分配的字符串

--cpus    指定cpu的数量

--memory  指定内存的大小

--network  指定容器使用的网络

还有其他的很多参数,可以查看帮助文档。 

  docker container 有时container可以省略

  

# 常用
docker create    创建容器
docker  start     启动容器
docker  run       创建并运行容器(相当于create + start)
docker  restart   重启容器
docker stop       停止容器(优雅的停止容器)
docker kill         停止容器(类似 kill -9)
docker rm          删除容器
docker logs        查看容器的日志
docker attach     实时捕获容器的标准输入、输出、错误流信息
docker exec       在容器中执行命令
 
# 不太常用
docker cp     在容器和主机之间文件的互相拷贝
docker export 把一个容器的文件系统归档为tar
docker pause  暂停一个或者多个容器内的所有进程
docker unpause    恢复一个或者多个容器内的所有进程
docker commit   从容器中创建一个新的镜像
docker prune   移除所有停止的容器
docker stats 展示容器的资源使用情况
docker diff 检测容器文件系统中文件或者目录的变化
docker inspect 列出容器详细的信息
docker rename  容器的重命名
docker wait 阻塞运行直到容器停止,然后打印出它的退出代码 
docker top 列出容器中运行的所有的进程
docker update 更新一个或者多个容器的配置

  

# 删除退出状态的容器,并把和容器在目录/var/li/docker相关匿名volume删除,挂载其他的卷不会删除
docker rm -fv `docker ps -aq -f status=exited`

# 更新容器的配置设置cpu限制
docker update --cpus=1 7c3b52874ba4 # 只允许使用一个cpu


# 更新容器的配置设置内存限制
docker update --memory=512M 7c3b52874ba4 # 只允许容器使用512M

注意在设置内存限制时,如果报错"Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.",这时我们需要修改内核参数

vim /etc/default/grub file

GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"

然后

sudo update-grub

重启系统

  docker容器的运行理念

docker容器中一般只运行一个进程,而且此进程需要在前台运行并且夯住。如果需要运行多个进程可以采用supervisor来进行统一管理。

下面我们来看一个例子:

docker run -d --name=ubuntu ubuntu

[root@vm1 ~]# docker run -d --name=ubuntu ubuntu
429f3264744fe5bfcae9b8446cfe560545748648329576f480d96e4af9ee494c
[root@vm1 ~]#
[root@vm1 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                     PORTS     NAMES
429f3264744f   ubuntu    "bash"    9 seconds ago   Exited (0) 7 seconds ago             ubuntu

  ubuntu默认的bash由于无法一直在前台运行所以直接就挂掉了。

下面我们尝试用tail -F /var 的方式运行

[root@vm1 ~]# docker run -d --name=ubuntu --rm=true  ubuntu tail -F /xxx
1f7dfa7a87033016e89894fcb126ded65c27da1fdddce5b2d2137b0d78943e91
[root@vm1 ~]#
[root@vm1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND          CREATED         STATUS         PORTS     NAMES
1f7dfa7a8703   ubuntu    "tail -F /xxx"   4 seconds ago   Up 2 seconds             ubuntu

  我们看到容器已经夯住了,其实直接用docker -i也可以使容器夯住,但是我们为了更好的获取容器的状态信息和输入日志信息,一般都会让docker内的进程直接在前台运行。

  下面我们基于centos8的基础上制作一个nginx的镜像

[root@vm1 ~]# docker run -d --name centos8 --rm=true centos tail -F /var
c7043a81529a22aa64b9c7f5ff34115f240a3c80a5da59baf66e497810b52f67
 
 
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND          CREATED         STATUS         PORTS     NAMES
c7043a81529a   centos    "tail -F /var"   4 seconds ago   Up 2 seconds             centos8
[root@vm1 ~]# docker exec -it c7043a81529a /bin/bash
[root@c7043a81529a /]#
[root@c7043a81529a /]#
[root@c7043a81529a /]# yum -y install nginx
...
Complete!
[root@c7043a81529a /]# pwd
/
[root@c7043a81529a /]# vi /etc/nginx/nginx.conf  # 这里我们nginx配置中增加一个行daemon off;
[root@c7043a81529a /]#
[root@c7043a81529a /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@c7043a81529a /]# nginx -v
nginx version: nginx/1.14.1
[root@c7043a81529a /]# exit
exit
[root@vm1 ~]# docker container commit -a 'Yangning' -m '基于centos8上制作nginx1.14的新镜像' centos8 centos8_nginx_1.14:v1
 
sha256:cae942db258287ddea50ff71893282a46eb508c83f00e2bf1d04558c20640552
[root@vm1 ~]#
[root@vm1 ~]# docker images
REPOSITORY                      TAG                 IMAGE ID       CREATED         SIZE
centos8_nginx_1.14              v1                  cae942db2582   7 seconds ago   316MB
zabbix/zabbix-java-gateway      alpine-5.4-latest   2552b7f42e42   9 days ago      84.4MB
nginx                           1.20                7ca45f2d188b   2 weeks ago     133MB
mysql                           8.0                 5c62e459e087   2 weeks ago     556MB
ubuntu                          latest              9873176a8ff5   2 weeks ago     72.7MB
zabbix/zabbix-web-nginx-mysql   alpine-5.4-latest   c75fcde05a14   3 weeks ago     166MB
zabbix/zabbix-server-mysql      alpine-5.4-latest   856abc12b012   3 weeks ago     69MB
busybox                         latest              69593048aa3a   4 weeks ago     1.24MB
centos                          latest              300e315adb2f   7 months ago    209MB
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# docker run -d -p 80:80 --name centos8_nginx  centos8_nginx_1.14:v1 nginx
3b3475678419e30fbeee28fc68680661745c641d305b699a811936b2f82cdae4
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# docker ps
CONTAINER ID   IMAGE                   COMMAND          CREATED          STATUS          PORTS                               NAMES
3b3475678419   centos8_nginx_1.14:v1   "nginx"          8 seconds ago    Up 7 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp   centos8_nginx
c7043a81529a   centos                  "tail -F /var"   25 minutes ago   Up 25 minutes   

  这样我们容器就直接启动成功了,这里我们打开浏览器访问下http://192.168.22.62就看到了如下页面。

posted @ 2021-07-06 18:13  早晨我在雨中采花  阅读(56)  评论(0编辑  收藏  举报