dockerfile.md

概述

dockerfile 用于构建 docker 镜像

使用

1
2
3
4
docker build -t $name .

docker build -t $name:$tagname .

技巧

RUN

  • 尽量使用 && \ 表示多行语句,使用 && 连接命令,如果前一个命令成功执行(返回 0),则执行下一个命令。这是确保每个命令依赖于前一个命令成功完成的常用方法

CMD

  • CMD [“./server”] 这是它的 json 数组写法,意味着不使用 shell 进行执行,则不可以使用 cd 等命令
  • CMD cd build/sc/ && ./server 这是使用 sh -c 执行的写法

构建服务示例

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
# 使用官方 Golang 镜像
FROM golang:latest

WORKDIR /sc-core
# 将源代码复制到容器中
COPY . /sc-core
# 拷贝凭证文件到 Docker 容器中
COPY .git-credentials /root/.git-credentials

# 运行 go mod tidy 来清理依赖项
RUN git config --global credential.helper 'store --file=/root/.git-credentials' && \
go env -w GOPRIVATE=git.ouryun.cn,192.168.20.12 && \
go env -w GONOSUMDB=git.ouryun.cn,192.168.20.12 && \
go env -w GONOPROXY=git.ouryun.cn,192.168.20.12 && \
go env -w GOINSECURE=git.ouryun.cn,192.168.20.12 && \
go env -w GOPROXY="https://goproxy.cn|https://goproxy.io|direct" && \
go mod tidy -x

# 构建应用
RUN make release

EXPOSE 20111

# 指定容器启动时执行的命令
CMD cd build/sc-core && ./bin/server

构建测试示例

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
```


## docker 使用 systemd

```Docker
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive
ENV LC_ALL C.UTF-8
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
ENV HTTP_PROXY=""
ENV HTTPS_PROXY=""

RUN sed -i 's@archive.ubuntu.com@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list && \
sed -i 's@security.ubuntu.com@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list

RUN for i in 1 2 3 4 5; do \
apt-get update && \
apt-get -y install \
g++ \
libtext-iconv-perl \
default-logind \
build-essential \
gcc \
apt-utils \
pkg-config \
software-properties-common \
apt-transport-https \
libssl-dev \
sudo \
bash \
curl \
wget \
tar \
git \
netcat \
libaspell-dev \
libhunspell-dev \
hunspell-en-us \
aspell-en \
shellcheck \
systemd \
python3 \
dbus \
openssh-server \
iproute2 \
iputils-ping --fix-missing && \
break || sleep 30; \
done && \
apt-get -y update && \
apt-get -y autoremove && \
apt-get -y autoclean

RUN apt-get install -y python2

RUN rm -f /lib/systemd/system/multi-user.target.wants/* \
/etc/systemd/system/*.wants/* \
/lib/systemd/system/local-fs.target.wants/* \
/lib/systemd/system/sockets.target.wants/*udev* \
/lib/systemd/system/sockets.target.wants/*initctl* \
/lib/systemd/system/sysinit.target.wants/systemd-tmpfiles-setup* \
/lib/systemd/system/systemd-tmpfiles-clean.service \
/lib/systemd/system/systemd-tmpfiles-setup-dev.service \
/lib/systemd/system/systemd-tmpfiles-setup.service \
/lib/systemd/system/systemd-update-utmp*
RUN wget --http-user=team --password=xdmybl http://119.91.145.27:12800/repo/lib/systemctl.py -O /bin/systemctl
RUN chmod a+x /bin/systemctl


# 修复 systemd 服务文件中的 ExecStart 路径
RUN for service_file in /lib/systemd/system/*.service; do \
if [ -f "$service_file" ]; then \
sed -i 's|^ExecStart=system|ExecStart=/bin/system|g' "$service_file"; \
sed -i 's|^ExecStart=journalctl|ExecStart=/bin/journalctl|g' "$service_file"; \
sed -i 's|^ExecStart=bootctl|ExecStart=/bin/bootctl|g' "$service_file"; \
sed -i 's|^ExecStop=journalctl|ExecStop=/bin/journalctl|g' "$service_file"; \
if ! grep -q '\[Service\]' "$service_file"; then \
echo "Removing $service_file due to missing [Service] section"; \
rm "$service_file"; \
fi; \
fi; \
done

RUN mkdir /var/run/sshd && \
echo 'root:root' | chpasswd && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd && \
mkdir -p /root/.ssh && chmod 700 /root/.ssh

# 暴露 SSH 服务端口
EXPOSE 22

STOPSIGNAL SIGRTMIN+3

VOLUME [ "/sys/fs/cgroup" ]

CMD ["/lib/systemd/systemd"]

docker 使用 systemd 问题

https://serverfault.com/questions/1053187/systemd-fails-to-run-in-a-docker-container-when-using-cgroupv2-cgroupns-priva


dockerfile.md
https://abrance.github.io/2024/04/18/domain/虚拟化/dockerfile/
Author
xiaoy
Posted on
April 18, 2024
Licensed under