Docker的基础知识和简单应用

时间:2022-07-22
本文章向大家介绍Docker的基础知识和简单应用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文中均是采用Centos7 64为进行测试,有些命令需根据自己内核替换,如yum在ubuntu中是apt

Docker是什么

Docker最初是dotCloud公司创始人Solomon Hykes在法国期间发起的一个公司内部项目,于2013年3月以Apache2.0授权协议开源,主要项目代码在Github上进行维护。

Docker使用google公司推出的Go语言进行开发实现。

docker是linux容器的一种封装,提供简单易用的容器使用接口。是最流行的Linux容器解决方案。

docker接口相当简单,用户可以方便的创业、销毁容器。

docker将应用程序与程序的依赖,打包在一个文件里面。运行这个文件就会生成一个虚拟容器。

程序那个在虚拟容器里面,如同运行在真实物理机上运行一样,有了docker,就不用担心环境问题了。

应用场景

  • web应用的自动化打包和发布
  • 自动化测试和持续集成、发布
  • 在服务型环境中部署和调整数据库或其它应用

Docker三大概念

  • 镜像 image
  • 容器 container
  • 仓库 repository

Docker容器的优势

  1. 更高效的利用系统资源
  2. 更快速的启动时间
  3. 一致的运行环境
  4. 持续交付和部署
  5. 更轻松的迁移

Docker安装

yum install docker

Docker 启动

systemctl start docker

Docker运行状态

systemctl status docker

Docker版本

docker version

Docker命令

docker --help
Usage:  docker COMMAND

A self-sufficient runtime for containers

Options:
      # 客户端配置文件位置
      --config string      Location of client config files (default "/root/.docker")
      # 启动debug模式
  -D, --debug              Enable debug mode
      # 打印使用
      --help               Print usage
      # 守护进程
  -H, --host list          Daemon socket(s) to connect to (default [])
      # 日志级别
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      # 信任证书签名CA
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      # tls证书路径
      --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
      # 打印版本信息并且退出
  -v, --version            Print version information and quit

Commands:
  # 当前shell下attach链接指定运行镜像
  attach      Attach to a running container
  # 通过Dockerfile定制镜像
  build       Build an image from a Dockerfile
  # 提交当前容器为新镜像
  commit      Create a new image from a container's changes
  # 从容器中复制文件或目录到宿主机
  cp          Copy files/folders between a container and the local filesystem
  # 创建一个新的容器,与run相同,但是create不启动容器
  create      Create a new container
  # 查看容器变化
  diff        Inspect changes on a container's filesystem
  # 从Docker服务获取容器事实时间
  events      Get real time events from the server
  # 在已存在的容器上执行命令
  exec        Run a command in a running container
  # 导出容器的内容流作为一个tar归档文件
  export      Export a container's filesystem as a tar archive
  # 展示一个镜像形成历史
  history     Show the history of an image
  # 查看系统当前镜像
  images      List images
  # 从tar包中的内容创建一个新的文件系统映像
  import      Import the contents from a tarball to create a filesystem image
  # 显示系统相关信息
  info        Display system-wide information
  # 查看容器详细信息
  inspect     Return low-level information on Docker objects
  # 结束指定的docker容器
  kill        Kill one or more running containers
  # 从一个tar包中加载一个镜像
  load        Load an image from a tar archive or STDIN
  # 注册或登录一个docker源服务器
  login       Log in to a Docker registry
  # 从当前Docker registry中退出
  logout      Log out from a 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
  # 从docker镜像源服务器拉取指定镜像或者库镜像
  pull        Pull an image or a repository from a registry
  # 推送指定镜像或者库镜像到docker源服务器
  push        Push an image or a repository to a registry
  # 重命名容器
  rename      Rename a container
  # 重启容器
  restart     Restart one or more containers
  # 移除一个或多个容器
  rm          Remove one or more containers
  # 移除一个或多个镜像
  rmi         Remove one or more images
  # 创建一个新的容器并运行一个命令
  run         Run a command in a new container
  # 保存一个镜像为tar包
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  # 在docker hun中搜索镜像
  search      Search the Docker Hub for images
  # 启动容器
  start       Start one or more stopped containers3
  # 统计容器使用资源
  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

常用命令

01. 搜索一个镜像

# hello-world 镜像名
docker search hello-world

02. 获取一个镜像

# hello-world 镜像名
docker pull hello-world

03. 运行一个docker镜像,产生一个容器

# hello-world 镜像名
docker run hello-world

04. 查看本地所有镜像

docker image ls

# 查看docker镜像
docker images

05. 删除指定镜像

docker image rmi hello-world

06. 列出容器

# 列出正在运行的容器
docker ps
# 列出所有运行过的容器
docker ps -a

07. 基于镜像新建一个容器并启动

docker run -d centos /bin/sh  -c "while true;do echo 正在运行; sleep 1; done"

# -d 后台运行容器
# /bin/sh 指定使用centos的bash解释器
# -c 运行一段shell命令

08. 将一个stopped的容器重新启动

# 查看容器对应ID
docker ps -a
# 启动 id只需要前三位即可
docker start f58

配置加速器

# 配置加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://95822026.m.daocloud.io

# 重启docker
systemctl restart docker