1 环境准备

1.1 移除旧版本Docker

[root@root ~]# sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
已加载插件:fastestmirror
参数 docker 没有匹配
参数 docker-client 没有匹配
参数 docker-client-latest 没有匹配
参数 docker-common 没有匹配
参数 docker-latest 没有匹配
参数 docker-latest-logrotate 没有匹配
参数 docker-logrotate 没有匹配
参数 docker-engine 没有匹配
不删除任何软件包
[root@root ~]# 

1.2安装工具包

[root@root ~]# sudo yum install -y yum-utils

1.3配置docker yum源

[root@root ~]# sudo yum-config-manager \
> --add-repo \
> http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2 安装最新docker

[root@root ~]# sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2.1 设置开机自启docker

[root@root ~]# docker ps  检查docker
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@root ~]# systemctl start docker 开启docker
[root@root ~]# docker ps 说明docker安装成功
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@root ~]# systemctl enable docker 设置开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@root ~]# 

2.2配置加速器

[root@root ~]# # 配置加速
[root@root ~]# sudo mkdir -p /etc/docker
[root@root ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
> }
> EOF
{
  "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
[root@root ~]# sudo systemctl daemon-reload
[root@root ~]# sudo systemctl restart docker

3 实操-镜像

3.1搜索镜像

[root@root ~]# docker search nginx

在这里插入图片描述

3.2下载镜像

下载最新版本的镜像

[root@root ~]# docker pull  nginx 

官网: https://hub.docker.com/
查看版本号,指定版本下载镜像
下载指定版本的镜像

[root@root ~]# docker pull nginx:1.26.0

3.3查看镜像

[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    4f67c83422ec   5 days ago    188MB
nginx        1.26.0    94543a6c1aef   4 weeks ago   188MB
[root@root ~]# 

在这里插入图片描述

3.4 删除镜像

[root@root ~]# docker rmi nginx:1.26.0

或者

[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    4f67c83422ec   5 days ago    188MB
nginx        1.26.0    94543a6c1aef   4 weeks ago   188MB
[root@root ~]# docker rmi 4f67c83422ec
Untagged: nginx:latest
Untagged: nginx@sha256:0f04e4f646a3f14bf31d8bc8d885b6c951fdcf42589d06845f64d18aec6a3c4d
Deleted: sha256:4f67c83422ec747235357c04556616234e66fc3fa39cb4f40b2d4441ddd8f100
Deleted: sha256:61eeae0da48f3cc48ebf1d0a192ad8f19cb6c6ed67d0f0a7ef1f1c443c1effd7
Deleted: sha256:5cde5b697c8179331355ba55c3754bb565e74b9c84170f398a736e29e8ece1f8
Deleted: sha256:8f957df28f8990c12af00f83cbc17560d57284835f695e3b1c2a39d22f6b117d
Deleted: sha256:4421830dc9390171c909a2beb619a00bb2c4a1fb8f7e187b14a38e1f84aa76db
Deleted: sha256:b25c84fbc54990754cb7662c5f299f2e44700731f5f19e610f61eb7348c2c2fc
Deleted: sha256:23b17aa4ac172d8d931417d0c681ea8f94d26e353a3f9d2d3cc75e01fed6d99e
[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.26.0    94543a6c1aef   4 weeks ago   188MB
[root@root ~]# 


4 实操-容器

4.1运行nginx容器

[root@root ~]# docker run nginx
不指定版本会自动下载最新的镜像

4.2 查看容器

[root@root ~]# docker ps 查看正在运行的容器
CONTAINER ID   IMAGE     COMMAND                   CREATED              STATUS              PORTS     NAMES
0d5a6e9fa449   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    relaxed_napier
[root@root ~]# docker ps -a 查看全部的容器
CONTAINER ID   IMAGE     COMMAND                   CREATED              STATUS              PORTS     NAMES
0d5a6e9fa449   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    relaxed_napier
[root@root ~]# 

在这里插入图片描述

4.3启动容器

[root@root ~]# docker start 0d5a6e9fa449(容器ID)
0d5a6e9fa449
[root@root ~]# 

4.5关闭容器

[root@root ~]# docker stop  relaxed_napier
relaxed_napier
[root@root ~]# 

4.6查看容器日志

docker logs 0d5a6e9fa449(容器ID)

4.7删除容器

[root@root ~]# docker rm 0d5a6e9fa449(容器ID)
Error response from daemon: cannot remove container "/relaxed_napier": container is running: stop the container before removing or force remove
[root@root ~]# docker rm -f 0d5a6e9fa449(容器ID)强制删除
0d5a6e9fa449
[root@root ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@root ~]# 

4.8让公网可以访问到nginx

-d 后台启动
–name 指定容器的名称
-p 将主机的端口与容器的端口向映射

[root@root ~]# docker run -d --name mynginx(指定容器的ID,一个镜像可以开启多个容器) -p 80:80 nginx
26ec8493b7efa6ad322e06e9fce5446b39c00c8a501ad7c76b6c4c690158a27c
[root@root ~]# 

4.9进入容器内部

一个容器就是相当于一个独立的linux系统。

[root@root ~]# docker exec -it mynginx /bin/bash
root@26ec8493b7ef:/# ll
bash: ll: command not found
root@26ec8493b7ef:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@26ec8493b7ef:/# cd /usr 
root@26ec8493b7ef:/usr# ls

5实操-保存镜像

5.1 提交镜像

可以将一个容器进行打包
-m 描述信息

[root@root ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS                               NAMES
26ec8493b7ef   nginx     "/docker-entrypoint.…"   40 minutes ago   Up 40 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   mynginx
[root@root ~]# docker commit -m "update indx.html" mynginx nginx:v1.0
sha256:ae6baa8d8e15110c9a893ec38a49cbf3c8e02873eb3d4a12255b8e9d3c2eea36
[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        v1.0      ae6baa8d8e15   8 seconds ago   188MB
nginx        latest    4f67c83422ec   5 days ago      188MB
nginx        1.26.0    94543a6c1aef   4 weeks ago     188MB
[root@root ~]# 

5.2 保存镜像

-o 指定导出的文件名和格式

[root@root ~]# docker save -o nginx.tar nginx:v1.0
[root@root ~]# ls
nginx.tar
[root@root ~]# 

5.3加载别人的镜像

[root@root ~]# docker load -i nginx.tar
-i 指定加载的文件

[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@root ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@root ~]# ls
nginx.tar
[root@root ~]# pwd
/root
[root@root ~]# docker load -i nginx.tar 
5d4427064ecc: Loading layer [==================================================>]  77.88MB/77.88MB
37719940dcaa: Loading layer [==================================================>]  113.9MB/113.9MB
d58e4a0f2971: Loading layer [==================================================>]  3.584kB/3.584kB
10988c108f66: Loading layer [==================================================>]  4.608kB/4.608kB
7da4ba4a0030: Loading layer [==================================================>]   2.56kB/2.56kB
261a5dc153b4: Loading layer [==================================================>]   5.12kB/5.12kB
3f6a3d22b9ce: Loading layer [==================================================>]  7.168kB/7.168kB
f810041d78ee: Loading layer [==================================================>]  14.34kB/14.34kB
Loaded image: nginx:v1.0
[root@root ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        v1.0      ae6baa8d8e15   7 minutes ago   188MB
[root@root ~]# 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部