docker镜像制作的命令,docker自定义镜像
一.Dockerfile制作镜像的命令
1.Dockerfile文件的编写格式
FROM //指定基础镜像(唯一)
FROM 镜像名:标签
USER //启动容器使用的用户,切换用户执行命令(唯一)
相当与sudo -u nobody,切换用户执行命令
RUN //在容器内执行命令,可以写多条
RUN touch /tmp/a.txt;touch /tmp/a.txt
WORKDIR //设置启动容器的默认工作目录(唯一),替代cd命令
WORKDIR /tmp/
RUM touch a.txt
ADD //把文件拷贝到容器内,如果文件是tar.xx格式,会自动解压
ADD myfile.tar /var/tmp
COPY //把文件拷贝到容器内,不会自动解压
COPY myfile.tar /tmp
ENV //设置启动容器的环境变量
TZ=Asia/Shanghai
EXPOSE //使用镜像创建的容器默认监听使用的端口号/协议
EXPOSE 1234/tcp //标识作用
ENTRYPOINT //容器默认的启动命令(唯一)
ENTRYPOINT="echo"
CMD="/bin/ls -l"
执行顺序:${ENTRYPOINT} ${CMD}
容器启动输出:/bin/ls -l
CMD //容器默认的启动参数,启动的时候执行什么参数(唯一)
CMD: ["/bin/ls","-l"] //cmd可被位置参数覆盖
创建镜像命令使用实例
FROM mylinux:latest
ADD myfile.tar /var/tmp
COPY myfile.tar /tmp
RUN touch /tmp/a.txt;useradd user001
USER user001
RUN touch /tmp/b.txt
WORKDIR /tmp/
RUN touch c.txt
ENV TZ=Asia/Shanghai
EXPOSE 1234/tcp
CMD ["/bin/bash"]
2.镜像构建命令
docker build -t 镜像名:标签 Dockerfile所在目录/
二.制作镜像
1.apache 镜像
FROM mylinux:latest
RUN dnf install -y httpd php && dnf clean all
COPY 00-mpm.conf /etc/httpd/conf.modules.d/00-mpm.conf
ADD myweb.tar.gz /var/www/html/
ENV LANG=C
WORKDIR /var/www/html/
EXPOSE 80/tcp
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
2.nginx 镜像
FROM mylinux:latest
ADD nginx-1.22.1.tar.gz /
WORKDIR /nginx-1.22.1
RUN dnf install -y openssl pcre
RUN dnf install -y openssl-devel pcre-devel gcc make
RUN dnf clean all
RUN ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module
RUN make
RUN make install
RUN echo 'Nginx is running !' >/usr/local/nginx/html/index.html
ENV PATH=${PATH}:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80/tcp
CMD ["nginx", "-g", "daemon off;"]
3.多阶段镜像
# 第一阶段编译程序
FROM mylinux:latest as builder
ADD nginx-1.22.1.tar.gz /
WORKDIR /nginx-1.22.1
RUN dnf install -y openssl-devel pcre-devel gcc make
RUN ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module
RUN make
RUN make install
RUN echo 'Nginx is running !' >/usr/local/nginx/html/index.html
# 第二阶段最终镜像
FROM mylinux:latest
RUN dnf install -y pcre openssl && dnf clean all
COPY --from=builder /usr/local/nginx /usr/local/nginx
ENV PATH=${PATH}:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80/tcp
CMD ["nginx", "-g", "daemon off;"]
一.Dockerfile制作镜像的命令
1.Dockerfile文件的编写格式
FROM //指定基础镜像(唯一)
FROM 镜像名:标签
USER //启动容器使用的用户,切换用户执行命令(唯一)
相当与sudo -u nobody,切换用户执行命令
RUN //在容器内执行命令,可以写多条
RUN touch /tmp/a.txt;touch /tmp/a.txt
WORKDIR //设置启动容器的默认工作目录(唯一),替代cd命令
WORKDIR /tmp/
RUM touch a.txt
ADD //把文件拷贝到容器内,如果文件是tar.xx格式,会自动解压
ADD myfile.tar /var/tmp
COPY //把文件拷贝到容器内,不会自动解压
COPY myfile.tar /tmp
ENV //设置启动容器的环境变量
TZ=Asia/Shanghai
EXPOSE //使用镜像创建的容器默认监听使用的端口号/协议
EXPOSE 1234/tcp //标识作用
ENTRYPOINT //容器默认的启动命令(唯一)
ENTRYPOINT="echo"
CMD="/bin/ls -l"
执行顺序:${ENTRYPOINT} ${CMD}
容器启动输出:/bin/ls -l
CMD //容器默认的启动参数,启动的时候执行什么参数(唯一)
CMD: ["/bin/ls","-l"] //cmd可被位置参数覆盖
创建镜像命令使用实例
FROM mylinux:latest
ADD myfile.tar /var/tmp
COPY myfile.tar /tmp
RUN touch /tmp/a.txt;useradd user001
USER user001
RUN touch /tmp/b.txt
WORKDIR /tmp/
RUN touch c.txt
ENV TZ=Asia/Shanghai
EXPOSE 1234/tcp
CMD ["/bin/bash"]
2.镜像构建命令
docker build -t 镜像名:标签 Dockerfile所在目录/
二.制作镜像
1.apache 镜像
FROM mylinux:latest
RUN dnf install -y httpd php && dnf clean all
COPY 00-mpm.conf /etc/httpd/conf.modules.d/00-mpm.conf
ADD myweb.tar.gz /var/www/html/
ENV LANG=C
WORKDIR /var/www/html/
EXPOSE 80/tcp
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
2.nginx 镜像
FROM mylinux:latest
ADD nginx-1.22.1.tar.gz /
WORKDIR /nginx-1.22.1
RUN dnf install -y openssl pcre
RUN dnf install -y openssl-devel pcre-devel gcc make
RUN dnf clean all
RUN ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module
RUN make
RUN make install
RUN echo 'Nginx is running !' >/usr/local/nginx/html/index.html
ENV PATH=${PATH}:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80/tcp
CMD ["nginx", "-g", "daemon off;"]
3.多阶段镜像
# 第一阶段编译程序
FROM mylinux:latest as builder
ADD nginx-1.22.1.tar.gz /
WORKDIR /nginx-1.22.1
RUN dnf install -y openssl-devel pcre-devel gcc make
RUN ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module
RUN make
RUN make install
RUN echo 'Nginx is running !' >/usr/local/nginx/html/index.html
# 第二阶段最终镜像
FROM mylinux:latest
RUN dnf install -y pcre openssl && dnf clean all
COPY --from=builder /usr/local/nginx /usr/local/nginx
ENV PATH=${PATH}:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80/tcp
CMD ["nginx", "-g", "daemon off;"]