Docker学习笔记(一):安装和基础用法

一、安装

1.1 在centos7上安装docker-ce

  • 检查系统和内核版本
[root@myhost img1]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@myhost img1]# uname -r   #内核版本必须在3.10以上,centos7刚好符合。
3.10.0-514.21.1.el7.x86_64
  • 依次执行以下命令
#添加软件包源
cd /etc/yum.repo.d/ wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo sed -i 's//https://mirrors.tuna.tsinghua.edu.cn/docker-ce/g' docker-ce.repo
#查看是否生效 [root@myhost yum.repos.d]# yum repolist 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile 源标识 源名称 状态 docker-ce-stable/x86_64 Docker CE Stable - x86_64 46
#安装 [root@myhost yum.repos.d]# yum install docker-ce -y

  至此,Docker安装完成。

1.2 配置Docker镜像加速

  • 在拉取镜像时指定镜像源地址(仅当前命令有效)
docker pull registry.docker-cn.com/myname/myrepo:mytag

  

  • 使用 –registry-mirror 配置 Docker 守护进程 (仅当前进程有效)
docker --registry-mirror=https://registry.docker-cn.com daemon

  

  • 修改 /etc/docker/daemon.json 文件(永久有效)
添加如下内容:
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

#需要重启服务生效
systemctl restart docker.service 

  

二、基础用法

2.1 Docker容器的状态机

一个容器可能处于的状态有如下几种:

  • created:已经被创建,但是还没有被启动 (使用 docker ps -a 命令可以列出)
  • running:运行中 (使用 docker ps 命令还列出)
  • paused:容器的进程被暂停了
  • restarting:容器的进程正在重启过程中
  • exited:上图中的 stopped 状态,表示容器之前运行过但是现在处于停止状态(要区别于 created 状态,它是指一个新创出的尚未运行过的容器)。可以通过 start 命令使其重新进入 running 状态
  • destroyed:容器被删除了,再也不存在了

可通过如下命令查看其状态:

docker inspect 容器名称
       
   "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2019-07-24T02:00:25.879483514Z", "FinishedAt": "2019-07-24T02:00:28.685934396Z" },

 

2.2 命令总结

  • 总体操作
[root@myhost ~]# docker --help

Usage:	docker [OPTIONS] COMMAND  #语法格式

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -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")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds                 #管理创建
  config      Manage Docker configs         #管理配置
  container   Manage containers             #管理容器
  engine      Manage the docker engine      #管理容器引擎
  image       Manage images                 #管理镜像
  network     Manage networks               #管理网络
  node        Manage Swarm nodes
  plugin      Manage plugins
  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

  

  • 对镜像的操作
[root@myhost ~]# docker image --help
Usage:	docker image COMMAND #语法格式
Manage images

Commands:
  build       Build an image from a Dockerfile   #读取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

Run 'docker image COMMAND --help' for more information on a command.
  • 对容器的操作
[root@myhost ~]# docker container --help
Usage:	docker container COMMAND  #语法格式

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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         #创建一个容器
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container         #获取容器日志
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container      #创建并运行一个容器
  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   #停止一个运行状态的容器
  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
  wait        Block until one or more containers stop, then print their exit codes

 

  • 对容器状态改变的命令
[root@myhost ~]# docker create --name web training/webapp  #创建名字为 web 的容器
7465f4cb7c49555af32929bd1bc4213f5e72643c0116450e495b71c7ec128502

[root@myhost ~]# docker inspect --format='{{.State.Status}}' web #其状态为 created
created 

[root@myhost ~]# docker start web  #启动容器
web 

[root@myhost ~]# docker exec -it web  /bin/bash #在容器中运行 bash 命令 

[root@myhost ~]# docker inspect --format='{{.State.Status}}' web #其状态为 running
running 

[root@myhost ~]# docker pause web #暂停容器
web 

[root@myhost ~]# docker inspect --format='{{.State.Status}}' web  #状态为暂停
paused 

[root@myhost ~]# docker unpause web #继续容器
web 

[root@myhost ~]# docker inspect --format='{{.State.Status}}' web   #状态为运行
running 

[root@myhost ~]# docker rename web newweb #重命名 

[root@myhost ~]# docker inspect --format='{{.State.Status}}' newweb   #状态为运行
running 

[root@myhost ~]# docker top newweb #在容器中运行 top 命令
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5009                4979                0                   16:28               ?                   00:00:00            python app.py

[root@myhost ~]# docker logs newweb #获取容器的日志
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 

[root@myhost ~]# docker stop newweb #停止容器
newweb 

[root@myhost ~]# docker inspect --format='{{.State.Status}}' newweb
exited 

[root@myhost ~]# docker rm newweb #删除容器
newweb31

[root@myhost ~]# docker inspect --format='{{.State.Status}}' newweb
Error: No such image, container or task: newweb

  

  

  

2.1 Docker平台的构成

 

Docker 平台基本上由三部分组成:

  1. 客户端:用户使用 Docker 提供的工具(CLI 以及 API 等)来构建,上传镜像并发布命令来创建和启动容器
  2. Docker 主机:从 Docker registry 上下载镜像并启动容器
  3. Docker registry:Docker 镜像仓库,用于保存镜像,并提供镜像上传和下载

参考链接:

  https://www.cnblogs.com/sammyliu/p/5875470.html

  https://docs.docker.com/

原文地址:https://www.cnblogs.com/ltlinux/p/11236419.html