Docker-Learn(三)创建镜像Docker(换源)

根据之前的内容基础,本小点的内容主要涉及到的内容是比较重要的文本Dockerfile


1. 编辑Dockerfile

启动命令行终端(在自己的工作空间当中),创建和编辑Dockerfile。

bash 复制代码
vim Dockerfile

然后写入以下内容

bash 复制代码
# 使用一个基础镜像  
FROM ubuntu:latest

# 换源
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bk
COPY sources.list /etc/apt/sources.list

#更新源
RUN apt-get update

# 安装所需的软件包(这里以安装curl和vim为例)  
RUN apt-get update && apt-get install -y curl vim

# 容器启动时执行的命令  
CMD ["/bin/bash"]
  • 首先同样的是使用了ubuntu作为起点获取最新的镜像
bash 复制代码
FROM ubuntu:latest
  • 将原始的sources.list文件备份,以避免后续出现问题
bash 复制代码
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bk
  • 将本地的sources.list文件复制到容器中的/etc/apt/目录,用于替换默认的软件源列表
bash 复制代码
COPY sources.list /etc/apt/sources.list  
  • 更新软件源列表,确保使用新的软件源
bash 复制代码
RUN apt-get update  
  • 更新软件源列表并安装所需的软件包
bash 复制代码
RUN apt-get update && apt-get install -y curl vim  
  • 指定容器启动时执行的命令
bash 复制代码
CMD ["/bin/bash"]

2.编辑sources.list文件

在这里使用阿里云源作为文件替换,也就是对应Dockerfile文件当中的sources.list文件,在当前工作目录下新建就好了。

bash 复制代码
vim sources.list

然后粘贴以下内容

bash 复制代码
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

:wq保存退出

此时我的工作空间的目录如下图所示:

3.创建镜像并且测试

  • 创建镜像,在命令行终端里面输入指令:
bash 复制代码
docker build -t test:docker_v1 .

等待镜像生成完成

  • 检查镜像是否生成成功
bash 复制代码
docker images

如果生成好话如下图所示

  • 进入到镜像进行测试
bash 复制代码
docker run -it test:docker_v1
apt-get update
curl -v www.baidu.com
vim /etc/apt/sources.list

没问题的话如下几张图所示:

镜像已经更换成阿里云

可以使用 curl 和vim等工具


可以查看系统信息


🌸🌸🌸完结撒花🌸🌸🌸


🌈🌈Redamancy🌈🌈


相关推荐
杨浦老苏1 小时前
家庭实验室监控仪表盘HomeLab-Monitor
运维·docker·监控·群晖
回忆2012初秋2 小时前
【Nginx】原理、配置与运维实战(2)
运维·nginx·策略模式
Urbano3 小时前
工装外套全制作流程、工序痛点及自动化设备升级方案
运维·自动化
映翰通朱工3 小时前
工业4G网关无公网IP远程运维实战(内网终端异地访问方案)
运维·服务器·网络·安全·智能路由器
洪晓露3 小时前
将 rke2 集群证书延长至 10 年
运维·服务器·数据库
谢平康4 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
IP老炮不瞎唠4 小时前
Python 价格监控如何实现?思路与实用方法分享
运维·服务器·网络
GIS数据转换器4 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
Tokai_Teio_15 小时前
第四届黄河流域 misc
运维·服务器
开发者联盟league5 小时前
使用k8s安装Sonarqube
云原生·容器·kubernetes