使用Dockerfile 构建JDK1.8镜像
step1 编写Dockerfile
root@master:/BigData/DockerFile# mkdir JDK1.8
root@master:/BigData/DockerFile# cd JDK1.8
root@master:/BigData/DockerFile/JDK1.8# #将jdk1.8压缩包放入该文件夹
root@master:/BigData/DockerFile/JDK1.8# vi Dockerfile
查看jdk1.8压缩包是否在文件夹内:
Dockerfile内容:
FROM ubuntu:16.04
MAINTAINER xxxxxx
WORKDIR /usr
RUN mkdir /usr/local/java
ADD jdk-8u181-linux-x64.tar.gz /usr/local/java/
ENV JAVA_HOME=/usr/local/java/jdk1.8.0_181
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLAS
SPATH
ENV PATH $JAVA_HOME/bin:$PATH
step2 安装镜像
root@master:/BigData/DockerFile/JDK1.8# docker build -t ubuntu/jdk1.8 . #构建镜像
Sending build context to Docker daemon 185.6MB
Step 1/10 : FROM ubuntu:16.04
---> 657d80a6401d
Step 2/10 : MAINTAINER zhaoyanyan
---> Running in aaac139797be
Removing intermediate container aaac139797be
---> ec7b02080508
Step 3/10 : WORKDIR /usr
---> Running in 583887245a6c
Removing intermediate container 583887245a6c
---> 6d9b8dc92d71
Step 4/10 : RUN mkdir /usr/local/java
---> Running in 57a3c89b2682
Removing intermediate container 57a3c89b2682
---> e4e2980731e0
Step 5/10 : ADD jdk-8u181-linux-x64.tar.gz /usr/local/java/
---> b5b84bf4904d
Step 6/10 : ENV JAVA_HOME=/usr/local/java/jdk1.8.0_181
---> Running in 993a1816e48a
Removing intermediate container 993a1816e48a
---> 5cb9958f12cf
Step 7/10 : ENV JRE_HOME $JAVA_HOME/jre
---> Running in 947957d73fb6
Removing intermediate container 947957d73fb6
---> 49c90fb57061
Step 8/10 : ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
---> Running in c782326263c2
Removing intermediate container c782326263c2
---> c0836e86267a
Step 9/10 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 1a3e76ef1133
Removing intermediate container 1a3e76ef1133
---> c237a8dd6898
Step 10/10 : CMD /usr/local/java/jdk1.8.0_181/bin/java -version
---> Running in fe9d61fb6c55
Removing intermediate container fe9d61fb6c55
---> a0f8080bbcee
Successfully built a0f8080bbcee
Successfully tagged ubuntu/jdk1.8:latest
step3 验证安装
root@master:/BigData/DockerFile/JDK1.8# docker run -it ubuntu/jdk1.8
root@49c73b230e90:/usr#
root@49c73b230e90:/usr# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
step4 保存修改的容器
修改后的容器,如果直接stop掉,则这些改动在下次启动时不会保留,因此需要把它保存到本地另一个镜像中。
root@master:/BigData/DockerFile/JDK1.8# docker ps -l #查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49c73b230e90 ubuntu/jdk1.8 "/bin/bash" About a minute ago Exited (0) 3 seconds ago amazing_mclaren
root@master:/BigData/DockerFile/JDK1.8# docker commit 49c73b230e90 jdk1.8:java #保存容器
sha256:65ecc6893c7b3055104440ed37ec2ac45acc46b19f7118cda031f371c9f4288c
root@master:/BigData/DockerFile/JDK1.8# docker stop 49c73b230e90 #停掉容器
49c73b230e90
root@master:/BigData/DockerFile/JDK1.8# docker images #查看镜像 可以看到已经在磁盘上了
REPOSITORY TAG IMAGE ID CREATED SIZE
jdk1.8 java 65ecc6893c7b 5 minutes ago 503MB
step5 导出镜像文件
root@master:/BigData/DockerFile/JDK1.8# docker export -o jdk1.8_run.tar 49c73b230e90 #导出 49c73b230e90容器到文件jdk1.8_run.tar
root@master:/BigData/DockerFile/JDK1.8# ll #查看jdk1.8_run.tar文件是否存在磁盘 这样导出的tar文件可以通过导入命令 在不同机器之间进行拷贝和迁移
total 643308
drwxr-xr-x 2 root root 4096 10月 7 19:47 ./
drwxr-xr-x 4 root root 4096 10月 7 19:04 ../
-rw-r--r-- 1 root root 366 10月 7 19:31 Dockerfile
-rw------- 1 root root 473084928 10月 7 19:47 jdk1.8_run.tar
使用Dockerfile 构建Python3开发环境镜像
step1 编写Dockerfile
root@master:/BigData# mkdir DockerFile
root@master:/BigData# cd DockerFile
root@master:/BigData/DockerFile# mkdir python3
root@master:/BigData/DockerFile# cd python3
root@master:/BigData/DockerFile/python3# vi Dockerfile
root@master:/BigData/DockerFile/python3#
其中Dockerfile内容:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get upgrade -y
# Install python3
RUN apt-get install -y python3
# Install pip
RUN apt-get install -y wget vim
RUN wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
RUN python3 /tmp/get-pip.py
RUN pip install --upgrade pip
#Install other libs
RUN pip install -U numpy
RUN pip install -U matplotlib
RUN pip install -U pandas
RUN pip install -U seaborn
RUN pip install -U scikit-learn
# Install python-dev
RUN apt-get update
RUN apt-get install -y python-dev
# Install jupyter notebook
RUN pip install jupyter
RUN pip install notebook
step2 安装镜像
root@master:/BigData/DockerFile/python3# docker build -t ubuntu/python3 .
step3 验证安装
root@master:/BigData/DockerFile/python3# docker run -it ubuntu/python3
root@4f4c6665f006:/#
root@4f4c6665f006:/# pip -V
pip 19.2.3 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
root@4f4c6665f006:/# python3.5
Python 3.5.2 (default, Jul 10 2019, 11:58:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
同样需要保存修改后的容器,也可以导出镜像,具体操作可参考JDK1.8。