51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

dockerfile自动化构建docer镜像 (centos)game案例

常用的命令:

dockerfile -常用命令
	MAINTAINER
		# 声明作者信息,官方已弃用,建议使用LABEL
	LABEL
		# LABEL可以基于key=value方式指定信息
	RUN
		执行命令
	WORKDIR
		# 指定工作目录,若不指定则默认路径为"/"
		就是 进入 容器后 自动切换到的目录
		WORKDIR /usr/local/nginx/html 
	COPY 
		将宿主机的文件拷贝到容器的指定路径
		COPY config/games.conf  /etc/nginx/conf.d/
	ADD
		如果文件为tar包,会自动解压并删除源文件。
		ADD softwares/baimei-games.tar.gz /usr/local/nginx/html/
	EXPOSE
		# 暴露容器的80端口
			EXPOSE 80 22 
	VOLUME
		# 将容器的指定路径进行持久化,会产生随机(匿名)存储卷
		VOLUME /usr/local/nginx/html/
	ENV
		# 向容器传递环境变量,基于key=value的语法格式
		ENV school=baimei \
    class=dashu \
    baimei_ADMIN=123456
	CMD
		# 容器启动时的命令
		# CMD ["nginx","-g","daemon off;"]
		CMD ["bash","-x","/start.sh"]
	至少记住10个

--

案例1

(1)games.conf

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/bird/;
    server_name   game01.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/pinshu/;
    server_name   game02.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/tanke/;
    server_name   game03.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/chengbao/;
    server_name   game04.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/motuo/;
    server_name   game05.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/liferestart/;
    server_name   game06.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/huangjinkuanggong/;
    server_name   game07.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/feijidazhan/;
    server_name   game08.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/zhiwudazhanjiangshi/;
    server_name   game09.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/xiaobawang/;
    server_name   game10.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/pingtai/;
    server_name   game11.baimei.com;
}

server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/dayu/;
    server_name   game12.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/maliao/;
    server_name   game13.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/menghuanmonizhan/;
    server_name   game14.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/qieshuiguo/;
    server_name   game15.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/wangzhezhicheng/;
    server_name   game16.baimei.com;
}
server {
    listen        0.0.0.0:80;
    root          /usr/local/nginx/html/zhiwuVSjiangshi/;
    server_name   game17.baimei.com;
}

(2) start.sh

#!/bin/sh
# class: dashu
# office: www.baimeidashu.com

nginx

if [ -n "$1" ];then
   echo root:$1 | chpasswd
elif [ -n "$BAIMEI_ADMIN" ]; then
   echo root:$BAIMEI_ADMIN | chpasswd
else 
   echo root:"123" | chpasswd
fi

ssh-keygen -A

/usr/sbin/sshd -D

(3)

(4)build.sh

#!/bin/bash

docker build -t baimei-games:v0.$1 -f Dockerfile .

docker container run -d baimei-games:v0.$1

docker ps -l

(5)Dockerfile

# 指定基础镜像
FROM centos:7

# 声明作者信息,官方已弃用,建议使用LABEL
MAINTAINER  dashu@baimei.com 

# LABEL可以基于key=value方式指定信息
LABEL school=baimei \
      class=dashu \
      address=北京市 \
      office=www.baimei.com \
      email=dashu@baimei.com \
      hobby=抽烟,喝酒,烫头


# 修改centos镜像的yum源为国内地址,并安装nginx,sshd服务(运行时,不能阻塞RUN指令,否则终止编译)
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
        -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
        -i.bak \
        /etc/yum.repos.d/CentOS-*.repo && \
    yum -y install epel-release && \
    sed -e 's!^metalink=!#metalink=!g' \
        -e 's!^#baseurl=!baseurl=!g' \
        -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
        -e 's!https\?://download\.example/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
        -i /etc/yum.repos.d/epel*.repo && \
    yum -y install nginx openssh-server initscripts && \
    rm -rf /var/cache/yum


# 指定工作目录,若不指定则默认路径为"/"
WORKDIR /usr/local/nginx/html 

# 将宿主机的文件拷贝到容器的指定路径
COPY config/games.conf  /etc/nginx/conf.d/
COPY scripts/start.sh /

# COPY softwares/baimei-games.tar.gz /usr/local/nginx/html/
## RUN tar xf /usr/local/nginx/html/baimei-games.tar.gz -C /usr/local/nginx/html/
### RUN cd /usr/local/nginx/html && tar xf baimei-games.tar.gz
#### RUN cd /usr/local/nginx/html && tar xf baimei-games.tar.gz && rm -f baimei-games.tar.gz
# RUN tar xf baimei-games.tar.gz && rm -f baimei-games.tar.gz

# 如果文件为tar包,会自动解压并删除源文件。
ADD softwares/baimei-games.tar.gz /usr/local/nginx/html/

# 暴露容器的80端口
EXPOSE 80 22 

# 将容器的指定路径进行持久化,会产生随机(匿名)存储卷
VOLUME /usr/local/nginx/html/

# 向容器传递环境变量,基于key=value的语法格式
ENV school=baimei \
    class=dashu \
    BAIMEI_ADMIN=123456

# 容器启动时的命令
# CMD ["tail","-f","/etc/hosts"]
# CMD ["nginx","-g","daemon off;"]
CMD ["bash","-x","/start.sh"]

运行:

docker run -d -p 81:80 --name web01 nginx:1.20.1  

然后配置etc:

可以启动了

赞(4)
未经允许不得转载:工具盒子 » dockerfile自动化构建docer镜像 (centos)game案例