Docker-基础镜像sshd、java构建【二进制包】

1、ssh镜像构建

1.1、需求

我们定制一个标准的ssh基础镜像,用于我们远程连接或者进行其他的应用镜像的基础镜像

1.2、编写Dockerfile

1.2.1、创建Dockerfile专用目录

mkdir /docker/images/ssh -p && cd /docker/images/ssh

1.2.1、创建秘钥认证

ssh-keygen -t rsa -C test@qq.com -f ~/.ssh/id_rsa -P ""
cat ~/.ssh/id_rsa.pub >authorized_keys

1.2.3、定制dockerfile

cat << 'CAT_END' > Dockerfile
FROM ubuntu
MAINTAINER cyc author

RUN apt-get update && apt-get install -y openssh-server curl net-tools && \
mkdir -p /var/run/sshd && mkdir -p /root/.ssh && sed -i 's/.*pam_loginuid.so/#&/g' /etc/pam.d/sshd && \
apt-get autoclean && apt-get clean && apt-get autoremove

ADD authorized_keys /root/.ssh/authorized_keys

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]
CAT_END

1.3、构建镜像

docker build -t ubuntu-ssh:v0.1 /docker/images/ssh/

1.4、使用新镜像启动一个容器,查看效果

docker run -d -p 10022:22 ubuntu-ssh:v0.1

]# docker ps
CONTAINER ID   IMAGE                           COMMAND                   CREATED          STATUS          PORTS                                     NAMES
4a4448332de1   ubuntu_ssh:v0.1                 "/usr/sbin/sshd -D"       14 seconds ago   Up 13 seconds   0.0.0.0:10022->22/tcp, :::10022->22/tcp   inspiring_wiles
9cef3ce365c9   lorel/docker-stress-ng:latest   "/usr/bin/stress-ng …"   12 hours ago     Up 12 hours                                               cpu_stress

# docker port 4a4448332de1
22/tcp -> 0.0.0.0:10022
22/tcp -> [::]:10022

1.5、ssh测试登陆

]# ssh root@192.168.10.19 -p 10022
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 3.10.0-1160.el7.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Fri May 19 01:17:20 2023 from 192.168.10.19
root@4a4448332de1:~# ifconfig | grep inet
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        inet 127.0.0.1  netmask 255.0.0.0

2、java镜像构建

2.1、需求

定制标准的 java环境,以便于基于java环境的 业务环境使用

2.2、编写Dockerfile

2.2.1、创建Dockerfile专用目录

mkdir /docker/images/java -p && cd /docker/images/java

2.2.2、定制dockerfile

cat << 'CAT_END' > Dockerfile
FROM ubuntu
MAINTAINER cyc author

ADD jdk-8u121-linux-x64.tar.gz /usr/local/

RUN ln -s /usr/local/jdk1.8.0_121 /usr/local/java

ENV JAVA_HOME /usr/local/java
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin
CAT_END

2.3、构建镜像

docker build -t ubuntu-jdk:8u121 /docker/images/java/

2.4、使用新镜像启动一个容器并且测试是否生效

]# docker run -it --rm ubuntu-jdk:8u121 bash
root@f96f91672c95:/# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
root@f96f91672c95:
/# echo $JAVA_HOME /usr/local/java

 

posted @ 2023-05-19 14:53  小粉优化大师  阅读(108)  评论(0编辑  收藏  举报