知识点:
1)手动构建镜像
2)Dockerfile快速构建镜像
阿里云yum源
https://opsx.alibaba.com/mirror
镜像制作
nginx镜像实例
创建并运行centos容器
[root@izm5edykqg8tzid352ga0iz mysql]# docker pull centos //以centos为基础
[root@izm5edykqg8tzid352ga0iz mysql]# docker run -it centos /bin/bash
[root@696cd0ad637b /]# //已经进入容器
更新容器yum源并安装nginx
[root@8ab2b9d7afab yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@8ab2b9d7afab yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@8ab2b9d7afab yum.repos.d]# yum -y install nginx
退出该容器,然后基于该容器做一个镜像
[root@izm5edykqg8tzid352ga0iz mysql]# docker commit -m "my nginx" e1edeef7375f(容器名or容器ID) danny/nginx:v1(镜像名:版本号)
sha256:a5d464ef190c80411161ec6193b877eb5592a1961aeb318e341a9f9c46a39d05
[root@izm5edykqg8tzid352ga0iz mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
danny/nginx v1 a5d464ef190c minutes ago MB //基于mynginx容器制作的镜像
docker.io/nginx latest 71c43202b8ac days ago MB
docker.io/centos latest 5182e96772bf weeks ago MB
docker.io/alpine latest 11cd0b38bc3c weeks ago 4.41 MB [root@izm5edykqg8tzid352ga0iz mysql]# docker run -dit --name nginxv1 danny/nginx:v1
[root@92c8d3a4bc2a /]#
[root@92c8d3a4bc2a /]# ps -ef //已经进入容器
UID PID PPID C STIME TTY TIME CMD
root : ? :: /bin/bash
root : ? :: ps -ef
[root@92c8d3a4bc2a /]# vim /etc/nginx/nginx.conf //修改配置文件添加 daemon off; //不在后台运行
[root@92c8d3a4bc2a /]# nginx -t //验证配置文件语法是否有误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@92c8d3a4bc2a /]# exit
exit [root@izm5edykqg8tzid352ga0iz mysql]# docker commit -m "my nginx" 92c8d3a4bc2a danny/nginx:v2 //再次提交修改版本v2
sha256:062c8ef6e694b94fec7213bc8c65a055d75dc968fe2b671d97826381e0259f48 [root@izm5edykqg8tzid352ga0iz mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
danny/nginx v2 062c8ef6e694 seconds ago MB //修改后提交的版本
danny/nginx v1 a5d464ef190c minutes ago MB
docker.io/nginx latest 71c43202b8ac days ago MB
docker.io/centos latest 5182e96772bf weeks ago MB
docker.io/alpine latest 11cd0b38bc3c weeks ago 4.41 MB [root@izm5edykqg8tzid352ga0iz mysql]# docker run -it -p : danny/nginx:v2 /usr/sbin/nginx //启动v2版本的容器
08df10e92f9df846a840d61d10d07c045397e40ff06d9b4afdc09e4840266d23 [root@izm5edykqg8tzid352ga0iz mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08df10e92f9d danny/nginx:v2 "/usr/sbin/nginx" seconds ago Up seconds 0.0.0.0:->/tcp optimistic_bardeen
然后就可以通过82端口访问了 http://IP:82
Dockerfile构建(快速构建镜像)
常用指令
FROM 指定基础镜像
MAINTAINER 维护者信息
RUN 加在命令前执行命令
ADD copy文件,会自动解压
WORKDIR 相当于cd进目录
VOLUME 目录挂载
EXPOSE 端口
vim /opt/dockerfile/nginx/dockerfile
#This docker file
#VERSION
#Author:danny
#Base image
FROM centos #
MAINTAINER danny danny@qq.com #Commands
RUN rpm -ivh https://mirrors.aliyun.com/centos/7.5.1804/extras/x86_64/Packages/epel-release-7-9.noarch.rpm RUN yum -y install nginx ADD index.html /usr/share/nginx/html/index.html #ADD会自动解压 RUN echo "daemon off;" >> /etc/nginx/nginx.conf EXPOSE CMD ["nginx"] [root@izm5edykqg8tzid352ga0iz nginx]# docker build -t mynginx:v3 /opt/dockerfile/nginx/
查看镜像制作结果
[root@izm5edykqg8tzid352ga0iz nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mynginx v3 3cf3c3308849 minutes ago MB
danny/nginx v2 062c8ef6e694 hours ago MB
danny/nginx v1 a5d464ef190c hours ago MB
docker.io/nginx latest 71c43202b8ac days ago MB
docker.io/centos latest 5182e96772bf weeks ago MB
docker.io/alpine latest 11cd0b38bc3c weeks ago 4.41 MB 制作并启动容器
[root@izm5edykqg8tzid352ga0iz nginx]# docker run -dit -p : --name mynginx_1 mynginx:v3 /usr/sbin/nginx
c373c95329937126d3e879e78c24843f026547975185ce1b6ea655f8f8d5420d
[root@izm5edykqg8tzid352ga0iz nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c373c9532993 mynginx:v3 "nginx" seconds ago Up seconds 0.0.0.0:->/tcp mynginx_1 [root@izm5edykqg8tzid352ga0iz nginx]# curl -I http://localhost:82
HTTP/1.1 OK
...