Docker实战之:创建 CentOS 8 容器

实战环境:系统为:CentOS8,Docker:版本为20.10.11,镜像为:nginx:latest 

[root@CentOS8 ~]# cat /etc/redhat-release
CentOS Linux release 8.5.2111

[root@CentOS8 ~]# docker --version
Docker version 20.10.11, build dea9396

[root@CentOS8 ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED        SIZE
nginx                     latest    f652ca386ed1   6 days ago     141MB
centos                    latest    5d0da3dc9764   2 months ago   231MB
centos/mysql-57-centos7   latest    f83a2938370c   2 years ago    452MB

实战目的:

创建一个CentOS的容器,并且实现SSH远程链接到容器内

搜索 centos 镜像

[root@CentOS8 ~]# docker search centos
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   6914      [OK]       
ansible/centos7-ansible           Ansible on Centos7                              135                  [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC session…   131                  [OK]
jdeathe/centos-ssh                OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   121                  [OK]
centos/systemd                    systemd enabled base container.                 105                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   92                   
imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              58                   [OK]
tutum/centos                      Simple CentOS docker image with SSH access      48                   
centos/postgresql-96-centos7      PostgreSQL is an advanced Object-Relational …   45                   
centos/httpd-24-centos7           Platform for running Apache httpd 2.4 or bui…   40                   
kinogmt/centos-ssh                CentOS with SSH                                 29                   [OK]
guyton/centos6                    From official centos6 container with full up…   10                   [OK]
nathonfowlie/centos-jre           Latest CentOS image with the JRE pre-install…   8                    [OK]
centos/tools                      Docker image that has systems administration…   7                    [OK]
drecom/centos-ruby                centos ruby                                     6                    [OK]
darksheer/centos                  Base Centos Image -- Updated hourly             3                    [OK]
mamohr/centos-java                Oracle Java 8 Docker image based on Centos 7    3                    [OK]
dokken/centos-7                   CentOS 7 image for kitchen-dokken               2                    
miko2u/centos6                    CentOS6 日本語環境                                   2                    [OK]
amd64/centos                      The official build of CentOS.                   2                    
mcnaughton/centos-base            centos base image                               1                    [OK]
blacklabelops/centos              CentOS Base Image! Built and Updates Daily!     1                    [OK]
starlabio/centos-native-build     Our CentOS image for native builds              0                    [OK]
smartentry/centos                 centos with smartentry                          0                    [OK]
jelastic/centosvps                An image of the CentOS Elastic VPS maintaine…   0                    

拉取CentOS镜像到本机

[root@CentOS8 ~]# docker pull nginx

查看本地有哪些镜像

[root@CentOS8 ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED        SIZE
nginx                     latest    f652ca386ed1   6 days ago     141MB
centos                    latest    5d0da3dc9764   2 months ago   231MB
centos/mysql-57-centos7   latest    f83a2938370c   2 years ago    452MB

容器操作

使用镜像 centos以后台模式启动一个容器
并将容器名称自定义为test1:--name test1
将容器的22端口映射到主机的10022端口:-p 10022:22
docker run -tid --name test1 -p 10022:22 centos:latest

docker run -tid --name test --privileged=true centos /sbin/init
或者
docker run -tid --name test --privileged=true centos /usr/sbin/init

[root@CentOS8 ~]# docker run -tid --name test --privileged=true centos /sbin/init
d12227f800d26ebe137dc45d9c538525a84aa93517738ea6a7d9f275709385e7


参数1:--name test
给这个容器自定义名称为test

参数2:--privileged=true
使用该参数,container内的root拥有真正的root权限。否则只是外部的一个普通用户权限。privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。允许你在docker容器中启动docker容器。

参数3:/usr/sbin/init 或 /sbin/init
启动容器之后可以使用systemctl

进入名称为 test 的容器:docker exec -it 容器名称 /bin/bash

[root@CentOS8 ~]# docker exec -it test /bin/bash
[root@d12227f800d2 /]# 

容器centos系统安装常用软件包

1、安装ssh,以后需要远程登录链接:yum install -y openssl openssh-server

[root@d12227f800d2 /]# yum install -y openssl openssh-server
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:04:39 ago on Wed Dec  8 16:20:56 2021.
Dependencies resolved.

省略...........

Installed:
  openssh-8.0p1-10.el8.x86_64   openssh-server-8.0p1-10.el8.x86_64   openssl-1:1.1.1k-4.el8.x86_64   openssl-pkcs11-0.4.10-2.el8.x86_64  

Complete!

# 配置SSH
vi /etc/ssh/sshd_config
Port 22                              # 去掉前面的#号
ListenAddress 0.0.0.0       # 去掉前面的#号

# 保存退出
wq!

# 重启SSH,并且设置开机启动
systemctl restart sshd
systemctl enable sshd

2、安装 passwod,并且创建该容器的系统密码:yum -y install passwd

# 安装 passwd
[root@d12227f800d2 /]# yum -y install passwd
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 1:54:50 ago on Wed Dec  8 15:39:20 2021.
Dependencies resolved.

省略...........

Installed:
  libuser-0.62-23.el8.x86_64                                            passwd-0.80-3.el8.x86_64                                           

Complete!

安装完毕后使用命令:passwd   创建系统密码

3、安装 net-tools【可选安装】:yum -y install net-tools

[root@d12227f800d2 /]# yum -y install net-tools
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:03:12 ago on Wed Dec  8 16:20:56 2021.
Dependencies resolved.

省略...........

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                                                                                

Complete!

4、查看22端口占用情况

[root@d12227f800d2 /]# netstat -tunlp | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      299/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      299/sshd 

5、设置中国时区:timedatectl set-timezone Asia/Shanghai

[root@d12227f800d2 ~]# timedatectl set-timezone Asia/Shanghai


[root@d12227f800d2 ~]# date        # 查看当前时间
Sun Dec 12 22:11:05 CST 2021

6、设置语言环境
dnf install langpacks-en glibc-all-langpacks -y        # 安装所有的字符集
localectl set-locale LANG=en_US.UTF-8                # 使用UTF-8编码的美国英语(US)

重新启动:reboot

[root@d12227f800d2 /]# reboot

查看当前所有容器

[root@CentOS8 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND            CREATED             STATUS                        PORTS                                     NAMES
680d92b04a74   centos    "/usr/sbin/init"   About an hour ago   Exited (137) 32 minutes ago                                             test3
f517637c9606   centos    "/sbin/init"       2 hours ago         Up 11 minutes                 0.0.0.0:10022->22/tcp, :::10022->22/tcp   test
7a5090b1fee1   centos    "/sbin/init"       About an hour ago   Exited (137) 22 minutes ago                                             test1

如果要停止容器使用:

停止容器:docker stop 容器ID
启动容器:docker start 容器ID
重启容器:docker restart 容器ID

远程链接到容器

使用ssh第三方软件远程链接容器

IP:192.168.186.123
端口:10022
系统账号:root
密码:123456

 可以看到容器的ID

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值