Docker的安装配置及基础学习

Docker的安装

# 1、卸载旧版本
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# 2、需要的软件包
yum install -y yum-utils

# 3、设置镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 4、更新yum索引
yum makecache fast

# 5、安装Docker引擎及相关依赖包 社区版
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

# 6、启动Docker 
systemctl start docker

# 7、查看安装是否成功
docker version

# 8、运行hello-world
docker run hello-world

# 9、查看下载的hello-world镜像
docker images


## 卸载 Docker Engine、CLI、containerd 和 Docker Compose 软件包:
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras

## 主机上的映像、容器、卷或自定义配置文件 不会自动删除。删除所有映像、容器和 卷:

 sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd

Docker的常用命令

帮助命令

docker version			# 显示docker的版本信息
docker info				# 显示docker的系统信息,包括镜像和容器的数量
docker --help			# 帮助命令

帮助文档地址:https://docs.docker.com/reference/

镜像命令

docker images 查看所有本地的主机上的镜像
[root@yunwei_test ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   16 months ago   13.3kB

# 解释
REPOSITORY 镜像的仓库源
TAG		   镜像的标签
IMAGE	   镜像的id
CREATED    镜像的创建时间
SIZE	   镜像的大小

# 可选项
  -a, --all            	# 列出所有的镜像
  -q, --quiet           # 只显示镜像的id
docker search 搜索镜像
[root@yunwei_test ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13746     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5242      [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   728       [OK]       
percona                         Percona Server is a fork of the MySQL relati…   599       [OK]       

# 可选项,通过搜索来过滤
 --filter=STARS=3000  #搜索出来的镜像就是STARS大于3---的
 
[root@yunwei_test ~]# docker search --filter=STARS=3000 mysql
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13746     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5242      [OK]       
[root@yunwei_test ~]# docker search --filter=STARS=5000 mysql
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13746     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5242      [OK]       

docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@yunwei_test ~]# docker pull mysql
Using default tag: latest	# 如果不写tag,默认就是latest
latest: Pulling from library/mysql
39fbafb6c7ef: Pull complete	  # 分层下载,docker image的核心 联合文件系统 	
76e9f8ca4fd7: Pull complete 
3898be77c395: Pull complete 
992f720677b1: Pull complete 
9b298d35e4a8: Pull complete 
c7a7ae1e6628: Pull complete 
9501ab7b6608: Pull complete 
69d00a593dfe: Pull complete 
d5a5c1244ef5: Pull complete 
ec08e11879d6: Pull complete 
f8dbf0a1ea0e: Pull complete 
Digest: sha256:03b0af22f4df273a2b7bb6320ca305f9e0dcaebe411a1ec75e6adc9fea051aa2   # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	# 真实地址

# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest

# 指定版本下载
[root@yunwei_test ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e048d0a38742: Pull complete 
c7847c8a41cb: Pull complete 
351a550f260d: Pull complete 
8ce196d9d34f: Pull complete 
17febb6f2030: Pull complete 
d4e426841fb4: Pull complete 
fda41038b9f8: Pull complete 
f47aac56b41b: Pull complete 
a4a90c369737: Pull complete 
97091252395b: Pull complete 
84fac29d61e9: Pull complete 
Digest: sha256:8cf035b14977b26f4a47d98e85949a7dd35e641f88fc24aa4b466b36beecf9d6
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 删除镜像
[root@yunwei_test ~]# docker rmi -f 容器id	# 删除指定的容器
[root@yunwei_test ~]# docker rmi -f 容器id 容器id 容器id 容器id # 删除多个容器
[root@yunwei_test ~]# docker rmi -f $(docker images -aq ) # 删除全部的容器

容器命令

说明:有了镜像之后才能创建容器,下载一个centos镜像进行测试。
docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
新建容器并启动
docker run [可选参数] image

# 参数说明
--name="Name"	容器的名字 tomcat1 tomcat2 用来区分容器
-d				以后台方式运行容器
-it				以交互方式运行容器
-p				指定容器的端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
-P				随机指定容器端口

# 测试,启动并进入容器
[root@yunwei_test ~]# docker run -it centos /bin/bash
[root@901e5a83ab29 /]# ls  # 查看容器内的centos,基础版本,很多命令都不完善
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器中退回主机
[root@901e5a83ab29 /]# exit
exit
[root@yunwei_test /]#ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

列出所有运行的容器
# docker ps 命令
   # 列出当前正在运行的容器+历史运行过的容器
-a # 列出当前正在运行的容器+历史运行过的容器
-n=# 显示最近创建的容器
-q # 只显示容器的编号

[root@yunwei_test /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@yunwei_test /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
901e5a83ab29   centos         "/bin/bash"   30 minutes ago   Exited (0) 28 minutes ago             upbeat_solomon
f11c4f1a8f46   feb5d9fea6a5   "/hello"      6 hours ago      Exited (0) 6 hours ago   

退出容器
exit  # 直接退出并停止容器
Ctrl + P + Q  # 退出但不停止容器
删除容器
docker rm 容器id	# 删除指定的容器,不能删除正在运行的容器,但如果需强制删除则 rm -f
docker rm -f $(docker ps -aq)	# 删除所有的容器
docker ps -a -q |xargs docker rm # 删除所有的容器
启动和停止容器的操作
docker start 容器id		# 启动容器
docker restart 容器id		# 重启容器
docker stop 容器id		# 停止当前正在运行的容器
docker kill 容器id		# 强制停止当前运行的容器

常用其他命令

后台启动容器
# 命令 docker run -d 镜像名
[root@yunwei_test ~]# docker run -d centos
cc078d471344f8ee68318853622bad6a8ec077479658709a82edf1c6888d000b

[root@yunwei_test ~]# docker ps  # docker ps后发现docker停止了
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# docker 容器使用后台运行时,必须要有一个前台进程,docker发现没有应用,就会自动停止
# ningx 容器启动后,发现没有提供服务,就会立刻停止。

查看日志
docker logs -f -t --tail 容器id 

# 编写一段shell测试脚本
[root@yunwei_test ~]# docker run -d centos /bin/bash -c 'while true; do echo Myth;sleep 1;done'

# [root@yunwei_test ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
f7d44d4e642a   centos    "/bin/bash -c 'while…"   3 minutes ago   Up 3 minutes             frosty_visvesvaraya

# 显示日志
 -tf			# 显示日志带上时间戳
 --tail number	# 显示日志最后多少条日志
 [root@yunwei_test ~]# docker logs -tf --tail 10 f7d44d4e642a
2023-02-03T02:18:09.036768384Z Myth
2023-02-03T02:18:10.039162872Z Myth
2023-02-03T02:18:11.043371114Z Myth
2023-02-03T02:18:12.047663981Z Myth
2023-02-03T02:18:13.051700874Z Myth
2023-02-03T02:18:14.054361646Z Myth
2023-02-03T02:18:15.056890565Z Myth
2023-02-03T02:18:16.060914018Z Myth
2023-02-03T02:18:17.063585858Z Myth
2023-02-03T02:18:18.067941296Z Myth
2023-02-03T02:18:19.071049640Z Myth
2023-02-03T02:18:20.073741441Z Myth
查看容器中进程信息
# 命令 docker top 容器id
[root@yunwei_test ~]# docker top f7d44d4e642a
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15383               15364               0                   10:15               ?                   00:00:00            /bin/bash -c while true; do echo Myth;sleep 1;done
root                15923               15383               0                   10:23               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看cpu状态
[root@yunwei_test ~]# docker stats
查看镜像的元数据
# 命令 docker inspect 容器id

# 测试
[root@yunwei_test ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
f7d44d4e642a   centos    "/bin/bash -c 'while…"   11 minutes ago   Up 11 minutes             frosty_visvesvaraya
[root@yunwei_test ~]# docker inspect f7d44d4e642a 
[
    {
   
        "Id": "f7d44d4e642a1a4bd2a1870c6a364a09d69a687de6adc59147f6bf68dae0e42e",
        "Created": "2023-02-03T02:15:45.923946588Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true; do echo Myth;sleep 1;done"
        ],
        "State": {
   
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值