1、常用指令
FROM:构建的新镜像是基于哪个镜像,例如:FROM centos:6
MAINTAINER:镜像维护者姓名或邮箱地址,例如:MAINTAINER duanshuaixing
RUN:构建镜像时运行的Shell命令,例如: RUN yum install httpd
CMD:运行容器时执行的Shell命令,例如:CMD [“-c”, “/start.sh”]、CMD ["/usr/sbin/sshd", "-D"]、CMD /usr/sbin/sshd –D
EXPOSE:声明容器运行的服务端口,例如:EXPOSE 80 443
ENV:设置容器内环境变量,例如:ENV MYSQL_ROOT_PASSWORD 123456
ADD:拷贝文件或目录到镜像,如果是URL或压缩包会自动下载或自动解压
例如:ADD
ADD [“
ADD https://xxx.com/html.tar.gz /var/www/html
ADD html.tar.gz /var/www/html
COPY:拷贝文件或目录到镜像,用法同上,例如:COPY ./start.sh /start.sh
ENTRYPOINT:运行容器时执行的Shell命令,
例如: ENTRYPOINT [“/bin/bash", “-c", “/start.sh"]
ENTRYPOINT /bin/bash -c ‘/start.sh’
VOLUME:指定容器挂载点到宿主机自动生成的目录或其他容器,例如:VOLUME ["/var/lib/mysql"]
USER:为RUN、CMD和ENTRYPOINT执行命令指定运行用户,USER例如:USER duanshuaixing
WORKDIR:为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录,例如:WORKDIR /data
HEALTHCHECK:健康检查
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost/ || exit 1
ARG:在构建镜像时指定一些参数
FROM centos:6
ARG user # ARG user=root USER $user
docker build --build-arg user=duanshuaixing Dockerfile .
2、RUN、CMD和ENTRYPOINT指令区别:
1> RUN在building时运行,可以写多条
2> CMD和ENTRYPOINT在运行container时运行,只能写一条,如果写多条,最后一条生效。
3> CMD在run时可以被COMMAND覆盖,ENTRYPOINT不会被COMMAND覆盖,但可以指定—entrypoint覆盖。
3、Build镜像命令
Usage:docker build [OPTIONS] PATH | URL | -
Options:
-t, --tag list # 镜像名称
-f, --file string # 指定Dockerfile文件位置
例如:
docekr build . # 默认找当前目录以Dockerfile为命名的文件
docker build -t shykes/myapp .
docekr build -t shykes/myapp -f /path/Dockerfile /path
docker build -t shykes/myapp - < Dockerfile
docker build -t shykes/myapp - < context.tar.gz
docker build -t shykes/myapp http://www.example.com/Dockerfile
docker build -f shykes/myapp http://www.example.com/contex.tar.gz
4、构建centos6支持ssh功能镜像
FROM centos:6
MAINTAINER duanshuaixing
ENV ROOT_PASSWORD mima@123
RUN yum install -y openssh-server
RUN echo $ROOT_PASSWORD |passwd --stdin root
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
5、构建centos7支持php环境
FROM ansible/centos7-ansible
MAINTAINER duanshuaixing
RUN yum install -y httpd php php-gd php-mysql mysql mysql-server
ENV MYSQL_ROOT_PASSWORD mima@123
RUN echo "" > /var/www/html/index.php
ADD start.sh /start.sh
RUN chmod +x /start.sh
VOLUME ["/var/lib/mysql"]
EXPOSE 80 3306
CMD /start.sh
cat start.sh
systemctl start httpd
systemctl start mysqld
mysqladmin -uroot password $MYSQL_ROOT_PASSWORD
tail -f
6、构建java镜像
FROM centos:6
MAINTAINER duanshuaixing
ADD jdk-8u45-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_45
ADD http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.0.45/bin/apache-
tomcat-8.0.45.tar.gz /usr/local
WORKDIR /usr/local/apache-tomcat-8.0.45
EXPOSE 8080
ENTRYPOINT ["bin/catalina.sh", "run"]