利用dockerfile封装centos7容器

  • 1.编写 dockerfile 用于封装一个新的镜像
cat > /opt/dockerfile-centos7.yml <<EOF
FROM centos:7
MAINTAINER sqlfans "78667417@qq.com"

ENV TZ Asia/Shanghai
RUN yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

RUN yum install -y -q --nogpgcheck initscripts openssh-clients
RUN yum install -y -q wget curl net-tools

#.安装ansible
# RUN yum install -y -q epel-release
# RUN yum install -y -q ansible

EXPOSE 23245
EOF
  • 2.使用 docker build 构建基础镜像,耗时约 00:01:43,镜像大小约 731 MB
time docker build -f /opt/dockerfile-centos7.yml -t centos7:7 .
docker images | grep centos
  • 3.利用新的镜像启动centos7容器,并将ssh端口映射为23245
docker run -it -d -p 23245:23245 --name centos7 centos7:7 /bin/bash
docker ps -a | grep centos
  • 4.测试一下新的centos容器
docker exec -it centos7 /bin/bash
> ip a
> netstat -lnpt
  • 5.测试ok,将新的容器导出,日后 load -i xxx.tar 就可以导入镜像
docker save -o /opt/centos7.2024.tar centos7:7
  • x.清理痕迹
docker stop $(docker ps -a | grep centos | awk '{print $1}')
docker rm $(docker ps -a | grep centos | awk '{print $1}')
docker rmi -f $(docker images -qf dangling=true)
docker rmi -f $(docker images | grep centos | awk '{print $3}')
Copyright © www.sqlfans.cn 2024 All Right Reserved更新时间: 2024-04-19 09:59:29

results matching ""

    No results matching ""