alpine镜像集成常用数据库客户端
业务需求,做一个包含各种命令的容器,所以基于alpine操作系统做了个镜像,包含:curl、openssl、openjdk17、postgresql16-client、mysql-client、mssql-tools
由于alpine官方镜像源里没有mssql-tools的包,参考了微软官方文档:https://learn.microsoft.com/zh-cn/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=alpine18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline,以及github上开源项目:https://github.com/croblesm/mssql-tools-alpine,自己做了一个mssql-tools18版本的镜像
话不多说
# 目录结构
├── Dockerfile
├── cmd-tools.yaml
└── OpenJDK17U-jdk_x64_alpine-linux_hotspot_17.0.9_9.tar.gz
1
2
3
2
3
# Dockerfile
FROM alpine:latest
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 安装curl、openssl
RUN apk --no-cache add bash curl openssl
# 安装openjdk17
COPY OpenJDK17U-jdk_x64_alpine-linux_hotspot_17.0.9_9.tar.gz /tmp/openjdk.tar.gz
RUN mkdir -p /opt/jdk && \
tar -xzvf /tmp/openjdk.tar.gz -C /opt/jdk --strip-components 1 && \
rm /tmp/openjdk.tar.gz
ENV JAVA_HOME=/opt/jdk
ENV PATH=${PATH}:${JAVA_HOME}/bin
# 安装postgresql客户端
RUN apk --no-cache add postgresql16-client
# 安装mysql客户端
RUN apk --no-cache add mysql-client
# 安装sqlserver客户端
WORKDIR /tmp
RUN apk add --no-cache curl gnupg --virtual .build-dependencies -- && \
#Download the desired package(s)
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/msodbcsql18_18.3.2.1-1_amd64.apk && \
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/mssql-tools18_18.3.1.1-1_amd64.apk && \
#(Optional) Verify signature, if 'gpg' is missing install it using 'apk add gnupg'
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/msodbcsql18_18.3.2.1-1_amd64.sig && \
curl -O https://download.microsoft.com/download/3/5/5/355d7943-a338-41a7-858d-53b259ea33f5/mssql-tools18_18.3.1.1-1_amd64.sig && \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import - && \
gpg --verify msodbcsql18_18.3.2.1-1_amd64.sig msodbcsql18_18.3.2.1-1_amd64.apk && \
gpg --verify mssql-tools18_18.3.1.1-1_amd64.sig mssql-tools18_18.3.1.1-1_amd64.apk && \
#Install the package(s)
echo y | apk add --allow-untrusted msodbcsql18_18.3.2.1-1_amd64.apk mssql-tools18_18.3.1.1-1_amd64.apk && \
# Deleting packages
apk del .build-dependencies && rm -f msodbcsql*.sig mssql-tools*.apk
WORKDIR /
# Adding SQL Server tools to $PATH
ENV PATH=$PATH:/opt/mssql-tools18/bin
CMD ["/bin/sh"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 镜像build
docker build -t cmd-tools:v1 .
1
# yaml:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: cmd-tools
labels:
app: cmd-tools
spec:
replicas: 1
selector:
matchLabels:
app: cmd-tools
template:
metadata:
labels:
app: cmd-tools
spec:
containers:
- name: cmd-tools
image: cmd-tools:v1
imagePullPolicy: Never
command: [ "/bin/sh", "-c" ]
args: [ "tail -f /dev/null" ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# k8s启动容器
kubectl apply -f cmd-tools.yaml
1
# 数据库连接示例
kubectl exec -it cmd-tools-0 sh
# psql -h xx.xx.xx.xx -p 5432 -U 用户 -W 密码
# mysql -h xx.xx.xx.xx -P 3306 -u用户 -p 密码
# sqlcmd -S xx.xx.xx.xx -U 用户 -P 密码 -N -C
1
2
3
4
2
3
4
上次更新: 2024/03/13, 13:59:39