欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用dockerfile创建镜像

程序员文章站 2022-03-12 12:41:24
...

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.34

MAINTAINER  fengzp <[email protected]>
RUN apt-get update
RUN apt-get -y install vim
RUN usermod -u 1000 mysql
RUN mkdir -p /var/run/mysqld
RUN chmod -R 777 /var/run/mysqld

VOLUME /etc/mysql
VOLUME /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_ci

docker cp mysql:/etc/mysql/ /Users/fengzp/Documents/docker/mysql/conf
docker cp mysql:/var/lib/mysql/ /Users/fengzp/Documents/docker/mysql/data
docker cp mysql:/script /Users/fengzp/Documents/docker/mysql/

docker stop mysql && docker rm mysql

docker 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 openjdk8

FROM centos:7

MAINTAINER fengzp <[email protected]>

ENV LANG en_US.UTF-8
ENV TOMCAT_VERSION 8.5.13
ENV CATALINA_HOME /opt/apache-tomcat-$TOMCAT_VERSION
ENV PATH $CATALINA_HOME/bin:$PATH
ENV JDK_VERSION 1.8.0

WORKDIR $CATALINA_HOME

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

RUN yum -y install java-$JDK_VERSION-openjdk-devel && rm -rf /var/cache/yum/*
ENV JAVA_HOME /usr/lib/jvm/java-openjdk

RUN yum -y install wget
RUN 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.gz
RUN cd /opt/ && rm -rf apache-tomcat-$TOMCAT_VERSION.tar.gz
RUN chmod +x $CATALINA_HOME/bin/*.sh

VOLUME $CATALINA_HOME/webapps 
VOLUME $CATALINA_HOME/logs
VOLUME $CATALINA_HOME/conf

EXPOSE 8080

CMD $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-jdk8

docker 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 tomcat8

docker 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.10

MAINTAINER  fengzp <[email protected]>

ENV LANG en_US.UTF-8

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
RUN apt-get update
RUN apt-get -y install vim

VOLUME /usr/share/nginx/html
VOLUME /etc/nginx/conf.d
VOLUME /var/log/nginx

构建容器:

docker build -t nginx-fzp .

docker run -d --name nginx -h nginx --restart always -p 80:80 -p 3343:3343 nginx-fzp

docker cp nginx:/usr/share/nginx/html /Users/fengzp/Documents/docker/nginx/html
docker cp nginx:/etc/nginx/conf.d /Users/fengzp/Documents/docker/nginx/conf.d
docker cp nginx:/var/log/nginx /Users/fengzp/Documents/docker/nginx/logs

docker stop nginx && docker rm nginx

docker 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