Dockerfile常用指令
指令 | 描述 |
---|---|
FROM | 构建新镜像是基于哪个镜像 |
LABEL | 标签 |
RUN | 构建镜像时运行的Shell命令 |
COPY | 拷贝文件或目录到镜像中 |
ADD | 解压压缩包并拷贝 |
ENV | 设置环境变量 |
USER | 为RUN、CMD和ENTRYPOINT执行命令指定运行用户 |
EXPOSE | 声明容器运行的服务端口 |
WORKDIR | 为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录 |
CMD | 运行容器时默认执行,如果有多个CMD指令,最后一个生效 |
ENTRYPOINT | 如果与CMD一起用,CMD将作为ENTRYPOINT的默认参数,如果如果有多个 ENTRYPOINT指令,最后一个生效 |
镜像分类
001 基础镜像,centos、Ubuntu、alpine
002 环境镜像,java、php、go
003 项目镜像,将项目与环境镜像一起打包
验证COPY、ADD、ENV
[root@es3 test2]# vi Dockerfile
FROM centos:latest
LABEL maintalner lizhenliang
RUN yum install wget curl -y
COPY a.txt /opt
ADD b.tar.gz /opt
ENV ABC=123
EXPOSE 80
WORKDIR /usr/local
CMD ["sleep","360000"]
[root@es3 test]# ls
a.txt b.tar.gz Dockerfile
[root@es3 test2]# docker build -t test .
[root@es3 test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest b1509b20753d 30 seconds ago 277MB
[root@es3 test]# docker run -d --name=test test
[root@es3 test]# docker exec -it test bash
[root@352b603d469c local]# pwd
/usr/local
[root@352b603d469c local]# echo $ABC
123
[root@352b603d469c local]# ls /opt
a.txt b.txt
#环境变量可以先默认,然后通过传参改变,不传参就是默认
[root@es3 test]# docker run -d --name=test2 -e ABC=666 test
[root@es3 test]# docker exec -it test2 bash
[root@b2f4f322307d local]# echo $ABC
666
验证脚本
[root@es3 test2]# vi run.sh
#!/bin/bash
echo hello $1
sleep 60000
[root@es3 test2]# chmod 777 run.sh
[root@es3 test2]# vi Dockerfile
FROM centos:latest
LABEL maintalner lizhenliang
COPY run.sh /usr/bin
ENV NAME=aliang
EXPOSE 80
CMD ["sh","-c","/usr/bin/run.sh $NAME"]
[root@es3 test2]# docker build -t test2 .
[root@es3 test2]# docker run -d --name test21 test2
[root@es3 test2]# docker logs -f test21
hello aliang
CMD与ENTRYPOINT联合使用
[root@es3 test3]# vi Dockerfile
FROM centos:latest
LABEL maintalner lizhenliang
ENTRYPOINT ["echo"]
CMD ["hello","aliang"]
[root@es3 test3]# docker build -t test3 .
[root@es3 test3]# docker run -d --name test31 test3
[root@es3 test3]# docker logs -f test31
hello aliang
[root@es3 test3]# docker run -d --name test32 test3 hello mv1 mv2 mv3
[root@es3 test3]# docker logs -f test32
hello mv1 mv2 mv3
CMD与ENTRYPOINT区别
001 CMD和ENTRYPOINT指令都可以用来定义运行容器时所使用的默认命令
002 Dockerfile至少指定一个CMD或ENTRYPOINT
003 CMD可以用作ENTRYPOINT默认参数,或者用作容器的默认命令
004 docker run指定<command>时,将会覆盖CMD
005 如果是可执行文件,希望运行时传参,应该使用ENTRYPOINT
前端项目镜像构建与部署Nginx
[root@es3 nginx]# vi Dockerfile
FROM centos:7
LABEL maintainer www.ctnrs.com
RUN yum install -y gcc gcc-c++ make \
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
ADD nginx-1.15.5.tar.gz /
RUN cd nginx-1.15.5 && \
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make -j 4 && make install && \
mkdir /usr/local/nginx/conf/vhost && \
cd / && rm -rf nginx* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
[root@es3 nginx]# ls
Dockerfile nginx-1.15.5.tar.gz nginx.conf php.conf
[root@es3 nginx]# docker build -t nginx:v1 .
[root@es3 nginx]# docker run -d -p 8011:80 --name=test1 nginx:v1
http://192.168.153.27:8011/
---------------------------------------------------------------------------
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
------------------------------------------------------------------------------
[root@es3 nginx]# docker run -d -v /opt/wwwroot:/usr/local/nginx/html -p 8012:80 --name=test2 nginx:v1
[root@es3 nginx]# vi /opt/wwwroot/index.html
hello nginx
http://192.168.153.27:8012/
hello nginx
[root@es3 nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 3209dc7241b5 9 minutes ago 379MB
PHP项目镜像构建:PHP
[root@es3 php]# vi Dockerfile
FROM centos:7
MAINTAINER www.ctnrs.com
RUN yum install epel-release -y && \
yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel autoconf \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
ADD php-5.6.36.tar.gz /
RUN cd php-5.6.36 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --enable-opcache \
--with-mysql --with-mysqli --with-pdo-mysql \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-mbstring --with-mcrypt --enable-hash && \
make -j 4 && make install && \
cp php.ini-production /usr/local/php/etc/php.ini && \
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
mkdir /usr/local/php/log && \
cd / && rm -rf php* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]
[root@es3 php]# docker build -t php:v1 .
[root@es3 php]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v1 c89e43978b56 About a minute ago 634MB
nginx v1 3209dc7241b5 22 minutes ago 379MB
容器化搭建个人博客系统
1638698233867.png自定义网络
[root@es3 php]# docker network create lnmp
创建Mysql容器
docker run -d \
--name lnmp_mysql \
--net lnmp \
--mount src=mysql-vol,dst=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8
创建PHP容器
docker run -d --name lnmp_php --net lnmp \
--mount src=wwwroot,dst=/wwwroot php:v1
创建Nginx容器
[root@es3 dockerfile]# cd nginx/
[root@es3 nginx]# ls
Dockerfile nginx-1.15.5.tar.gz nginx.conf php.conf
docker run -d --name lnmp_nginx --net lnmp -p 88:80 --mount src=wwwroot,dst=/wwwroot \
--mount type=bind,src=$PWD/php.conf,dst=/usr/local/nginx/conf/vhost/php.conf nginx:v1
wordpress博客
相关环境
[root@es3 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8895c1ec5d0c nginx:v1 "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp lnmp_nginx
bb25cc01227a php:v1 "php-fpm" About a minute ago Up About a minute 9000/tcp lnmp_php
b2aa872b8c38 mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp lnmp_mysql
test.php
[root@es3 nginx]# docker volume inspect wwwroot
[
{
"CreatedAt": "2021-12-05T18:00:48+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/wwwroot/_data",
"Name": "wwwroot",
"Options": null,
"Scope": "local"
}
]
[root@es3 nginx]# vi /var/lib/docker/volumes/wwwroot/_data/test.php
<?php phpinfo();?>
http://192.168.153.27:88/test.php
1638698963460.png
wordpress
[root@es3 dockerfile]# cp -r wordpress-4.9.4-zh_CN.tar.gz /var/lib/docker/volumes/wwwroot/_data
[root@es3 _data]# tar -xzvf wordpress-4.9.4-zh_CN.tar.gz
[root@es3 _data]# ls
test.php wordpress wordpress-4.9.4-zh_CN.tar.gz
[root@es3 _data]# mv wordpress/* ./
http://192.168.153.27:88/
1638699308070.png
1638699418629.png
1638699451194.png
[root@es3 _data]# vi wp-config.php
......
#手工创建wp-config.php文件,并将以下文字粘贴于其中
1638699629054.png
1638699657427.png
1638699703688.png
JAVA项目镜像构建:Tomcat
tomcat:v1
[root@es3 tomcat]# ls
apache-tomcat-8.5.43.tar.gz Dockerfile ROOT.war
[root@es3 tomcat]# vi Dockerfile
FROM centos:7
MAINTAINER www.ctnrs.com
ENV VERSION=8.5.43
RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
yum clean all && \
rm -rf /var/cache/yum/*
ADD apache-tomcat-${VERSION}.tar.gz /usr/local/
RUN mv /usr/local/apache-tomcat-${VERSION} /usr/local/tomcat && \
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
[root@es3 tomcat]# docker build -t tomcat:v1 .
[root@es3 tomcat]# docker run -d -p 802:8080 --name=tomcat1 tomcat:v1
http://192.168.153.27:802/
1638700524625.png
tomcat:v2
[root@es3 tomcat]# vi Dockerfile2
FROM tomcat:v1
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ROOT.war /usr/local/tomcat/webapps
[root@es3 tomcat]# docker build -t tomcat:v2 -f Dockerfile2 .
[root@es3 tomcat]# docker run -d -p 803:8080 --name=tomcat2 tomcat:v2
http://192.168.153.27:803/
JAVA微服务镜像构建:Jar
[root@es3 java]# vi Dockerfile
FROM java:8-jdk-alpine
LABEL maintainer www.ctnrs.com
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY hello.jar /
EXPOSE 8888
CMD ["/bin/sh", "-c", "java -jar $JAVA_OPTS /hello.jar"]
[root@es3 java]# docker build -t hello:v1 .
[root@es3 java]# docker run -d --name=hello1 -p 806:8888 hello:v1
[root@es3 java]# docker logs -f hello1
Hello world!
sleep 1 day...
编写Dockerfile最佳实践
• 减少镜像层:一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层。
• 优化镜像大小:一次RUN形成新的一层,如果没有在同一层删除,无论文件是否最后删除,
都会带到下一层,所以要在每一层清理对应的残留数据,减小镜像大小。
• 减少网络传输时间:例如软件包、mvn仓库等
• 多阶段构建:代码编译、部署在一个Dockerfile完成,只会保留部署阶段产生数据。
• 选择最小的基础镜像:例如alpine
网友评论