Docker-CE 安装 CentOS

Posted LiuJun2Son

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-CE 安装 CentOS相关的知识,希望对你有一定的参考价值。

1.查看可用的 CentOS 版本

1.访问 CentOS 镜像库地址:https://hub.docker.com/_/centos?tab=tags&page=1。
点击右上角的:View Available Tags 之后便可以查看 CentOS 其他的版本

2.使用 docker 命令来查看 CentOS 其他的版本

[root@VM_0_6_centos docker]# docker search centos8
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
2233466866/centos8                   https://hub.docker.com/r/2233466866/centos8     4                                       
roboxes/centos8                      A generic CentOS 8 base image.                  2                                       
sirajahamed/centos8                                                                  0                                       
ecpe4s/centos8-spack                                                                 0                                       
thinakar/centos8                     centos8 core with net-utils, vim, sshuttle      0                                       
nmstate/centos8-nmstate-dev                                                          0                                       
memiiso/centos8-java8python3docker   centos8-java8python3docker                      0                                       
memiiso/centos8-java8python3         centos8 With java8 and python3                  0                                       
prestocpp/centos8-gcc9                                                               0                                       
wuykimpang/centos8-extended                                                          0                                       
memiiso/centos8-java8                centos8-java8                                   0                                       
alekseychudov/centos8-systemd        CentOS 8 based systemd image                    0                                       
sharpreflections/centos8-build       CentOS8 with development tools                  0                                       
alazartech/centos8                                                                   0                                       
ydli/centos8                         centos8.2 with ansible-2.9.10, awscli-1.18.80                                       
hyperglance/centos8                                                                  0                                       
anthrax/centos8                                                                      0                                       
ecpe4s/centos8-x86_64                                                                0                                       
8378006361/centos8.2.2004                                                            0                                       
vglad/centos8                        Based on official latest CentOS 8 image with0                                       
loging/centos8-python37              centos8 + python3.7.5                           0                                       
krestomatio/centos8-minimal          Testing a Centos 8 minimal container image l…   0                                       
ecpe4s/centos8-base                                                                  0                                       
x97epe/centos8_httpd_wp_host                                                         0                                       
x97epe/centos8_phpfpm    

2.拉取指定版本的 CentOS 镜像

docker pull centos:latest // 拉取最新的版本
docker pull centos:centos7 // 拉取 centos7 版本
docker pull centos:centos8 // 拉取 centos8 版本
docker pull centos:centos8.2.2004 // 拉取 centos8.2.2004 版本

3.拉取最新版本的 CentOS 镜像

[root@VM_0_6_centos docker]# docker pull centos:latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete 
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@VM_0_6_centos docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        10 days ago         215MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
[root@VM_0_6_centos docker]# 

4.运行容器

运行容器的命令

 docker run -itd --name centos-test centos:latest

运行容器shell脚本

docker run \\
  -it \\	
  --name centos-test \\
  -d centos:latest

docker run -itd --name centos-test centos:latest
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-d: 后台运行容器,并返回容器ID;
–name=“nginx-lb”: 为容器指定一个名称;

[root@VM_0_6_centos docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        10 days ago         215MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
[root@VM_0_6_centos docker]# docker run -itd --name centos-test centos:latest
43da5cab96653c82f41a8738a9f0bbe55031333926ae13f107daf09394ae3a1c

5.查看已经运行的容器

[root@VM_0_6_centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
43da5cab9665        centos:latest       "/bin/bash"         8 seconds ago       Up 8 seconds                            centos-test
[root@VM_0_6_centos docker]# 

6.停止运行中的容器

docker stop 容器ID

[root@VM_0_6_centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
43da5cab9665        centos:latest       "/bin/bash"         13 minutes ago      Up 13 minutes                           centos-test
[root@VM_0_6_centos docker]# docker stop 43da5cab9665 
43da5cab9665
[root@VM_0_6_centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@VM_0_6_centos docker]# 

7.重启已停止的容器

docker start 容器ID

[root@VM_0_6_centos docker]# docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
43da5cab9665        centos:latest       "/bin/bash"         14 minutes ago      Exited (0) About a minute ago                       centos-test
661f0fb866e5        hello-world         "/hello"            53 minutes ago      Exited (0) 53 minutes ago                           elated_hertz
7cadf3f486f9        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                        trusting_liskov
9e49c5da5e19        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                        epic_fermat
257d854e67ea        hello-world         "/hello"            2 hours ago         Exited (0) 2 hours ago                              gracious_hugle
dabefc346703        hello-world         "/hello"            2 hours ago         Exited (0) 2 hours ago                              eloquent_easley
[root@VM_0_6_centos docker]# docker start 43da5cab9665
43da5cab9665
[root@VM_0_6_centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
43da5cab9665        centos:latest       "/bin/bash"         14 minutes ago      Up 4 seconds                            centos-test
[root@VM_0_6_centos docker]# 

8.进入 CentOS 容器

通过 exec 命令进入 CentOS 容器

在容器 centos:latest 中开启一个交互模式的终端
docker exec -i -t centos-test /bin/bash

-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端
/bin/bash : 执行 bash

[root@VM_0_6_centos docker]# docker start centos-test
centos-test
[root@VM_0_6_centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
43da5cab9665        centos:latest       "/bin/bash"         22 minutes ago      Up 2 seconds                            centos-test
[root@VM_0_6_centos docker]# docker exec -i -t  centos-test /bin/bash
[root@43da5cab9665 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@43da5cab9665 /]# 

9.查看CentOS的版本

[root@43da5cab9665 /]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core) 
[root@43da5cab9665 /]# 

执行: exit 推出 bash

10.将Centos镜像保存成 tar 归档文件

docker save -o centos8.tar centos:latest

-o :输出到的文件。

[root@VM_0_6_centos images-bak]# pwd
/var/lib/docker/images-bak
[root@VM_0_6_centos images-bak]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        10 days ago         215MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
[root@VM_0_6_centos images-bak]# docker save -o centos8.tar centos:latest
[root@VM_0_6_centos images-bak]# ls
centos8.tar
[root@VM_0_6_centos images-bak]# 

11.删除Centos镜像

删除本地镜像前,先停止该镜像在容器的运行
docker rmi -f centos:latest

[root@VM_0_6_centos images-bak]# docker rmi -f centos:latest
Untagged: centos:latest
Untagged: centos@sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
[root@VM_0_6_centos images-bak]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@VM_0_6_centos images-bak]# 

12.加载本地 centos8.tar 镜像

docker load --input centos8.tar

[root@VM_0_6_centos images-bak]# ls
centos8.tar
[root@VM_0_6_centos images-bak]#  docker load --input centos8.tar
Loaded image: centos:latest
[root@VM_0_6_centos images-bak]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        10 days ago         215MB

以上是关于Docker-CE 安装 CentOS的主要内容,如果未能解决你的问题,请参考以下文章

centos7安装docker-ce

安装docker-ce与卸载(centos 7)

centos7.6在线yum安装docker-ce

Docker-CE 安装 CentOS

Docker-CE 安装 CentOS

centos 8 docker-ce 安装