一. 常用的dockerfile构建命令
FROM #指定基础镜像
MAINTAINER #镜像的作者,一般是 姓名+邮箱
RUN #运行镜像
ADD #添加依赖内容
COPY #将文件内容拷贝到镜像中
WORKDIR #设置进入容器后的工作目录
VOLUME #挂载目录,和上面的工作目录不一样
EXPOSE #暴露端口,不然run的时候你得-p指定暴露,这里写了就不需要-p
CMD #指定容器运行时需要运行的命令,会被替换
ENTRYPOINT #指定容器运行时需要运行的命令,会被追加
ONBUILD #继承DockerFile的时候触发指令
ENV #构建的时候设置环境变量
二. 手动构建nginx容器镜像过程
从centos7.6启动镜像为容器
docker run -d --name eginxfile01 --privileged=true -v database -v config -p 8000:80 centos:7.6.1810 /usr/sbin/init
以交互模式进入容器
docker exec -it eginxfile01 /bin/bash
备份原来的yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
下载 wget
yum -y install wget
设置aliyun的yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
添加EPEL源
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
清理yum缓存
yum clean all
yum makecache
yum -y update
centos7基础配置安装包
yum install -y bash-completion bash-completion-extras vim net-tools initscripts
下载nginx
yum install -y nginx
开启服务
systemctl start nginx
systemctl enable nginx
配置首页文件
rm -rf /usr/share/nginx/html/index.html
echo "nginx" > /usr/share/nginx/html/index.html
测试
curl localhost:8000
打包成nginx镜像
docker commit -a 'jiekeliu' -m 'centos7:nginx' eginxfile01 jiekeliu/eginxfile01:1.0
三. dockerile 构建相同的镜像
#从centos:7.6.1810开始创建镜像
FROM centos:7.6.1810
#这个镜像的作者
MAINTAINER jiekeliu<12345@qq.com>
#设置环境变量,相当于开发语言的设置变量
ENV MYPATH /usr/local
#设置工作目录,这个相当重要,和命令的执行权限有关
WORKDIR $MYPATH
#添加数据卷,相当于linux的共享文件夹
VOLUME ["database","config"]
#暴露端口,相当于linux的防火墙开放端口
EXPOSE 80
EXPOSE 3306
#运行linux命令
RUN cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak \
&& yum -y install wget \
&& wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo \
&& wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo \
&& yum clean all \
&& yum makecache \
&& yum -y update \
&& yum install -y bash-completion bash-completion-extras vim net-tools initscripts \
&& yum install -y nginx \
&& rm -rf /usr/share/nginx/html/index.html \
&& echo "nginx" > /usr/share/nginx/html/index.html
CMD ["/usr/sbin/init"]
314

被折叠的 条评论
为什么被折叠?



