博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用dockerfile创建镜像
阅读量:7167 次
发布时间:2019-06-29

本文共 4073 字,大约阅读时间需要 13 分钟。

dockerfile命令

1、FROM(指定基础镜像)

FROM < image>:< tag>

2、MAINTAINER(指定镜像创建者信息)

MAINTAINER

3、RUN(运行命令)

RUN可以运行任何被基础image支持的命令。

例如: RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai

4、ENV(设置环境变量)

例如:ENV LANG en_US.UTF-8

5、WORKDIR(切换目录)

相当于cd命令,对RUN,CMD,ENTRYPOINT生效。

6、CMD(设置容器启动时执行的操作)

例如:CMD $CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out

7、EXPOSE(指定容器需要映射到宿主机器的端口)

例如:EXPOSE 8080 9090

8、VOLUME(指定挂载点)

例如:VOLUME $CATALINA_HOME/webapps $CATALINA_HOME/logs $CATALINA_HOME/conf

下面是一些dockfile例子

mysql

FROM mysql:5.6.34MAINTAINER  fengzp 
RUN apt-get updateRUN apt-get -y install vimRUN usermod -u 1000 mysqlRUN mkdir -p /var/run/mysqldRUN chmod -R 777 /var/run/mysqldVOLUME /etc/mysqlVOLUME /script

构建容器:

docker build -t mysql-5.6.34 .docker run --name mysql -h mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql-5.6.34 --character-set-server=utf8 --collation-server=utf8_general_cidocker cp mysql:/etc/mysql/ /Users/fengzp/Documents/docker/mysql/confdocker cp mysql:/var/lib/mysql/ /Users/fengzp/Documents/docker/mysql/datadocker cp mysql:/script /Users/fengzp/Documents/docker/mysql/docker stop mysql && docker rm mysqldocker run --name mysql -h mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /Users/fengzp/Documents/docker/mysql/data:/var/lib/mysql -v /Users/fengzp/Documents/docker/mysql/script:/script -v /Users/fengzp/Documents/docker/mysql/conf:/etc/mysql -d mysql-5.6.34 --character-set-server=utf8 --collation-server=utf8_general_ci

tomcat

#tomcat8 openjdk8FROM centos:7MAINTAINER fengzp 
ENV LANG en_US.UTF-8ENV TOMCAT_VERSION 8.5.13ENV CATALINA_HOME /opt/apache-tomcat-$TOMCAT_VERSIONENV PATH $CATALINA_HOME/bin:$PATHENV JDK_VERSION 1.8.0WORKDIR $CATALINA_HOMERUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeRUN yum -y install java-$JDK_VERSION-openjdk-devel && rm -rf /var/cache/yum/*ENV JAVA_HOME /usr/lib/jvm/java-openjdkRUN yum -y install wgetRUN cd /opt/ && wget "http://apache.fayea.com/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz"RUN cd /opt/ && tar -zxf apache-tomcat-$TOMCAT_VERSION.tar.gzRUN cd /opt/ && rm -rf apache-tomcat-$TOMCAT_VERSION.tar.gzRUN chmod +x $CATALINA_HOME/bin/*.shVOLUME $CATALINA_HOME/webapps VOLUME $CATALINA_HOME/logsVOLUME $CATALINA_HOME/confEXPOSE 8080CMD $CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out

构建容器:

docker build -t tomcat8-jdk8 .docker run -d --name tomcat8 -h tomcat8 tomcat8-jdk8docker cp tomcat8:/opt/apache-tomcat-8.5.13/webapps /Users/fengzp/Documents/docker/tomcat8/docker cp tomcat8:/opt/apache-tomcat-8.5.13/conf /Users/fengzp/Documents/docker/tomcat8/docker cp tomcat8:/opt/apache-tomcat-8.5.13/logs /Users/fengzp/Documents/docker/tomcat8/docker stop tomcat8 && docker rm tomcat8docker run -d --name tomcat8 -h tomcat8 -p 42000:8080 -v /Users/fengzp/Documents/docker/tomcat8/webapps:/opt/apache-tomcat-8.5.13/webapps -v /Users/fengzp/Documents/docker/tomcat8/logs:/opt/apache-tomcat-8.5.13/logs -v /Users/fengzp/Documents/docker/tomcat8/conf:/opt/apache-tomcat-8.5.13/conf tomcat8-jdk8

nginx

FROM nginx:1.11.10MAINTAINER  fengzp 
ENV LANG en_US.UTF-8RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeRUN apt-get updateRUN apt-get -y install vimVOLUME /usr/share/nginx/htmlVOLUME /etc/nginx/conf.dVOLUME /var/log/nginx

构建容器:

docker build -t nginx-fzp .docker run -d --name nginx -h nginx --restart always -p 80:80 -p 3343:3343 nginx-fzpdocker cp nginx:/usr/share/nginx/html /Users/fengzp/Documents/docker/nginx/htmldocker cp nginx:/etc/nginx/conf.d /Users/fengzp/Documents/docker/nginx/conf.ddocker cp nginx:/var/log/nginx /Users/fengzp/Documents/docker/nginx/logsdocker stop nginx && docker rm nginxdocker run -d --name nginx -h nginx --restart always -p 80:80 -p 3343:3343 -v /Users/fengzp/Documents/docker/nginx/html:/usr/share/nginx/html -v /Users/fengzp/Documents/docker/nginx/conf.d:/etc/nginx/conf.d -v /Users/fengzp/Documents/docker/nginx/logs:/var/log/nginx nginx-fzp

构造镜像命令

docker build -t <私服地址:>imagesname

转载于:https://www.cnblogs.com/andyfengzp/p/6424150.html

你可能感兴趣的文章