桥山之巅,姬水之畔

Dockerfile使用-集成JAVA Python Redis Tomcat Nginx镜像,Based on Ubuntu18.04

2019.08.22

镜像信息

  • Based on ubuntu18.04

  • Environment

  • jdk8 >> apt install -y openjdk-8-jdk

  • tomcat7 >> install dir:/opt/tomcat-7.0.96

  • python3.7 >> install dir:/opt/Python-3.7.4

  • nginx1.14 >> apt-get install -y nginx

  • redis5.0.5 >> install dir:/opt/redis-5.0.5

  • general-tools

  • vim >> apt-get install -y vim

  • net-tools >> apt-get install -y net-tools

  • curl >> apt install -y curl

  • cron >> apt-get install -y cron

  • inetutils-ping >> apt-get install -y inetutils-ping

  • wget >> apt-get install -y wget

Dockerfile

# BASE IMAGE
FROM ubuntu:latest

# tzdata时区
ENV DEBIAN_FRONTEND=noninteractive TIME_ZONE=Asia/Shanghai


# 镜像标签, 起说明作用
LABEL name="Prod Ubuntu" \
	build-data="2019-08-21" \
	maintainer="614580167@qq.com" \
	version="1.0"
	
	
# 添加阿里源
RUN /bin/bash -c awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {} main multiverse restricted universe \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {} main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {}-backports main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {}-proposed main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {}-security main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb http://mirrors.aliyun.com/ubuntu/ {}-updates main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb-src http://mirrors.aliyun.com/ubuntu/ {} main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb-src http://mirrors.aliyun.com/ubuntu/ {}-backports main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb-src http://mirrors.aliyun.com/ubuntu/ {}-proposed main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb-src http://mirrors.aliyun.com/ubuntu/ {}-security main multiverse restricted universe >> /etc/apt/sources.list \
	&& awk 'NR==3{print substr($1,18)}' /etc/lsb-release | xargs -I {} echo deb-src http://mirrors.aliyun.com/ubuntu/ {}-updates main multiverse restricted universe >> /etc/apt/sources.list

# 更新源
RUN apt-get -y update && apt-get -y upgrade && apt-get -y dist-upgrade


# install general-tools
RUN apt-get install -y vim net-tools curl cron inetutils-ping wget lsof

# vi 编辑器显示行号
RUN /bin/bash -c echo 'set nu'>>/etc/vim/vimrc


# 安装python3.7
# 安装相关依赖
RUN apt-get install -y tzdata \
	&& ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime \
	&& echo $TIME_ZONE > /etc/timezone \
	&& dpkg-reconfigure --frontend $DEBIAN_FRONTEND tzdata \
	&& apt-get install -y gcc make zlib* \
	&& apt-get install -y build-essential python-dev python-setuptools python-pip python-smbus \
	&& apt-get install -y libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev tk-dev libsqlite3-dev \
	&& apt-get install -y libssl-dev openssl libffi-dev
	
WORKDIR /opt/
RUN wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz && tar -zxvf Python-3.7.4.tgz
RUN cd /opt/Python-3.7.4 && ./configure && make && make install

# 默认使用python3.7
RUN rm /usr/bin/python 
RUN ln -s /usr/local/bin/python3.7 /usr/bin/python

WORKDIR /opt/
RUN rm Python-3.7.4.tgz


# 安装Tomcat7
RUN cd /opt/ \
 	&& wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.96/bin/apache-tomcat-7.0.96.tar.gz \
 	&& tar -zxvf apache-tomcat-7.0.96.tar.gz \
 	&& mv apache-tomcat-7.0.96 tomcat-7.0.96 \
 	&& rm apache-tomcat-7.0.96.tar.gz
	
# 安装Tomcat7(方式二)
#  编译命令:docker build -f /opt/docker/Dockerfile2 -t ysl_env_tomcat:1.0 /opt/docker/ 
#  其中 命令最后的/opt/docker 指定的是 build 上下文,ADD指令中apache-tomcat-7.0.96.tar.gz 的位置是相对上下文的
# ADD apache-tomcat-7.0.96.tar.gz /opt/
# RUN cd /opt/ && mv apache-tomcat-7.0.96 tomcat-7.0.96


# 配置环境变量
ENV CATALINE_HOME /opt/tomcat-7.0.96
ENV CATALINE_BASE /opt/tomcat-7.0.96
ENV PATH $PATH:$CATALINE_HOME:$CATALINE_HOME/lib:$CATALINE_HOME/bin


# 安装redis
RUN cd /opt/ \
	&& wget http://download.redis.io/releases/redis-5.0.5.tar.gz \
	&& tar -zxvf redis-5.0.5.tar.gz \
	&& cd /opt/redis-5.0.5 \
	&& make \
	&& make install \
	&& cp redis.conf redis.conf.bak \
	&& sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /opt/redis-5.0.5/redis.conf \
	&& sed -i 's/protected-mode yes/protected-mode no/g' /opt/redis-5.0.5/redis.conf \
	&& sed -i 's/daemonize no/daemonize yes/g' /opt/redis-5.0.5/redis.conf \
	&& cd /opt/ \
	&& rm redis-5.0.5.tar.gz


# 安装nginx
RUN apt-get install -y nginx

# 安装java8
RUN apt install -y openjdk-8-jdk


WORKDIR /


# 对外暴露8080端口
# EXPOSE 8080


# 导致新的镜像不能启动
# release info
# ENTRYPOINT echo "\n\n\n\t\t\t\t \
# 				 Welcome to ysl_prod_ubuntu:1.0 (based on Ubuntu 18.04) \n\
# 				 Dockerfile:	https://blog.csdn.net/github_33809414 \n\
# 				 Build-data:	2019-08-21 \n\
# 				 Support:	614580167@qq.com"


# 导致新的镜像不能启动
# ENTRYPOINT ["/bin/echo", "\n\n\n\t\t\t\t"] 
# CMD ["Welcome to ysl_ubuntu:1.0 (based on Ubuntu 18.04) \n\
# 				 Dockerfile:	https://blog.csdn.net/github_33809414 \n\
# 				 Build-data:	2019-08-21 \n\
# 				 Support:	614580167@qq.com"]				 


#CMD echo "该指令不会被执行,只会执行最后一条"

# Default command
CMD ["/bin/bash"]

将Dockerfile编译为镜像

docker build -f Dockerfile的路径 -t 镜像名:tag

遇到的问题

1. Dockerfile交互问题

  1. 如果是apt安装场合,使用-y 参数解决
RUN apt-get install -y xxx
  1. 安装脚本场合 比如我们用sh Anaconda3-2019.07-Linux-x86_64.sh来安装anaconda的时候,”烦人”的anaconda会问四个问题,我的回答顺序分别是Enter,yes,Enter,yes。于是你可以这样写:
RUN sh -c '/bin/echo -e "\nyes\n\nyes" | sh Anaconda3-2019.07-Linux-x86_64.sh'
  1. Dockerfile中安装tzdata交互场合
# 设置环境变量
ENV DEBIAN_FRONTEND noninteractive # 非交互模式
ENV TIME_ZONE Asia/Shanghai # 目标时区

# 安装tzdata软件包(默认:Etc/UTC)
RUN apt-get install -y tzdata 

# 建立到期望的时区的链接(ubuntu16.04以上需要),并且修改/etc/timezone
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone

# 重新配置tzdata软件包,使得时区设置生效
RUN dpkg-reconfigure --frontend $DEBIAN_FRONTEND tzdata

2. 对【每条指令都是在一个新的容器中执行】的理解

  • 以下指令报错,/bin/sh: 1: ./configure: not found
WORKDIR /opt/Python-3.7.4
RUN ./configure
RUN make
RUN make install

是因为,在执行RUN ./configure指令时,已经在一个新的容器中了,而在新的容器中没有进入到/opt/Python-3.7.4目录,自然找不到configure

  • 解决方法 将指令写成一条指令即可RUN cd /opt/Python-3.7.4 && ./configure && make && make install

3. Dockerfile中加入ENTRYPOINT指令,镜像能build成功,但是不能run起来

原因还没有找到,希望大神看到了告知