Dockerfile编写
目录
一、基础语法
https://www.cnblogs.com/linagcheng/p/13440287.html#%E5%9B%9B%E3%80%81dockerfile%E7%BC%96%E5%86%99
FROM ubuntu:18.04 # 指定基础镜像,必须是第一条指令
# RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list # 设置apt源,可选
RUN mkdir -p /root/vul_web # 创建项目的目录
ADD . /root/vul_web # 将当前目录下项目文件复制到镜像的目录下
WORKDIR /root/vul_web # 设置项目工作目录
RUN apt-get update &&
apt-get install -y vim python3 python3-pip language-pack-zh* mysql-client # 安装项目环境
RUN python3 -m pip install -r /root/vul_web/requirements.txt -i https://pypi.douban.com/simple/ # 安装项目依赖包
二、构建项目镜像缓慢
docker build -t image_name:tag .
构建项目镜像时缓慢
1、dockerfile中直接设置:
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
2、修改镜像中的apt源
-
本地创建一个
sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
-
将
sources.list
拷贝到镜像中# 写在dockerfile这种 RUN rm -rf /etc/apt/sources.list ADD sources.list /etc/apt/ # 注意本地sources.list的路径
三、dockerfile设置时区(Ubuntu 18.04基础镜像)
设置镜像时区需要安装tzdata
1、基本方法
这样设置采用交互式,必须人工值守进行安装,而且会出现下图的情况,一直处于卡着的状态
FROM ubuntu:18.04
ENV TZ=Asia/Shanghai # 设置为环境变量,后期可修改
RUN apt-get install -y tzdata && # 安装tzdata
ln -fs /usr/share/zoneinfo/$TZ /etc/localtime
2、解决办法
1)设置tzdata的前端类型(通过环境变量)
ENV DEBIAN_FRONTEND=noninteractive
2)安装tzdata软件包
此时,采用默认时区Etc/UTC
RUN apt-get install -y tzdata
3)建立到期望的时区的链接
设置时区为Asia/Shanghai
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4)重新配置tzdata软件包,使得时区设置生效
RUN dpkg-reconfigure -f noninteractive tzdata
3、设置时间案例
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
# RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list # 设置apt源,可选
RUN apt-get install -y tzdata &&
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
dpkg-reconfigure -f noninteractive tzdata
-
构建镜像时的结果: