dockerfile常用指令
FROM:构建新的镜像是基于哪个镜像,例如:FROM centos:6
MAINTAINER:镜像维护者姓名或邮箱地址,例如:MAINTAINER goldenyin
RUN:构建镜像时运行的shell命令,例如:
RUN ["yum","install","https"]
RUN yum install -y httpd
CMD:运行容器时执行的shell命令,例如:
CMD ["-c","/start.sh"]
CMD ["/usr/sbin/sshd","-D"]
CMD /usr/sbin/sshd -D
EXPOSE:声明容器运行的服务端口,例如:EXPOSE 80 443
EVN:设置容器内环境变量,例如:ENV MYSQL_ROOT_PASSWORD 123456
ADD:拷贝文件或目录到镜像,如果是URL或压缩包会自动下载或自动解压,例如:
ADD <src>...<dest>
ADD ["<src>",...,"<dest>"]
ADD https://xxx.com/xxx.tar.gz /var/www/html
ADD xxx.tar.gz /var/www/html
COPY:拷贝文件或目录到镜像,用法与ADD类似,例如: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 goldenyin
WORKDIR:为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录,例如:WORKDIR /data
HEALTHCHECK:健康检查,例如:HEALTHCHECK --interval=5m --timeout=3s --retries=3 CMD curl -f http://localhost/ ||exit 1
ARG:在构建镜像时指定一些参数,例如:
FROM centos:6
ARG user #ARG user=root
USER $user
#docker build --build-arg user=goldenyin Dockerfile.
示例
[root@docker test_dackerfile]# pwd
/root/test_dackerfile
[root@docker test_dackerfile]# ls
Dockerfile php-5.6.31.tar.gz
[root@docker test_dackerfile]# cat Dockerfile
FROM centos:7
MAINTAINER www.xxx.com
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel openssl libmcrypt libmcrypt-devel mcrypt mhash php-mcrypt
ADD php-5.6.31.tar.gz /tmp/
RUN cd /tmp/php-5.6.31 &&
./configure --prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-gd --with-png-dir=/usr/local/libpng
--with-jpeg-dir=/usr/local/jpeg
--with-freetype-dir=/usr/local/freetype
--with-xpm-dir=/usr/
--with-zlib-dir=/usr/local/zlib
--with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf
--with-openssl --enable-pcntl --enable-sockets
--with-xmlrpc --enable-zip --enable-soap
--without-pear
--with-gettext --enable-session
--with-curl --enable-ctype &&
make -j4 &&
make install &&
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf &&
sed -i "s/127.0.0.1/0.0.0.0/g" /usr/local/php/etc/php-fpm.conf &&
sed -i "21a daemonize = no" /usr/local/php/etc/php-fpm.conf &&
rm -fr /tmp/php-5.6.31* && yum clean all
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]
build镜像命令
usage:docker image build [OPTIONS] PATH | URL -
options:
-t,--tag list #镜像名称
-f,--file string #指定Dockerfile文件位置
示例:
docker build .
docker build -t shykes/myapp .
docker build -t shykes/myapp -f /path/Dockerfile /path
[root@docker test_dackerfile]# docker image build -t php:v1 .
创建成功返回如下信息:
Removing intermediate container 3ec421e076d7
---> b4994afab959
Step 6/8 : WORKDIR /usr/local/php
---> Running in 283ac6a479aa
Removing intermediate container 283ac6a479aa
---> a65d38cbec2a
Step 7/8 : EXPOSE 9000
---> Running in 70859693269d
Removing intermediate container 70859693269d
---> 77c0dd4f6d02
Step 8/8 : CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]
---> Running in e5671de5e354
Removing intermediate container e5671de5e354
---> c956f56431e4
Successfully built c956f56431e4
Successfully tagged php:v1
构建php网站平台镜像
自定义网络
docker network create lnmp
创建php容器
docker run -itd
--name lnmp_php
--net lnmp
--mount type=bind,src=/app/wwwroot/,dst=/usr/local/nginx/html
php:v1
创建nginx容器
docker run -itd
--name lnmp_nginx
--net lnmp
-p 888:80
--mount type=bind,src=/app/wwwroot/,dst=/usr/local/nginx/html
nginx:v1
创建mysql容器
docker run -itd
--name lnmp_mysql
--net lnmp
-p 3306:3306
--mount src=mysql-vol,dsr=/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456
mysql --character-set-server=utf-8
构建JAVA网站镜像
构建java环境镜像
FROM centos:77
MAINTAINER www.xxx.com
ADD jdk-8u45-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_45
ADD apache-tomcat-8.0.46.tar.gz /usr/local
RUN rm -f /usr/local/*.tar.gz
WORKDIR /usr/local/apache-tomcat-8.0.46
EXPOSE 8080
ENTRYPOINT ["./bin.catalina.sh","run"]
创建容器
docker run -itd
--name=tomcat
-p 8080:8080
--mount type=bind,src=/app/webapps/,dst=/usr/local/apache-tomcat-8.0.46/webapps
tomcat:v1