欢迎来到CloudService文涵的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

Docker介绍和安装

一、准备安装环境

1)创建虚拟机
安装vmware软件。安装secureCRT或者xshell软件。
下载centos7.6地址:https://mirrors.aliyun.com/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
创建虚拟机。网络为nat模式。

2)配置网络
修改网卡:/etc/sysconfig/network-scripts/ifcfg-ens33
修改和添加如下配置:
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.88.101
NETMASK=255.255.255.0
GATEWAY=192.168.88.2
DNS=8.8.8.8

修改dns:/etc/resolv.conf
添加如下信息:
nameserver 8.8.8.8

重启网络: systemctl restart network
测试网络:ping www.baidu.com

3)关闭防火墙和selinux
systemctl disable firewalld
systemctl stop firewalld

修改selinux配置文件:/etc/selinux/config 
改为:SELINUX=disabled
让修改生效:setenforce 0

4)设置主机名
hostnamectl set-hostname hqs
修改之后:可以重新登录或直接输入bash即可立即生效。

5)更新yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-7.repo
# 清理yum
yum clean all
yum makecache

5)修改为上海时间
timedatectl set-timezone "Asia/Shanghai"
查看修改结果:timedatectl

二、设置Docker仓库

# 1)执行命令安装必要的包
# yum-utils包中提供yum-config-manager工具
# devicemapper存储驱动需要device-mapper-persistent和lvm2
yum install -y yum-utils device-mapper-persistent-data lvm2

# 2)执行命令设置Docker CE稳定版的仓库地址
# 这里选择设置为阿里巴巴的镜像仓库源
[root@hqs111 yum.repos.d]# yum-config-manager --add-repo \
  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

# 执行后再/etc/yum.repos.d目录下就多了一个docker.repo文件,它定义了仓库地址
# 默认只有稳定版被启用。
# 如果要启用Nightly版本:
yum-config-manager --enable docker-ce-nightly  (不用执行)
# 如果要启用test版本:
yum-config-manager --enable docker-ce-test   (不用执行)
# 禁用仓库方法:
yum-config-manager --disable docker-ce-test   (不用执行)
# 将阿里云的源改为docker官方源:
yum-config-manager --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo    (不用执行)

三、安装Docker CE

# 准备工作:清理老的docker
yum remove docker docker-client docker-client-latest docker-common \
   docker-latest docker-latest-logrotate docker-logrotate docker-engine

# 1)最简单的方法是执行以下命令安装最新版本的Docker CE和containerd
yum install docker-ce docker-ce-cli containerd.io
# 查看当前版本
[root@hqs111 yum.repos.d]# docker --version
Docker version 20.10.12, build e91ed57

# 2)卸载已安装的docker-ce及其关联依赖
[root@hqs111 yum.repos.d]# yum remove docker-ce
[root@hqs111 yum.repos.d]# yum remove docker-ce-cli
[root@hqs111 yum.repos.d]# yum remove docker-selinux
[root@hqs111 yum.repos.d]# yum remove docker-engine-selinux
# 检查
[root@hqs111 yum.repos.d]# docker --version     
bash: /usr/bin/docker: No such file or directory

# 3)安装指定版本的docker-ce
[root@hqs111 yum.repos.d]# yum install -y docker-ce-19.03.2-3.el7 docker-ce-cli-19.03.2-3.el7 containerd.io
[root@hqs111 yum.repos.d]# docker --version
Docker version 19.03.2, build 6a30dfc

# 4)查看可用的docker版本
# 默认是按照版本从低到高排列
[root@hqs111 yum.repos.d]# yum list docker-ce --showduplicates
# 添加sort -r 是将结果按照版本从高到低排列
[root@hqs111 yum.repos.d]# yum list docker-ce --showduplicates | sort -r
   软件包名称                  版本字符串                          仓库的名称(软件包存储的位置)
docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.5-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.2-3.el7                    @docker-ce-stable(有@说明是本机已经安装的版本)
docker-ce.x86_64            3:19.03.15-3.el7                   docker-ce-stable 
docker-ce.x86_64            3:19.03.14-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.13-3.el7                   docker-ce-stable

四、启动Docker并测试

# 启动docker服务
[root@hqs ~]# systemctl start docker
# 查看docker服务状态
[root@hqs ~]# systemctl status docker

1、测试docker

# 运行hello-world镜像
[root@hqs ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

2、卸载Docker

# 执行卸载命令
yum remove docker-ce docker-ce-cli containerd.io
# 卸载后,主机上的镜像、容器等不会被删除,删除命令如下:
rm -rf /var/lib/docker
# 管理员必须手动删除任何已编辑的配置文件

五、安装Docker之后的配置

1、配置Docker开机自动启动

[root@hqs docker]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

# 禁用开机启动:systemctl disable docker  

2、以非root用户管理docker

# 创建docker组——centos安装docker后默认有docker组
[root@hqs docker]# sudo groupadd docker
groupadd: group 'docker' already exists

# 向docker组添加用户
[root@hqs docker]# useradd hqs
[root@hqs docker]# sudo usermod -aG docker hqs

3、开启Docker远程访问

# 编辑docker.service单元配置文件
[root@hqs docker]# systemctl edit docker.service
# 输入以下信息:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0
.0.0:2375

# 重新加载systemctl配置
[root@hqs docker]# systemctl daemon-reload

# 重启docker
[root@hqs docker]# systemctl restart docker

# 检查确认Docker守护进程是否在所配置的端口上侦听
# 安装网络监测工具
[root@hqs docker]# yum install -y net-tools
# 监测docker守护进程
[root@hqs docker]# netstat -lntp | grep dockerd
tcp6       0      0 :::2375                 :::*                    LISTEN      2122/dockerd
# 在Docker客户端命令中通过-H选项指定要连接的远程主机
[root@hqs docker]# docker -H tcp://192.168.88.101:2375 info

六、Docker命令行

1、Docker命令行接口类型

  • 引擎命令行接口(Engine CLI):docker主要的命令,包含docker和dockerd命令。
  • 容器编排命令行接口(Compose CLI):Docker Compose提供,让用户构建并运行多容器应用。
  • 机器命令行接口(Machine CLI):用来配置和管理远程docker主机。
  • DTR命令行接口:部署管理docker可信注册中心。
  • UCP命令行接口:部署和管理通用面板(universal control plane)。

2、docker命令

[root@hqs docker]# docker

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                                  # 启动调试模式
  -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                        # 管容器
  context     Manage contexts                          # 管上下文
  engine      Manage the docker engine                 # 管引擎
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes                       # 管swarm节点
  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

Commands:                                                      # 操作命令列表
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile                    # 构建镜像
  commit      Create a new image from a container 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 filesystem
  events      Get real time events from the server
  exec        Run a command in a running container                # 在运行中的容器中执行命令
  export      Export a container 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.

3、docker基本语法

# 基本语法
docker [选项] 命令
# 短格式的为一个连字符(-)加上单个字符,如-d、-v。
[root@hqs docker]# docker -v
Docker version 19.03.2, build 6a30dfc

[root@hqs docker]# docker run -i -t -d ubuntu /bin/bash
# -d 参数来实现:Docker 容器在后台以守护态(Daemonized)形式运行。
# -t 选项:让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上。 
# -i 则让容器的标准输入保持打开
21810e97ff7cbc5d84476cd2ac7e8ccfa7736431abc1850ee6a10ae4ba3570ab
[root@hqs docker]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
21810e97ff7c        ubuntu              "/bin/bash"         11 seconds ago      Up 10 seconds 

# 长格式为两个连字符加上字符串,如--daemon、, --version。
[root@hqs docker]# docker --version
Docker version 19.03.2, build 6a30dfc

# 短格式的单字符选项可以组合在一起使用
[root@hqs docker]# docker run -itd ubuntu /bin/bash    
d2e3f069366bf2245a796ad436b66cdb0366a1c8e3bd3ebc9197dd6547432c2c
[root@hqs docker]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d2e3f069366b        ubuntu              "/bin/bash"         2 seconds ago       Up 2 seconds  

# 多值选项
[root@hqs docker]# docker run -a stdin -a stdout -astderr ubuntu /bin/ls
bin
boot
...
sys
tmp
usr
var
# 有时多值选项月可以使用更复杂的值字符串

任务1:docker安装好,能够运行hello-world,能开机自动启动

任务2:查看学习一个docker的常用命令,运行一个centos容器并尝试连接进去

posted on 2022-04-20 19:39  Cloudservice  阅读(53)  评论(0编辑  收藏  举报