Docker01-介绍、安装与使用


前言

一款产品从开发到上线,从操作系统,到运行环境,再到应用配置。作为开发+运维之间的协助我们需要关心的问题很多,这也是很多软件公司不得不面对的问题,特别是各种版本的迭代之后,不同版本环境的兼容,对运维人员都是考验。

Docker之所以发展如此迅速,针对运维它给出了一个标准化的解决方案。所以,现在Docker成为了开发和运维必备的技能之一,极大便利的帮助解决了应用的环境和版本迭代问题。

本系列文章让你轻松进入docker的世界!


 

一、Docker定义

docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

 

二、Docker优点

1、更快速的交付和部署

传统:一堆帮助文档、安装程序;

Docker:打包镜像,测试发布,一键运行;

2、更便捷的升级和扩缩容

传统:安装环境、配置参数、应用打包部署,重新来一遍;

Docker:我们部署应用就和积木一样!项目打包为一个镜像,扩展 服务器A,直接在服务器B上运行 镜像即可,无须搭建环境,打包部署,完成扩容!

3、更简单的系统运维 

传统:开发、测试、UAT、生产各个环境存在一直性差异,经常会导致意想不到的问题,这些都是无意义的问题;

Docker:各个开发环境高度一致化,避免环境带来的问题干扰;

4、更高效的计算资源利用

传统:存在部分应用,部署后,导致不能部署第二个相同应用;

Docker:Docker是内核级别的虚拟化,可以在一个物理机上运行很多的容器示例,服务期被压榨到极致;

 

三、Docker组成

Docker是一个client-server结构的系统,docker-server启动完毕后,会启动一个守护进程,通过socket来接收docker-client的命令,从而在server上执行命令所要执行的操作!

镜像(Image)

Docker镜像就好比一个模板,可以通过这个模板来创建容器服务,tomcat镜像===> run ===> tomcat01容器(提供服务),通过这个镜像可以创建多个容器(最终服务或项目运行就是在容器内);

 

容器(Container):

Docker利用容器技术,独立运行一个或者一个组的应用,通过镜像来创建的;

启动、停止、删除,基本操作命令;

目前就可以把容器理解为一个简易的linux系统 + 应用 + 环境;

 

仓库(Repository):

仓库就是存放镜像的地方,库分为共有仓库和私有仓库;

Docker Hub(默认是国外的),阿里云...等都有容器服务器(配置镜像加速);

 

三、Docker安装

服务器常用CentOS,这里以CentOS 7系统上安装Docker为示例。

建议服务器在安装后,切换yum源,采用国内163或者阿里的yum源,安装应用会快很多。

 

1、检查系统内核

最新docker安装要求centos的内核版本在3.10以上

[root@localhost ~]# uname -r
3.10.0-1160.6.1.el7.x86_64

2、使用yum安装docker

  • yum更新(可略过,内核版本不够时需要)
yum -y update
  • docker安装
yum -y install docker
  • docker启动
systemctl start docker.service

可能报错Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

解决方法:执行 vi /etc/sysconfig/selinux , 把 selinux 属性值改为disabled。然后重启系统,docker就可以启动啦

 

  • 开机自启动
systemctl enable docker.service
  • 配置docker镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

镜像加速的地址请设置成阿里的地址("https://xxx.mirror.aliyuncs.com"是随手写的地址),请自己登录阿里云获取自己分配的加速地址,这样docker下载应用会快很多。

阿里云镜像加速地址获取:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

最后使用 docker info查看配置是否生效;(信息最后又显示镜像地址的属性,看是否已经修改为自己的阿里云镜像加速地址);

docker info

 

四、Docker使用

docker的使用基本都是使用docker提供的命令为主。下面是docker一些常用命令的分类和讲解

1、帮助命令

命令说明
docker version查看docker版本信息
docker info查看docker系统信息(镜像,容器,扩卷,宿主机信息,镜像地址等)
docker help 命令docker命令的解析说明
docker status 容器id容器的进程信息

 

重点介绍docker help命令

docker help是一个帮助命令,所用docker的操作命令都可以使用help进行查看,是大家应该需要重点理解的一个命令。

[root@MiWiFi-R4-srv v2]# docker help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")  #客户端配置文件的位置
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode  #启用Debug调试模式
  -H, --host list          Daemon socket(s) to connect to  #守护进程的套接字(Socket)连接
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")  #设置日志级别
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem") #信任证书签名CA
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem") #TLS证书文件路径
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem") #TLS密钥文件路径
      --tlsverify          Use TLS and verify the remote #使用TLS验证远程
  -v, --version            Print version information and quit #打印版本信息并退出

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.8.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container  #当前shell下attach连接指定运行镜像
  build       Build an image from a Dockerfile   #通过Dockerfile定制镜像
  commit      Create a new image from a container's changes #提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem  #从容器中拷贝指定文件或者目录到宿主机中
  create      Create a new container #创建一个新的容器,同run 但不启动容器
  diff        Inspect changes to files or directories on a container's filesystem #查看docker容器变化
  events      Get real time events from the server  #从docker服务获取容器实时事件
  exec        Run a command in a running container  #在已存在的容器上运行命令
  export      Export a container's filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import)
  history     Show the history of an image  #展示一个镜像形成历史
  images      List images #列出系统当前镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export)
  info        Display system-wide information  #显示系统相关信息
  inspect     Return low-level information on Docker objects  #查看容器详细信息
  kill        Kill one or more running containers  #kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save)
  login       Log in to a Docker registry #注册或者登录一个docker源服务器
  logout      Log out from a Docker registry #从当前Docker registry退出
  logs        Fetch the logs of a container #输出当前容器日志信息
  pause       Pause all processes within one or more containers  #暂停容器
  port        List port mappings or a specific mapping for the container  #查看映射端口对应的容器内部源端口
  ps          List containers #列出容器列表
  pull        Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像
  push        Push an image or a repository to a registry  #推送指定镜像或者库镜像至docker源服务器
  rename      Rename a container #重命名容器
  restart     Restart one or more containers #重启运行的容器
  rm          Remove one or more containers #移除一个或者多个容器
  rmi         Remove one or more images  #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
  run         Run a command in a new container   #创建一个新的容器并运行一个命令
  save        Save one or more images to a tar archive (streamed to STDOUT by default) #保存一个镜像为一个tar包(对应load)
  search      Search the Docker Hub for images  #在dockerhub中搜索镜像
  start       Start one or more stopped containers #启动容器
  stats       Display a live stream of container(s) resource usage statistics  #统计容器使用资源
  stop        Stop one or more running containers #停止容器
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE #给源中镜像打标签
  top         Display the running processes of a container #查看容器中运行的进程信息
  unpause     Unpause all processes within one or more containers  #取消暂停容器
  update      Update configuration of one or more containers  # 更新容器的配置文件
  version     Show the Docker version information #查看容器版本号
  wait        Block until one or more containers stop, then print their exit codes #截取容器停止时的退出状态值

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

To get more help with docker, check out our guides at https://docs.docker.com/go/guides

通过docker help comand可以常看docker命令的具体使用方法(尤其是参数选项说明)

使用示例:docker help ps

[root@MiWiFi-R4-srv v2]# docker help ps

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -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 container IDs
  -s, --size            Display total file sizes

-a:显示所用的容器

-q:只显示容器的ID

 

2、镜像命令

操作命令说明
检索docker search 关键字我们经常去docker hub上检索镜像的详细信息,如镜像的TAG
拉取docker pull 镜像名:tag:tag是可选的,tag表示标签,多为软件的版本,默认是latest
列表docker images查看本地所有镜像
删除docker rmi image-id删除指定的本地镜像

镜像查看地址:https://hub.docker.com/

 

3、容器命令

命令说明
docker run args 镜像名称根据镜像启动容器
docker ps查看运行中的容器
docker stop 容器id停止运行中的容器
docker ps ‐a查看所有的容器
docker start 容器id启动容器
docker rm 容器id删除一个容器
docker logs 容器id查看容器的日志(-f:不断显示)
docker exec -it 容器id /bin/bash进入容器交互式界面
docker commit 容器id 镜像名称提交容器为新的镜像

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值