1. 卸载旧版本

Docker在CentOS上的安装

官方文档:https://docs.docker.com/engine/install/centos/

 sudo yum remove docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine

2.下载需要的安装包

sudo yum install -y yum-utils

3. 设置yum镜像仓库

默认是国外的:

sudo yum-config-manager \
   --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

其他镜像源

镜像加速器 镜像加速器地址
Docker 中国官方镜像 https://registry.docker-cn.com
DaoCloud 镜像站 http://f1361db2.m.daocloud.io
Azure 中国镜像 https://dockerhub.azk8s.cn
科大镜像站 https://docker.mirrors.ustc.edu.cn
阿里云 http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
七牛云 https://reg-mirror.qiniu.com
网易云 https://hub-mirror.c.163.com
腾讯云 https://mirror.ccs.tencentyun.com

这里使用阿里镜像

sudo yum-config-manager \
   --add-repo \
  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 更新yum软件包索引

yum makecache fast

5. 开始安装docker

sudo yum install docker-ce docker-ce-cli containerd.io

注:docker-ce 表示社区版,企业版为 docker-ee,分别下载docker的核心,客户端,以及容器,这个都安装最新版本。

其他版本

yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable

指定版本安装

yum install -y docker-ce-18.09.0-3.el7 docker-ce-cli-18.09.0-3.el7 containerd.io-1.2.0-3.el7

6. 启动docker

sudo systemctl start docker

查看如下表示启动成功

[root@hadoop101 ~]# sudo systemctl start docker
[root@hadoop101 ~]# docker version
Client: Docker Engine - Community
Version:           20.10.12
API version:       1.41
Go version:       go1.16.12
Git commit:       e91ed57
Built:             Mon Dec 13 11:45:41 2021
OS/Arch:           linux/amd64
Context:           default
Experimental:      true

Server: Docker Engine - Community
Engine:
Version:          20.10.12
API version:      1.41 (minimum version 1.12)
Go version:       go1.16.12
Git commit:       459d0df
Built:           Mon Dec 13 11:44:05 2021
OS/Arch:         linux/amd64
Experimental:     false
containerd:
Version:          1.4.13
GitCommit:       9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
runc:
Version:          1.0.3
GitCommit:       v1.0.3-0-gf46b6ba
docker-init:
Version:          0.19.0
GitCommit:       de40ad0

7. 测试

sudo docker run hello-world

[root@hadoop101 ~]# sudo 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.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

[root@hadoop101 ~]#

表示运行成功。

8. docker卸载

卸载依赖

sudo yum remove docker-ce docker-ce-cli containerd.io

删除资源

sudo rm -rf /var/lib/docker

sudo rm -rf /var/lib/containerd

9. 阿里云镜像加速服务

登录阿里控制台,在所有产品与服务中找到 容器镜像服务

在镜像工具下有个镜像加速器:

4个配置步骤如下:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://d5o9x66u.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

10.docker常用命令

帮助命令

docker version

docker info

docker -help

https://docs.docker.com/engine/reference/run/

镜像命令

docker images 查看主机上所有镜像

可选性

-a, --all             #显示所有主机上的镜像
-q, --quiet           #只显示镜像id

[root@hadoop101 ~]# docker images -aq
feb5d9fea6a5
[root@hadoop101 ~]#

docker search 搜索镜像

docker search mysql -f=STARS=5000

[root@hadoop101 ~]# docker search mysql -f=STARS=3000
NAME     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12213     [OK]
mariadb   MariaDB Server is a high performing open sou…   4689     [OK]

docker pull 下载镜像

默认下载,不指定版本,则下载最新版本

docker pull mysql

[root@hadoop101 ~]# docker pull mysql
Using default tag: latest #默认最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #docker分层下载
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实下载地址

上面真实下载地址和名称mysql相同,如下两个下载命令相同:

docker pull mysql

docker pull docker.io/library/mysql:latest

指定版本下载,要在官方文档中支持的版本中指定,最新支持版本如下

8.0.28-oracle, 8.0-oracle, 8-oracle, oracle
8.0.28, 8.0, 8, latest, 8.0.28-debian, 8.0-debian, 8-debian, debian
5.7.37-oracle, 5.7-oracle, 5-oracle
5.7.37, 5.7, 5, 5.7.37-debian, 5.7-debian, 5-debian

我们再下载一个5.7的版本

docker pull mysql:5.7

[root@hadoop101 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@hadoop101 ~]#

可以看到,在我们下载一个默认版本后,再下载一个指定版本,上面一些依赖是通用的,就提示已存在,这样就极大节省了空间,并且两个版本毫不影响。

此时我们在查看镜像时

[root@hadoop101 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql         5.7       c20987f18b13   2 months ago   448MB
mysql         latest   3218b38490ce   2 months ago   516MB
hello-world   latest   feb5d9fea6a5   5 months ago   13.3kB
[root@hadoop101 ~]#

docker rmi 删除镜像

docker rmi -f c20987f18b13 :根据镜像id删除指定镜像

docker rmi -f $(docker images -aq):删除全部镜像

容器命令

有了镜像,才可以使用容器命令,我们先下载一个linux系统

docker pull centos

创建容器并启动

文档:https://docs.docker.com/engine/reference/commandline/run/

docker run [可选参数] image

#可选参数

-- name="容器名字"

-d #后台方式运行

-it #使用交互式运行,运行并进入容器

-p #小写的p,指定容器端口,-p 8080:8080

-p ip:主机端口:容器端口

-p 主机端口:容器端口

-p 容器端口

容器端口

-P #大写的p,随机指定容器端口

运行一个容器,并进入容器

docker run -it centos /bin/bash

[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@655b77d11f20 /]#

可以看到,我们已经进入容器,并且容器的id为:655b77d11f20

退出使用 exit,表示容器停止并退出

Ctrl+p+q:容器不停止且退出

列出当前容器

docker ps:列出当前正在运行的容器

docker ps [可选参数]

-a 列出所有容器,包括正在运行的和历史运行的容器

-n 列出最近创建的容器

-q 列出容器编号

删除容器

docker rm 7e9e7a1082f8:删除指定容器id,正在运行的无法删除,-f 可强制删除

docker rm -f $(docker ps -aq):删除所有容器

docker ps -aq|xargs docker rm -f:删除所有容器

启动和停止容器

docker start 容器id   #启动一个容器
docker restart 容器id #重启一个容器
docker stop 容器id #停止一个容器
docker kill 容器id #强制停止一个容器

其他常用命令

后台启动容器

docker run -d centos

注意:这里发现centos又停止了,没有启动起来。

这是docker的一个问题,docker容器使用后台运行,就必须要有一个前台进程,例如使用这个容器的一个应用,如果没有,docker容器就会自动停止。

查看日志

docker logs -ft --tail 100 4b3bc9e6e95f

[root@hadoop101 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
bb1b1f3023a2   centos    "/bin/bash"   12 seconds ago   Up 11 seconds             trusting_beaver
[root@hadoop101 ~]# docker logs -ft --tail 10 bb1b1f3023a2

这里日志为空,下面我们创建一个脚本来显示日志,重新创建一个容器,然后执行脚本

docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"

[root@hadoop101 ~]# docker run -d centos /bin/sh -c "while true;do echo hello hahaha...;sleep 1;done"
62f18b85321a007b9e01c1cb237e9f4942b4d90dfae8ec22fceaa62ddb7de822
[root@hadoop101 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS     NAMES
62f18b85321a   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             elastic_bose
[root@hadoop101 ~]# docker logs -ft --tail 10 62f18b85321a
2022-03-06T15:38:08.383093375Z hello hahaha...
2022-03-06T15:38:09.388922480Z hello hahaha...
2022-03-06T15:38:10.395572128Z hello hahaha...
2022-03-06T15:38:11.401655410Z hello hahaha...
2022-03-06T15:38:12.410696441Z hello hahaha...
2022-03-06T15:38:13.413753657Z hello hahaha...
2022-03-06T15:38:14.427307889Z hello hahaha...
2022-03-06T15:38:15.432224741Z hello hahaha...

查看容器中的进程

docker top 630e00985bd2

[root@hadoop101 ~]# docker top 630e00985bd2
UID     PID     PPID     C     STIME     TTY     TIME         CMD
root    2033    2013     0      22:21     pts/0   00:00:00   /bin/bash
[root@hadoop101 ~]#

查看容器元数据信息

docker inspect 630e00985bd2

显示内容过长,不再展示。

进入正在运行的容器

方式一:

docker exec -it 630e00985bd2 /bin/bash

[root@hadoop101 ~]# docker exec -it 630e00985bd2 /bin/bash
[root@630e00985bd2 /]# ps
  PID TTY         TIME CMD
   15 pts/1    00:00:00 bash
   30 pts/1    00:00:00 ps
[root@630e00985bd2 /]# exit
exit
[root@hadoop101 ~]#

方式二:

docker attach 630e00985bd2

区别:docker exec表示开启一个新的终端,类似于命令行,可做常用操作,docker exec进入正在运行的终端,有输出会正常打印。

从容器内拷贝文件到主机

格式:docker cp 容器id:容器内文件目录 主机目录

docker cp fe721a2a3c06:/opt/test.txt /opt/

[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@fe721a2a3c06 /]# cd /opt
[root@fe721a2a3c06 opt]# touch test.txt
[root@fe721a2a3c06 opt]# (ctrl+p+q)
[root@hadoop101 ~]# docker cp fe721a2a3c06:/opt/test.txt /opt/
[root@hadoop101 ~]# cd /opt
[root@hadoop101 opt]# ll
total 4
drwx--x--x   4 root     root       28 Mar  6 13:24 containerd
drwxr-xr-x. 23 zhangbao zhangbao 4096 Nov 14 00:12 module
drwxr-xr-x   3 root     root       17 Nov 13 23:59 redis
drwxr-xr-x.  5 zhangbao zhangbao  286 Nov 14 00:00 software
-rw-r--r--   1 root     root        0 Mar  8 22:47 test.txt

Docker安装与基本命令使用的更多相关文章

  1. Docker安装与基本命令

    docker安装 Ubuntu 更新apt包索引 sudo apt-get update 更新apt包索引 sudo apt-get upgrade 安装docker sudo apt-get ins ...

  2. docker 安装与基本命令

    安装 Install Docker for Linux Download Docker for Mac Install Docker for Windows 镜像是docker三大核心概念中最重要的. ...

  3. docker安装和基本命令

    Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口. ...

  4. docker安装及基本命令

    Ubuntu安装docker sudo apt-get install docker.io Centos安装docker # 更新系统软件包 yum -y upgrade # 官方下载地址 curl ...

  5. Docker02 基本命令、开发环境搭建、docker安装nginx、Dockerfile、路径挂载

    1 基本命令 1.1 docker相关 centos6.5 安装docker环境 >sudo yum install -y http://mirrors.yun-idc.com/epel/6/i ...

  6. Docker环境安装,基本命令集合

    一.docker安装 1).卸载旧的安装包 centos7默认安装的docker版本是1.13.1,卸载它,安装新的版本. root用户下,一次把这坨命令复制进去 yum remove docker ...

  7. Docker安装基本命令操作,带你了解镜像和容器的概念!

    上一章节我们了解了Docker的基本概念,以及相关原理.这一章节进行实操. <Docker这么火爆.章节一:带你详尽了解Docker容器的介绍及使用> 一.Docker安装 声明:Dock ...

  8. docker版本演变,安装,基本命令

    1.docker 版本信息 Docker CE在17.03版本之前叫Docker Engine,版本号从0.1.0(2013-03-23)~1.13.1(2017-02-08),详见https://d ...

  9. docker初识-docker安装、基于docker安装mysql及tomcat、基本命令

    一.docker是什么 用go语言开发,开源的应用容器引擎,容器性能开销极低 二.整体架构图 Docker 包括三个基本概念: 镜像(Image):Docker 镜像(Image),就相当于是一个 r ...

随机推荐

  1. django之js模板插件artTemplate的使用

    安装: 方式1:artTemplate模板源码下载地址:https://aui.github.io/art-template/zh-cn/index.html 方式2:使用node.js进行安装:np ...

  2. springboot实战小项目-简要介绍、vue项目创建

    因为菜,所以要好好学习! 一.项目介绍:这是一个后台管理系统,准备实现的功能: 1.登录.注册.个人信息查看.退出登录 2.根据关键字查询用户.新增用户.根据id或者其他字段排序.编辑用户信息.删除用 ...

  3. ApacheCN 深度学习译文集 20201229 更新

    新增了七个教程: TensorFlow 和 Keras 应用开发入门 零.前言 一.神经网络和深度学习简介 二.模型架构 三.模型评估和优化 四.产品化 TensorFlow 图像深度学习实用指南 零 ...

  4. sqlserver查出来的数据丢失

    问题描述 今天利用sqlserver查数据,使用了一个ROUND(308.16000, 2)函数,发现在sqlserver工具中显示正常,但是在服务端查出来就会发生精度丢失问题. ROUND和CAST ...

  5. VMware14安装windows7的详细过程

    感谢大佬:https://blog.csdn.net/u012230668/article/details/81701893 一.安装VMware虚拟机,以及下载一份ghost win7系统 下载地址 ...

  6. 21个实用便利的PHP代码

    转载请注明来源:https://www.cnblogs.com/hookjc/ 1. PHP可阅读随机字符串 此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能. /*** ...

  7. Java微信公众号服务器配置-验证Token

    一.填写服务器配置 首先我们需要在微信公众平台上填写服务器配置 重点内容    服务器地址URL(一定要外网能访问的到)        在我们提交配置的时候,微信会发送GET请求到URL上,      ...

  8. 直播流媒体ums

    准备工具 下载  UltrantMediaServer服务器 FlashMediaLiveEncoder测试直播工具 第一步 安装 UltrantMediaServer服务器 第二步 打开网也输入   ...

  9. Centos 系统目录概述

    Linux目录一切从根目录开始,即"/",根下面的目录是一个有层次的树状结构.并且分区或磁盘是必须挂载在根目录才可以正常访问.做一个形象的比喻:目录类似一个一个的入口,而根目录则是 ...

  10. insert/delete/select/update 以及一些在select中常用的函数之类的

    crud程序员:create,read,update.delete 一.insert 细节说明: 1.插入的数据应与字段的数据类型相同.比如把'abc'添加到int类型会错误2.数据的长度应在列的规定 ...