一篇文章熟悉docker命令行

Docker介绍


Docker容器技术于2013年作为开源Docker引擎推出。是一个开源的应用容器引擎,基于 Go 语言并遵从Apache2.0协议开源。基于 Linux 内置的 Namespace 和 CGroup 等系统内隔离机制而抽象出来的一种轻虚拟化技术。

为什么用Docker


l 更快速的交付和部署:

对开发和运维人员来说,最希望的就是一次创建或配置,可以在任意地方正常运行 ,解决开发环境和生产环境不一致的问题。

l 更轻松的迁移和扩展:

Docker 容器几乎可以在任意的平台上运行,包括物理机、虚拟机、公有云、私有云、个人电脑、服务器等。 这种兼容性可以让用户把一个应用程序从一个平台直接迁移到另外一个。

l 更高的资源利用率(微服务)

Docker 对系统资源的利用率很高,一台主机上可以同时运行数千个 Docker 容器

Docekr应用场景


场景一:节省项目环境部署时间

1.单项目打包

每次部署项目到测试、生产等环境,都要部署一大堆依赖的软件、工具,而且部署期间出现问题几率很大,不经意就花费了很长时间。

Docker主要理念就是环境打包部署,可在任意DockerEngine运行。前期我们只需要将每个项目环境打包到镜像,push到镜像仓库,当有需要部署这个项目时,直接pull镜像启动容器,这个项目就可以访问了!一次构建多次部署,一劳永逸。

2.整套项目打包

有一个产品可以整套部署到客户那里,以往都是派一名实施工程师到客户那部署。如果用了Docker,我们可以前期将这套项目封装打包起来,实现一键部署,分分钟钟搞定,就不需要再派人过去了。比如官方的DockerCompose编排工具。

3.新开源技术试用

直接从公共镜像仓库pull项目官方做好镜像启动容器

场景二:环境一致性

Docker将项目环境打包成镜像,可以在任何DockerEngine上浪。此时Docker就是我们这些项目的基石,Docker可移植性,保持运行状态一致性

场景三:持续集成

一个项目版本快速迭代的测试场景,需要一个合理的CI(持续集成)/CD(持续部署)环境支撑。CI/CD是一个周期性自动化项目测试流程,包括构建、部署、测试、发布等工作,很少需要人工干预。

场景四:微服务

尽可能细粒度拆分业务程序架构,由多个独立服务组成业务系统。一个容器一个服务,容器之间相互隔离

场景五:弹性伸缩

容器集群化管理已经有成熟的解决方案,比如:官方的Swarm,谷歌的K8S

容器和虚拟机


虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统。在实体计算机中能够完成的工作在虚拟机中都能够实现。在计算机中创建虚拟机时,需要将实体机的部分硬盘和内存容量作为虚拟机的硬盘和内存容量。每个虚拟机都有独立的CMOS、硬盘和操作系统,可以像使用实体机一样对虚拟机进行操作。
Vm管理系统 Hyper-V、Xen以及KVM
https://docs.microsoft.com/zh…
Docker架构设计图

Docker概念

镜像(Image)

Docker 镜像(Image)就是一个只读的模板。例如:一个镜像可以包含一个完整的操作系统环境,里面仅安装了 Apache 或用户需要的其它应用程序。镜像可以用来创建 Docker 容器,一个镜像可以创建很多容器。Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。

容器(Container)

Docker 利用容器(Container)来运行应用。容器是从镜像创建的运行实例。它可以被启动、开始、停止、删除。每个容器都是相互隔离的。

文件系统隔离技术使得Docker成为了一个非常有潜力的虚拟化技术。在容器中可以对文件进行修改、删除、创建。

仓库(Registry)

仓库(Repository)是集中存放镜像文件的场所。仓库分为公开仓库(Public)和私有仓库(Private)两种形式。最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。

官方仓库地址:https://hub.docker.com

Docker安装

Windwos安装

https://docs.docker.com/docke…

Centos安装

关闭防火墙

systemctl stop firewalld

关闭selinux

获取selinux状态

[root@instance-xkxy07le ~]# getenforce
Disabled

关闭selinux

vim /etc/selinux/config
SELINUX=disabled

安装依赖包

yum install -y  yum-utils device-mapper-persistent-data lvm2

添加 yum 软件源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

查看可安装的版本

[root@instance-xkxy07le ~]# yum list docker-ce --showduplicates | sort -r
Loaded plugins: langpacks, versionlock
Installed Packages
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.4-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 

yum安装docker

1. 默认安装最新版本

yum install docker-ce

2. 安装指定版本

yum install docker-ce-3:18.09.5-3.el7

启动docker

systemctl start docker

配置阿里云加速器

https://cr.console.aliyun.com…

Docker操作镜像命令

[root@instance-xkxy07le ~]# docker image --help
Usage:    docker image COMMAND
Manage images
Commands:
  build       Build an image from a Dockerfile
  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

1.查找镜像

docker search redis

2.拉取镜像

docker pull redis

3.查看本地镜像

docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              de25a81a5a0b        2 weeks ago         98.2MB

4.删除镜像

docker image rm redis

5.镜像打包成tar文件

docker image save de25a81a5a0b -o redis.tar

6.tar文件加载到镜像中

docker image load -i redis.tar

7.给image打标签

docker image tag IMAGEID  REPOSITORY:VERSION

8.删除未使用的镜像

 docker image prune

9.Dockerfile构建镜像

docker image build

10.查看镜像的详情

docker inspect

Docker操作容器命令

[root@instance-xkxy07le ~]# docker image --help
Usage:    docker image COMMAND
Manage images
Commands:
  build       Build an image from a Dockerfile
  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

1.列出容器

[root@instance-xkxy07le ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
54e39c3af532        redis:latest        "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:6379->6379/tcp   amazing_sinoussi

docker container ps

-a :显示所有的容器,包括未运行的。
-f :根据条件过滤显示的内容。
–format :指定返回值的模板文件。
-l :显示最近创建的容器。
-n :列出最近创建的n个容器。
–no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。

输出详情介绍:

CONTAINER ID:容器 ID。
IMAGE:使用的镜像。
COMMAND:启动容器时运行的命令。
CREATED:容器的创建时间。
STATUS:容器状态。
状态有7种:
created(已创建)
restarting(重启中)
running(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)

2.创建一个新的容器并运行一个命令

[root@instance-xkxy07le ~]# docker run -p 6379:6379 -d redis:latest redis-server
54e39c3af532dc183204381a86f41ae3b7f18c754a8d5abae1f911aa83b92cea

-d, –detach=false, 指定容器运行于前台还是后台,默认为false
-i, –interactive=false, 打开STDIN,用于控制台交互
-t, –tty=false, 分配tty设备,该可以支持终端登录,默认为false
-u, –user=””, 指定容器的用户
-a, –attach=[], 登录容器(必须是以docker run -d启动的容器)
-w, –workdir=””, 指定容器的工作目录
-c, –cpu-shares=0, 设置容器CPU权重,在CPU共享场景使用
-e, –env=[], 指定环境变量,容器中可以使用该环境变量
-m, –memory=””, 指定容器的内存上限
-P, –publish-all=false, 指定容器暴露的端口
-p, –publish=[], 指定容器暴露的端口
-h, –hostname=””, 指定容器的主机名
-v, –volume=[], 给容器挂载存储卷,挂载到容器的某个目录
–volumes-from=[], 给容器挂载其他容器上的卷,挂载到容器的某个目录
–cap-add=[], 添加权限,权限清单详见:https://linux.die.net/man/7/c…
–cap-drop=[], 删除权限,权限清单详见: https://linux.die.net/man/7/c…
–cidfile=””, 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法
–cpuset=””, 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU
–device=[], 添加主机设备给容器,相当于设备直通
–dns=[], 指定容器的dns服务器
–dns-search=[], 指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件
–entrypoint=””, 覆盖image的入口点
–env-file=[], 指定环境变量文件,文件格式为每行一个环境变量
–expose=[], 指定容器暴露的端口,即修改镜像的暴露端口
–link=[], 指定容器间的关联,使用其他容器的IP、env等信息
–lxc-conf=[], 指定容器的配置文件,只有在指定–exec-driver=lxc时使用
–name=””, 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字
–net=”bridge”, 容器网络设置:
bridge 使用docker daemon指定的网桥
host //容器使用主机的网络
container:NAME_or_ID >//使用其他容器的网路,共享IP和PORT等网络资源
none 容器使用自己的网络(类似–net=bridge),但是不进行配置
–privileged=false, 指定容器是否为特权容器,特权容器拥有所有的capabilities
–restart=”no”, 指定容器停止后的重启策略:
no:容器退出时不重启
on-failure:容器故障退出(返回值非零)时重启
always:容器退出时总是重启
–rm=false, 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)
–sig-proxy=true, 设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理

2.进入容器

[root@instance-xkxy07le ~]# docker exec -it 容器名/容器id /bin/bash
root@54e39c3af532:/data# hostname
54e39c3af532 

3. 查看容器的输出日志

docker  logs  容器名

4.查看容器的资源使用

docker  stats  容器名

–no-stream

5.查看容器的具体信息

docker inspect 容器名/容器id

6.cp 拷贝文件到容器

docker cp php.ini 容器名/容器id:/

7.查看容器运行的进程

 docker top 容器名/容器

8.停止/启动/重启

docker stop/start/restart  容器名/容器

9.删除容器

docker rm  容器名/容器

10.更新容器资源限制

docker update 容器名/容器

–blkio-weight Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
–cpu-period Limit CPU CFS (Completely Fair Scheduler) period
–cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
–cpu-rt-period Limit the CPU real-time period in microseconds
–cpu-rt-runtime ALimit the CPU real-time runtime in microseconds
–cpu-shares , -c CPU shares (relative weight)
–cpus Number of CPUs
–cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
–cpuset-mems MEMs in which to allow execution (0-3, 0,1)
–kernel-memory Kernel memory limit
–memory , -m Memory limit
–memory-reservation Memory soft limit
–memory-swap Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
–restart Restart policy to apply when a container exits

数据卷

数据卷 是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性:

数据卷 可以在容器之间共享和重用
对 数据卷 的修改会立马生效
对 数据卷 的更新,不会影响镜像
数据卷 默认会一直存在,即使容器被删除

[root@instance-xkxy07le ~]# docker volume
Usage:    docker volume COMMAND
Manage volumes
Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.

创建一个数据卷

docker volume create my-vol

查看所有的 数据卷

docker volume ls

查看指定数据卷的信息

docker volume inspect my-vol

删除数据卷

docker volume rm my-vol

无主的数据卷可能会占据很多空间,要清理请使用以下命令

 docker volume prune

查看数据卷的具体信息

docker inspect web

启动一个挂载数据卷的容器

docker run  -d --name nginxweb  -p 80:80  -v /home/www:/usr/share/nginx/html  nginx 

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理