关于ubuntu 容器连接宿主机

发布于:2025-07-03 ⋅ 阅读:(31) ⋅ 点赞:(0)

一、ubuntu部署docker

1. 卸载旧版 Docker

sudo apt-get remove docker docker-engine docker.io containerd runc

2. 安装依赖工具

apt-get install ca-certificates curl gnupg lsb-release

3. 添加阿里云 Docker 镜像源的 GPG 密钥

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

4. 添加阿里云 Docker 镜像源

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

5. 安装 Docker CE

apt-get install docker-ce docker-ce-cli containerd.io
systemctl start docker
docker pull centos:centos7
sudo mkdir -p /etc/docker
vim /etc/docker/daemon.json  

root@ip-172-31-1-68:~# cat /etc/docker/daemon.json 
{
  "registry-mirrors" : ["https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://dockercf.jsdelivr.fyi",
"https://docker.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi",
"https://mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://mirror.iscas.ac.cn",
"https://docker.rainbond.cc",
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://dockerproxy.com",
"https://gst6rzl9.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"http://mirrors.ustc.edu.cn/",
"https://mirrors.tuna.tsinghua.edu.cn/",
"http://mirrors.sohu.com/" 
],
 "insecure-registries" : [
    "registry.docker-cn.com",
    "docker.mirrors.ustc.edu.cn"
    ],
"debug": true,
"experimental": false
}


sudo systemctl daemon-reload
 sudo systemctl restart docker
docker pull centos:centos7

二、配置centos容器

docker run -itd --name centos -p 60001:22 --privileged centos:centos7 /usr/sbin/init
docker exec -it centos /bin/bash

没有ssh板块记得安装

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

三、配置宿主机账户密码登录


vim /etc/ssh/sshd_config

PermitRootLogin和PasswordAuthentication 修改为yes

进入vi /etc/ssh/sshd_config.d/60-cloudimg-settings.conf目录,目录下会有一个*-settings.conf的文件,编辑这个文件,内容改为

PasswordAuthentication yes
   
sudo passwd root
sudo systemctl restart sshd

四、测试登录

 

进入容器内部ssh 宿主机,或者本地远程工具登录容器都可以,安全组放开自己ip。

docker exec -it centos /bin/bash

ssh root@52.83.224.8 -p 60001


网站公告

今日签到

点亮在社区的每一天
去签到