我们使用Docker compose和开源工具Dobi(GitHub - dnephin/dobi: A build automation tool for Docker applications)从一个Linux基础容器镜像(我们使用Almalinux),构建支持SSH两个容器,以及一个具备X11 forwarding的有Google chrome浏览器和Jetbain idea集成开发工具的容器。借鉴如此容器环境构建,可以很方便来搭建基于Windows下容器环境的Hadoop、Spark大数据教学开发的环境。可以参考本作者的另外几篇文章:Windows PC上创建大数据职业技能竞赛实验环境之一 / 七。
首先我们来看看什么是Docker compose。Docker compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。Windows下安装Docker Desktop,应该也有了Docker compose。
C:\MyDevz\dockerp>docker version
Client:
Cloud integration: v1.0.29
Version: 20.10.21
API version: 1.41
Go version: go1.18.7
Git commit: baeda1f
Built: Tue Oct 25 18:08:16 2022
OS/Arch: windows/amd64
Context: default
Experimental: true
Server: Docker Desktop 4.14.1 (91661)
Engine:
Version: 20.10.21
API version: 1.41 (minimum version 1.12)
Go version: go1.18.7
Git commit: 3056208
Built: Tue Oct 25 18:00:19 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.9
GitCommit: 1c90a442489720eec95342e1789ee8a5e1b9536f
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
C:\MyDevz\dockerp>docker-compose version
Docker Compose version v2.13.0
开源工具Dobi是一款Docker应用的自动化构建工具,用Dobi能保持项目高效组织,方便重构。他采用dobi.yaml来具体定义容器构建的资源与任务,并直接用dobi运行任务。Dobi可以通过github下载,具有windows和linux的不同程序。这里是windows下载链接:https://github.com/dnephin/dobi/releases/download/v0.15.0/dobi-windows.exe
一 构建支持SSH的容器镜像
创建项目目录,我的是C:\mydevz\dockerp。进入项目于目录。将下载好的dobi拷贝到目录,修改文件名。
dobi-windows.exe -> dobi.exe
1 在当前目录下创建Dockerfile。
FROM almalinux:latest
MAINTAINER "LIU Gang from Hiroki Takeyama"
# sshd
RUN dnf -y install openssh-server openssh-clients; \
sed -i 's/^\(UsePAM yes\)/# \1/' /etc/ssh/sshd_config; \
ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' && \
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' && \
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''; \
sed -i 's/^#\(PermitRootLogin\) .*/\1 yes/' /etc/ssh/sshd_config; \
rm -f /run/nologin; \
dnf clean all;
# 免密
RUN sed -i 's/^#\(PubkeyAuthentication yes\)/\1/' /etc/ssh/sshd_config && \
echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config; \
ssh-keygen -t rsa -f /root/.ssh/id_rsa -P '' && \
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys;
# 基础应用
RUN dnf -y install net-tools iproute procps;
RUN dnf -y install bzip2 wget vim less;
RUN dnf clean all;
# 环境变量
ENV TIMEZONE Asia/Shanghai
ENV ROOT_PASSWORD ******* # root登录密码
RUN ln -fs /usr/share/zoneinfo/${TIMEZONE} /etc/localtime; \
echo "root:${ROOT_PASSWORD}" | chpasswd; \
touch /var/log/lastlog;
# 暴露ssh端口
EXPOSE 22
ENTRYPOINT ["/usr/sbin/init"]
在Dockerfile中通过ENTRYPOINT ["/usr/sbin/init"],让容器启动时启动systemd。
2 创建dobi.yaml。
meta:
project: dockerp
default: create
image=almalinux:
image: almalinux
context: .
dockerfile: Dockerfile
tags:
- 'S0.1'
# - '{env.VERSION}'
annotations:
description: "Build the almalinux image"
alias=create:
tasks: ['almalinux:build']
annotations:
description: "Create the images and containers"
alias=clean:
tasks: ['almalinux:rm']
annotations:
description: "Remove the images and containers"
这时我们可以来创建新的容器镜像almalinux:S0.1。
C:\MyDevz\dockerp>dobi --version
dobi version 0.15.0 (build: 5c1481d, date: Sat Feb 27 19:05:16 UTC 2021)
C:\MyDevz\dockerp>dobi list
Resources:
almalinux Build the almalinux image
clean Remove the images and containers
create Create the images and containers
C:\MyDevz\dockerp>dobi
[WARN] Failed to load auth config: no docker configuration found
Step 1/12 : FROM almalinux:latest
---> 39f63d416992
Step 2/12 : MAINTAINER "LIU Gang from Hiroki Takeyama"
---> Running in ecec068122c7
Removing intermediate container ecec068122c7
---> b84502db34ad
Step 3/12 : RUN dnf -y install openssh-server openssh-clients; sed -i 's/^\(UsePAM yes\)/# \1/' /etc/ssh/sshd_config; ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' && ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''; sed -i 's/^#\(PermitRootLogin\) .*/\1 yes/' /etc/ssh/sshd_config; rm -f /run/nologin; dnf clean all;
---> Running in 21c50551fb82
AlmaLinux 8 - BaseOS 1.3 MB/s | 2.9 MB 00:02
AlmaLinux 8 - AppStream 3.2 MB/s | 9.9 MB 00:03
AlmaLinux 8 - Extras 7.5 kB/s | 18 kB 00:02
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
===================================================