下载 ubuntu 镜像

$ docker pull ubuntu:18.04

18.04: Pulling from library/ubuntu
5bed26d33875: Pull complete 
f11b29a9c730: Pull complete 
930bda195c84: Pull complete 
78bf9a5ad49e: Pull complete 
Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               4e5021d210f6        2 weeks ago         64.2MB

这个镜像是真的小,只有 64M.

启动一个容器

$ docker run -it ubuntu:18.04
root@2467ccc1a35b:/#

一开始发现容器内系统连不了网,后来也不知怎么回事,重启几次docker或容器就好了。。。

修改容器内 apt-get 源

18.04 对应的是 bionic,如果版本不对,比如换成trusty,apt-get 是不会自动解决依赖问题的!!

cd /etc/apt
cp sources.list sources.list.bak
echo "">sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main multiverse restricted universe">>sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main multiverse restricted universe">>sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main multiverse restricted universe">>sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main multiverse restricted universe">>sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main multiverse restricted universe">>sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main multiverse restricted universe">>sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main multiverse restricted universe">>sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main multiverse restricted universe">>sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main multiverse restricted universe">>sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main multiverse restricted universe">>sources.list
apt-get update

安装 java

apt-get install software-properties-common
apt-get install default-jre
apt-get install default-jdk
java -version 

如果正确安装好了 java,则会打印版本信息:

openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)

查看 java 安装路径

$ update-alternatives --config java

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.

$ cd /usr/lib/jvm/java-11-openjdk-amd64  
$ ls

bin  conf  docs  include  jmods  legal  lib  man  release

给 jdk11 添加 jre

我们惊奇地发现 jdk11 没有 jre 的目录,手动添加 jre

$ bin/jlink --module-path jmods --add-modules java.desktop --output jre
$ ls

bin  conf  docs  include  jmods  jre  legal  lib  man  release

$ ls jre

bin  conf  include  legal  lib  release

配置环境变量 JAVA_HOME

echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc

退出当前容器

root@2467ccc1a35b:/# exit

保存镜像

将装好 java 的镜像保存为一个副本

$ docker commit -m "java install" 2467ccc1a35b ubuntu:java

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              java                1ab4e3bba1f5        6 seconds ago       741MB
ubuntu              latest              4e5021d210f6        2 weeks ago         64.2MB

新的镜像变成了 741M,下次只要从这个镜像创建容器,就可以直接使用 java 了!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐