docker制作自定义镜像
前提准备:
centos镜像
两种方法:
1.基于centos镜像使用commit创建新镜像
2.基于centos镜像使用Dockerfile构建新镜像
方法一:
1 [root@docker1 ~]# docker run -it centos 2 [root@2ec16f90b708 /]# rm -rf /etc/yum.repos.d/* 3 [root@2ec16f90b708 /]# vi /etc/yum.repos.d/aliyun.repo
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64
enabled=1
gpgcheck=0
5 [root@2ec16f90b708 /]# yum clean all 6 [root@2ec16f90b708 /]# yum repolist 7 [root@2ec16f90b708 /]# yum -y install net-tools iproute psmisc vim-enhanced 8 [root@2ec16f90b708 /]# ifconfig 9 [root@2ec16f90b708 /]# exit 10 [root@docker1 ~]# docker ps -a 11 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 12 2ec16f90b708 centos "/bin/bash" 10 minutes ago 13 [root@docker1 ~]# docker start 2ec16f90b708 14 [root@docker1 ~]# docker commit 2ec myos:v1 15 [root@docker1 ~]# docker images 16 REPOSITORY TAG IMAGE ID CREATED SIZE 17 myos v1 9d14368c8ca8 3 seconds ago 18 centos latest 9f38484d220f 8 weeks ago 202MB 19 [root@docker1 ~]# docker run -it myos:v1 20 [root@ad5ca31ee5a7 /]# ifconfig
方法二:
1.创建Dockerfile文件
[root@docker1 ~]# mkdir oo [root@docker1 ~]# cd oo [root@docker1 oo]# vim Dockerfile FROM myos:v1 RUN yum -y install httpd ENV EnvironmentFile=/etc/sysconfig/httpd WORKDIR /var/www/html/ RUN echo "test" > /var/www/html/index.html EXPOSE 80 CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
2.根据Dockerfile构建镜像
[root@docker1 oo]# docker build -t myos:http . #当前目录下寻找Dockerfile文件
3.运行,测试
[root@docker1 oo]# docker run -d myos:http f4e8ff7f74a762a4e21e8db5fd77b232cbffec1d38c4f7571e2a89f6f131c869 [root@docker1 oo]# docker inspect f4e ... "IPAddress": "172.17.0.2", ... [root@docker1 oo]# curl 172.17.0.2 test