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🌈🌈


相关推荐
木易双人青2 小时前
01-Docker-简介、安装与使用
运维·docker·容器
专注API从业者3 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
Lovyk4 小时前
Linux 正则表达式
linux·运维
ac.char6 小时前
在CentOS系统中查询已删除但仍占用磁盘空间的文件
linux·运维·centos
罗不俷7 小时前
【RH134知识点问答题】第13章:运行容器
容器·rhel
中科米堆7 小时前
中科米堆CASAIM自动化三维测量设备测量汽车壳体直径尺寸
运维·自动化·汽车·视觉检测
缘华工业智维8 小时前
CNN 在故障诊断中的应用:原理、案例与优势
大数据·运维·cnn
开航母的李大9 小时前
软件系统运维常见问题
运维·服务器·系统架构·运维开发
AI大模型9 小时前
基于 Docker 的 LLaMA-Factory 全流程部署指南
docker·llm·llama
华强笔记11 小时前
Linux内存管理系统性总结
linux·运维·网络