Docker-核心篇(1)-CentOS7安装Docker社区版

411 阅读7分钟

一、Docker安装

  1. 卸载之前安装Docker
sudo yum remove docker \
          docker-client \
          docker-client-latest \
          docker-common \
          docker-latest \
          docker-latest-logrotate \
          docker-logrotate \
          docker-engine
  1. 安装yum-utils包(提供yum-config-manager实用程序)并设置稳定存储库
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. 安装Docker Engine和containerd的最新版本,或者进入下一步安装特定版本
yum install -y docker-ce docker-ce-cli containerd.io
  1. 安装完成后默认服务未启动
    • systemctl status docker:查看docker服务状态
[root@localhost vagrant]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://docs.docker.com
  1. 启动docker
    • systemctl start docker
[root@localhost vagrant]# systemctl start docker
[root@localhost vagrant]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-07-29 13:02:30 UTC; 2s ago
     Docs: https://docs.docker.com
 Main PID: 21062 (dockerd)
    Tasks: 8
   Memory: 56.2M
   CGroup: /system.slice/docker.service
           └─21062 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.611437237Z" level=info msg="schem...grpc
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.611445537Z" level=info msg="ccRes...grpc
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.611450657Z" level=info msg="Clien...grpc
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.627090140Z" level=info msg="Loadi...rt."
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.727488472Z" level=info msg="Defau...ess"
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.760258611Z" level=info msg="Loadi...ne."
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.793136460Z" level=info msg="Docke...10.7
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.793270992Z" level=info msg="Daemo...ion"
Jul 29 13:02:30 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.819195488Z" level=info msg="API l...ock"
Hint: Some lines were ellipsized, use -l to show in full.
  1. 关闭docker
    • systemctl start docker
[root@localhost vagrant]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
[root@localhost vagrant]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2021-07-29 13:02:54 UTC; 13s ago
     Docs: https://docs.docker.com
  Process: 21062 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=0/SUCCESS)
 Main PID: 21062 (code=exited, status=0/SUCCESS)

Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.727488472Z" level=info msg="Defau...ess"
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.760258611Z" level=info msg="Loadi...ne."
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.793136460Z" level=info msg="Docke...10.7
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.793270992Z" level=info msg="Daemo...ion"
Jul 29 13:02:30 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Jul 29 13:02:30 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:30.819195488Z" level=info msg="API l...ock"
Jul 29 13:02:54 localhost.localdomain systemd[1]: Stopping Docker Application Container Engine...
Jul 29 13:02:54 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:54.942850962Z" level=info msg="Proce...ed'"
Jul 29 13:02:54 localhost.localdomain dockerd[21062]: time="2021-07-29T13:02:54.943694289Z" level=info msg="Daemo...ete"
Jul 29 13:02:54 localhost.localdomain systemd[1]: Stopped Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
  1. 开机自动启动
    • systemctl enable docker
[root@localhost vagrant]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

二、Docker卸载

  1. 卸载Docker Engine、CLI和Containerd包
yum remove docker-ce docker-ce-cli containerd.io
  1. 删除所有镜像、容器和卷,主机上的映像、容器、卷或自定义配置文件不会自动删除
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
  1. 必须手动删除任何已编辑的配置文件

三、Docker镜像

3.1 官网镜像仓库

  1. 中央仓库地址:hub.docker.com/

image.png

  1. 通常选择带有Official Image为官方镜像
  2. 镜像同样也有很多版本,标识一个镜像的名称就是【官方镜像名称:tags】,例如MySQL 5.7版本就是 mysql:5.7

image.png

  1. 各个镜像的使用在详情页可以看到
  2. 但是默认的镜像仓库在国外,国内下载速度不是很理想,可以使用镜像加速器,例如阿里的镜像加速

3.2 阿里镜像仓库

  1. 阿里的镜像仓库加速服务需要登录网站后可以申请,所以镜像加速与个人绑定
  2. 镜像地址:cr.console.aliyun.com/cn-hangzhou…
  3. 通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://tvwe72le.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

四、Docker 命令

  • 帮助命令
    • docker --help
[root@localhost ~]# docker --help

Usage:	docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -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
      --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:
  container   Manage containers
  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
  volume      Manage volumes

Commands:
  attach      Attach to a running container
  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
  create      Create a new container
  diff        Inspect changes on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  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
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a 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
  pull        Pull an image or a repository from a registry
  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
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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.
  1. 启动命令:systemctl start docker
  2. 关闭命令:systemctl stop docker
  3. Docker 描述信息
    • docker info
# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc version: v1.0.0-0-g84113ee
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1127.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 487MiB
 Name: localhost.localdomain
 ID: H3NC:RHOD:UI64:ICUE:PP7J:OFPG:BMQR:OEHI:FFWL:EGR3:N7NR:Z5PW
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://tvwe72le.mirror.aliyuncs.com/
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled