首页 > 其他分享 >Docker介绍和安装

Docker介绍和安装

时间:2023-05-22 10:06:25浏览次数:30  
标签:-- root ce yum 介绍 Docker 安装 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

Docker介绍和安装_Docker

修改selinux配置文件:/etc/selinux/config  

改为:SELINUX=disabled

让修改生效:setenforce 0

Docker介绍和安装_linux_02

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容器并尝试连接进去

标签:--,root,ce,yum,介绍,Docker,安装,docker
From: https://blog.51cto.com/u_15130867/6320856

相关文章

  • Linux安装Ubuntu镜像,常用命令、IP配置等
    Ubuntu1.系统安装ubuntu安装步骤:部分版本分辨率问题显示不全,可以先试用,点击右上角修改分辨率,再安装2.登录ubuntu只能登录创建系统的用户为root修改密码:sudopasswdroot切换root用户:1.sudo-i-uroot2.sudosu3.配置IP安装后尝试pingwww.baidu.com(安装后自动获......
  • Linux 安装配置Java
    1、下载文件Javajdk-8u371-linux-x64.tar.gz 2、上传并解压文件#tar-zxvfjdk-8u371-linux-x64.tar.gz 3、配置环境变量#JavaexportJAVA_HOME=/usr/local/jdk1.8.0_371exportPATH=$JAVA_HOME/bin:$PATHexportCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/l......
  • 搜狗输入法介绍与分析
      关于对我电脑上的搜狗输入法的使用评价搜狗输入法是一款中文输入法,其主要优点和特点包括:用户选择:搜狗输入法拥有丰富的输入方式,包括拼音、五笔、手写、语音等多种输入方式,满足了用户不同习惯和需求的选择。记住用户选择:搜狗输入法会根据用户的使用习惯和输入历史记录记......
  • iOS应用通过地址(URL)安装
    目标:iOS应用通过URL地址来安装准备:1.bundleidentifier2.应用图标3.下载地址  完整的下载地址是itms-services://?action=download-manifest&url=https://www.yourdomain.cn/download/2.1.0-10637/test.116d7.56d82df.20170122.test.domain.cn.ipa.plist其中,配置test.116d7.56d8......
  • 微服务与springcloud的介绍
    1.什么是微服务?随着互联网行业的发展,对服务的要求也越来越高,服务架构也从单体架构逐渐演变为现在流行的微服务架构。微服务是一种经过良好架构设计的分布式架构方案。微服务的上述特性其实是给分布式架构制定一个标准,进一步降低服务之间的耦合,提供服务的独立性和灵活性。做到高......
  • Linux-yum-ftp服务安装与配置
    1.为什么使用ftp安装ftp主要是还为了传输文件,但是我有一台服务器或者计算机,直接登录进去获取文件不行吗?为啥还要多此一举用ftp呢?原因是用ftp的话,它的会话是持久的,只有一次认证过程,传输多个文件都是使用同一个连接。因为ftp就是为远程文件交互而设计的,更适;而且有些时候只是为了......
  • nacos 安装和使用
    Nacos是阿里巴巴开源项目,用于构建微服务应用的服务发现、配置管理和服务管理。在微服务项目中不同模块之间服务调用时,实现服务注册与发现。Nacos使用:Nacos是java开发的,依赖Java环境来运行Nacos的下载https://github.com/alibaba/nacos/releases/download/1.4.3/na......
  • 如何在UBUNTU中卸载 OpenJDK,安装 Oracle JDK 17
    可以按照以下步骤在Ubuntu中卸载OpenJDK:1.打开终端并输入以下命令,列出您系统上已安装的Java软件包:```dpkg--list|grep-Ei"jdk|jre"```该命令会显示您系统上已安装的Java软件包的清单。2.选择您想要卸载的Java软件包并输入以下命令进行卸载。例如,如......
  • 最快Android模拟器Genymotion的安装
        在这开发Android的一段时间内,一直是使用真机进行测试的,使用过的机子包括华为U8825d,红米,MX3.深知Android的最大问题就是碎片化,就是各种适配问题,甚是头疼。也是一直听说Genymotion的快速与强大,也是亲眼见过别人使用Genymotion,完全把Android自带的AVD碾压的渣都不剩。所以......
  • Dockerfile练习二---------------------------构建自己的mysql镜像
    构建自己的mysql镜像 ##NYSQL的shell安装脚本##[root@dockertomcat]#cd~[root@docker~]#mkdirmysql[root@docker~]#cdmysql/[root@dockermysql]#vimrun.sh#!/bin/bashyum-yinstall\ncurses\ncurses-devel\bison\cmake\make\gcc\gcc-c++cd/optcdmysql-5......