使用docker运行CentOS容器
  tmj7hqW6m224 10天前 16 0

一、从远程仓库查找需要的镜像

1.是通过Docker Hub网站搜索(https://hub.docker.com/

 2.是通过docker search命令来查找镜像

PS D:\learning\CentOS7> docker search centos
NAME                               DESCRIPTION                                      STARS     OFFICIAL
centos                             DEPRECATED; The official build of CentOS.        7721      [OK]
kasmweb/centos-7-desktop           CentOS 7 desktop for Kasm Workspaces             43
bitnami/centos-base-buildpack      Centos base compilation image                    0
dokken/centos-7                    CentOS 7 image for kitchen-dokken                10
spack/centos7                      CentOS 7 with Spack preinstalled                 2
dokken/centos-stream-8             CentOS Stream 8 image for use with Test Kitc…   5
dokken/centos-8                    CentOS 8 image for use with Test Kitchen's k…   6
dokken/centos-6                    EOL: CentOS 6 image for kitchen-dokken           0
dokken/centos-stream-9             CentOS Stream 9 image for use with Test Kitc…   10
atlas/centos7-atlasos              ATLAS CentOS 7 Software Development OS           3
ustclug/centos                     Official CentOS Image with USTC Mirror           0
spack/centos6                      CentOS 6 with Spack preinstalled                 1
eclipse/centos_jdk8                CentOS, JDK8, Maven 3, git, curl, nmap, mc, …   5
corpusops/centos-bare              https://github.com/corpusops/docker-images/      0
corpusops/centos                   centos corpusops baseimage                       0
eclipse/centos_go                  Centos + Go                                      0
spack/centos-stream                                                                 2
fnndsc/centos-python3              Source for a slim Centos-based Python3 image…   0
eclipse/centos_spring_boot         Spring boot ready image based on CentOS          0
openmicroscopy/centos-systemd-ip   centos/systemd with iproute, for testing mul…   0
eclipse/centos                     CentOS based minimal stack with only git and…   1
eclipse/centos_vertx               CentOS + vertx                                   0
eclipse/centos_wildfly_swarm       CentOS, WildFly, Swarm                           0
eclipse/centos_nodejs              CentOS based nodejs4 stack                       0
dockette/centos                    My Custom CentOS Dockerfiles                     1

 

二、拉取镜像并运行容器

1.拉取指定的版本,这里我用的是官方镜像的centos7指定版本,除了指定发布源,还可以通过tag指定历史版本,不指定默认是latest

docker pull centos:centos7.9.2009

2.等待拉取完之后,通过docker images命令可以查看到已经存在相应的镜像了

PS D:\learning\CentOS7> docker images
REPOSITORY   TAG              IMAGE ID       CREATED       SIZE
centos       centos7.9.2009   eeb6ee3f44bd   2 years ago   204MB

3.启动一个docker容器并运行image

docker run -itd --name centos-container -p 1022:22 centos:centos7.9.2009

-itd实际是-i:以交互模式运行容器。通常与-t 同时使用。这允许与容器进行交互,例如在容器内执行命令或查看输出。-t为容器重新分配一个伪输入终端。通常与-i同时使用。-d后台运行容器,并返回容器 ID。使用此参数时,容器会在后台运行,不会阻塞终端。但要注意,如果容器内的主命令在后台运行,容器会立即停止。解决方法是通过 -i 或 -t 为 -d 提供一个伪终端,或者将 tail -f /dev/null 添加到命令中,以保持容器运行。-p 1022:22指定将宿主机的1022端口映射到容器的22端口,22端口是远程ssh连接使用的端口。

4.进入容器内部

docker exec -it centos-container /bin/bash

 运行后发现,我们似乎以root权限登录进了Linux终端,但注意,这里的root用户不是真正具有root权限,这应该是处于容器安全考虑,防止容器中以root权限执行的某些操作影响宿主机,实际操作中也应该避免使用root权限。

比如用下面命令查看防火墙服务状态,会发现没有权限(Desktop-Bus没有启动)

[root@00f003f51d2f /]# systemctl status firewalld
Failed to get D-Bus connection: Operation not permitted

 

三、设置ssh远程连接

1.为了初期设置centos的ssh连接,执行下列命令退出容器并关闭,然后追加--privileged参数使我们进入容器后有root权限。

# 退出容器内部
exit
# 停止容器
docker stop centos-container
# 删除容器
docker rm centos-container
# 重新以特权模式启动一个容器运行镜像--privileged会赋予容器完全的特权,并设置entrypoint为/usr/sbin/init
docker run -itd --name centos-container -p 1022:22 --privileged=true centos:centos7.9.2009 /usr/sbin/init
# 进入容器内部
docker exec -it centos-container /bin/bash

 

2.安装ssh服务并重启

yum install net-tools.x86_64 -y
yum install -y openssh-server
systemctl restart sshd

 3.设置root用户密码

[root@2a792e360945 /]# passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

 

4.使用ssh客户端登录

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 10天前 0

暂无评论

推荐阅读
tmj7hqW6m224