CentOS 的Docker 安装与使用

CentOS 的Docker 安装与使用

Docker 解决的问题

1.同步开发环境与生产环境

解决项目在我电脑上可以运行!但是在生产环境不能运行的问题。

2.更加方便的搭建部署环境

解决配置超级麻烦的应用环境问题,比如集群…。
解决环境不能跨平台,从Windows发布到Linux环境。

官网

在这里插入图片描述
官网地址:https://www.docker.com/
文档地址:https://docs.docker.com/
仓库地址:https://hub.docker.com/

Docker 安装

Docker 的组成

在这里插入图片描述
image(镜像): docker镜像就像是一个模版,可以通过这个模版来创建容器服务。通过这个镜像可以创建多个容器。
container(容器): 可以理解为简易的linux系统
repository(仓库): 存放镜像的地方。仓库分为公有仓库和私有仓库。

安装Docker

环境准备

系统:CentOS 7

[root@localhost home]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

内核:3.10.0-1160.42.2.el7.x86_64

[root@localhost home]# uname -r
3.10.0-1160.42.2.el7.x86_64
安装

1.卸载旧的版本

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

2.安装docker需要的工具包

yum install -y yum-utils

3.设置docker镜像地址

# 默认国外的地址
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

# 推荐使用阿里云镜像地址
yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4.安装docker相关的包

yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

5.启动docker

systemctl start docker

6.使用命令验证是否安装成功

[root@localhost home]# docker version
Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:04:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:02:38 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.10
  GitCommit:        770bd0108c32f3fb5c73ae1264f7e503fe7b2661
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

7.安装 hello-world 镜像 测试

docker run hello-world

8.查看是否安装成功

docker images

Docker的常用命令

Docker的帮助命令

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

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

镜像命令

docker images: 查看本地的镜像

[root@localhost ~]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB

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

# 可选项
# -a			列出所有镜像
# -q			只显示镜像的id

docker seach:搜索镜像

[root@localhost ~]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        17721     [OK]       
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   181                  
bitnami/nginx                                     Bitnami nginx Docker Image                      143                  [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   67                   
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   22                   [OK]
rancher/nginx-ingress-controller                                                                  11                   

# 解释
# NAME			镜像名称
# DESCRIPTION	镜像描述
# STARS			镜像星的数量
# OFFICIAL		镜像是否为正式版
# AUTOMATED		镜像是否自动更新版本

# 可选项
# -f, --filter filter   过滤搜索的结果
# --limit int       	设置查询结果最大的条数

docker pull:下载镜像

[root@localhost ~]# docker pull nginx
Using default tag: latest			# 默认latest 下载最新版
latest: Pulling from library/nginx
a603fa5e3b41: Pull complete 		# 分层下载
c39e1cda007e: Pull complete 
90cfefba34d7: Pull complete 
a38226fb7aba: Pull complete 
62583498bae6: Pull complete 
9802a2cfdb8d: Pull complete 
Digest: 
# 签名
sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest 		# 真实地址

docker rmi : 删除镜像

[root@localhost ~]# docker rmi -f 镜像id						# 移除单个镜像
[root@localhost ~]# docker rmi -f 镜像id 镜像id 镜像id		# 移除多个镜像
[root@localhost ~]# docker rmi -f $(docker images -aq)		# 移除全部镜像

容器命令

有了镜像才能创建容器

# 下载 centos 镜像
docker pull centos

新建容器并启动

docker run [可选参数] image

# 参数说明
# --name="容器名称"			给容器取得名字
# -d						后台方式运行
# -it						使用交互方式运行,进入容器后可以查看操作内容
# -p						容器的端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
	容器端口
# -P						随机端口

# 测试启动并进去容器
[root@localhost ~]# docker run -it centos /bin/bash

退出命令

# 容器退出并停止
exit
# 容器退出不停止
ctrl + P + Q

docker ps:查看运行的容器

# 查看当前运行
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
c921bc3bdeb4   centos    "/bin/bash"   36 seconds ago   Up 34 seconds             keen_boyd

# 查看所有历史运行
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS     NAMES
9bf115c58244   centos         "/bin/bash"              3 minutes ago   Exited (0) 2 minutes ago             gracious_tu

# 可选项
# -a 	列出当前运行加历史运行过的容器
# -q 	只列出容器编号
# -n=10	累出最近多少条的记录

删除容器

docker rm 容器id					# 删除指定容器id的容器,不能删除运行中的容器	
docker rm -f $(docker ps -aq)	# 删除所有容器

启动和停止容器

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

常用的其他命令

后台启动容器

docker run -d 镜像名称

查看日志

docker logs 容器id

# 可选项
# --details        		显示日志的额外信息
# -f, --follow         	跟随日志输出
#     --since string   	显示自时间戳以来的日志
# -n, --tail string    	显示从日志末尾倒数的行数,默认全部
# -t, --timestamps     	显示时间

查看镜像的元数据

docker inspect 镜像id

进入当前运行的容器

# 进入容器
docker exec -it 容器id /bin/bash		进入容器开启一个新的终端,操作内部

docker attach 容器id 				进入容器查看内部正在执行的终端

从容器内拷贝到主机

docker cp 容器id:容器内的路径 主机的路径
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lv_longcheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值