docker build/run与内网穿透[frpc/npc]实战

1,359 阅读5分钟

一、docker build镜像

dockerfile 指令

RUN
	用于指定docker build过程中要运行的命令,而不是docker run此dockerfile构建成的镜像时运行;

dockerfile CMD指令引入变量的影响测试

# 以下测试 通过docker run -e 传递变量的测试
CMD [ "./apps/frp/frpc -c" "${CONF_FILE}"]
sh: missing ]
/apps/frp/frpc.ini

CMD /apps/npc/npc -c $CONF_FILE
flag provided but not defined: -c

CMD [" sh"" -c"" /apps/npc/npc -c $CONF_FILE "]
/bin/sh: [ sh, -c, /apps/npc/npc -c /apps/npc/conf/npc.conf ]: not found

CMD [" /apps/npc/npc -c $CONF_FILE "]
Start container npc333 failed: {"message":"OCI runtime create failed: container_linux.go:367: starting container process caused: exec: \" /apps/npc/npc -c $CONF_FILE \": stat /apps/npc/npc -c $CONF_FILE : no such file or directory: unknown"}.

CMD [" /apps/npc/npc -c ${CONF_FILE} "]
Start container npc444 failed: {"message":"OCI runtime create failed: container_linux.go:367: starting container process caused: exec: \" /apps/npc/npc -c ${CONF_FILE} \": stat /apps/npc/npc -c ${CONF_FILE} : no such file or directory: unknown"}.

CMD [" /apps/npc/npc -c " " ${CONF_FILE} "]
/bin/sh: [ /apps/npc/npc -c : not found

CMD [" /apps/npc/npc "  " -c " " ${CONF_FILE} "]
/bin/sh: [ /apps/npc/npc : not found

2.1、npc打包镜像

root@hh:/volume1/development/source# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
97518928ae5f: Pull complete
Digest: sha256:635f0aa53d99017b38d1a0aa5b2082f7812b03e3cdb299103fe77b5c8a07f1d2
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
root@hh:/volume1/server/source/npc_0.26.10# docker run -it npc_0.26.10:v1.6
/bin/sh: [ /apps/npc/npc : not found

root@hh:/volume1/server/source/npc_0.26.10# docker run -it npc_0.26.10:v1.6 /bin/bash
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
root@hh:/volume1/server/source/npc_0.26.10# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc_0.26.10:v1.6
/bin/sh: [ /apps/npc/npc : not found

CMD [" /apps/npc/npc "  " -c "  ${CONF_FILE} ]
root@hh:/volume1/server/source/npc_0.26.10# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc_0.26.10:v1.7
/bin/sh: [ /apps/npc/npc : not found

CMD echo $CONF_FILE
root@hh:/volume1/server/source/npc_0.26.10# docker tag npc_0.26.10:v1.8 192.168.50.204:33080/mynas/npc_0.26.10:v1.8
root@hh:/volume1/server/source/npc_0.26.10# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc_0.26.10:v1.8
/apps/npc/npc.conf
root@hh:/volume1/server/source/npc_0.26.10# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.confsadfasdfasdf npc_0.26.10:v1.8
/apps/npc/npc.confsadfasdfasdf

CMD echo $CONF_FILE \
	&& pwd \
	&& ls -alhi /apps/npc
root@hh:/volume1/server/source/npc_0.26.10# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.confsadfasdfasdf npc_0.26.10:v1.8.1
/apps/npc/npc.confsadfasdfasdf
/apps/npc
total 12M
   2019 drwxr-xr-x    1 root     root          50 Nov 19 15:16 .
   2018 drwxr-xr-x    1 root     root           6 Nov 19 10:13 ..
   2022 ----------    1 root     root         760 Nov 19 15:16 Dockerfile
   2023 d---------    1 root     root          52 Nov 19 10:56 conf
   2026 -rw-r-xr-x    1 root     root       11.5M Apr  8  2021 npc
 235093 -rwx------    1 1026     users        278 Nov 17 02:07 npc.conf

#CMD echo $CONF_FILE \
#   && pwd \
#   && ls -alhi /apps/npc \
#   && ./npc -h
 root@hh:/volume1/server/source# docker build -t npc_0.26.10:v1.8.2 -f npc_0.26.10.dockfile .
 root@hh:/volume1/server/source# docker tag npc_0.26.10:v1.8.2 192.168.50.204:33080/mynas/npc_0.26.10:v1.8.2
 root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.confsadfasdfasdf npc_0.26.10:v1.8.2
/apps/npc/npc.confsadfasdfasdf
/apps/npc
total 12M
   2019 drwxr-xr-x    1 root     root          30 Nov 19 15:24 .
   2018 drwxr-xr-x    1 root     root           6 Nov 19 10:13 ..
   2022 d---------    1 root     root          52 Nov 19 10:56 conf
   2025 -rw-r-xr-x    1 root     root       11.5M Apr  8  2021 npc
 235093 -rwx------    1 1026     users        278 Nov 17 02:07 npc.conf
Usage of ./npc:
  -config string
    	Configuration file path

- docker build范例

root@hh:/volume1/server/source# docker build -t npc_0.26.10:v1.8.4 -f npc_0.26.10.dockfile .
CMD [" ./npc "  " -config "  ${CONF_FILE} ]
root@hh:/volume1/server/source# docker build -t npc_0.26.10:v1.8.3 -f npc_0.26.10.dockfile .
root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.confsadfasdfasdf npc_0.26.10:v1.8.3
/bin/sh: [ ./npc : not found

CMD ./npc -config ${CONF_FILE}
root@hh:/volume1/server/source# docker build -t npc_0.26.10:v1.8.4 -f npc_0.26.10.dockfile .

root@hh:/volume1/server/source# docker tag npc_0.26.10:v1.8.4 192.168.50.204:33080/mynas/npc_0.26.10:v1.8.4
root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.confsadfasdfasdf npc_0.26.10:v1.8.4
2021/11/19 15:29:18.410 [I] [npc.go:231]  the version of client is 0.26.10, the core version of client is 0.26.0
2021/11/19 15:29:18.410 [E] [control.go:94]  Config file /apps/npc/npc.confsadfasdfasdf loading error open /apps/npc/npc.confsadfasdfasdf: no such file or directory

root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc-0.25.3:v1.1
2021/11/19 15:42:35.181 [I] [npc.go:173]  the version of client is 0.25.3, the core version of client is 0.25.0
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x916ef0]

goroutine 27 [running]:
github.com/cnlh/nps/client.StartFromFile(0x7ffef0bdff03, 0x12)
	/home/travis/gopath/src/github.com/cnlh/nps/client/control.go:94 +0x70
created by main.(*npc).run
	/home/travis/gopath/src/github.com/cnlh/nps/cmd/npc/npc.go:186 +0x405

root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc-0.24.0:v1.2
2021/11/19 15:47:24.927 [I] [npc.go:89]  the version of client is 0.24.0, the core version of client is 0.24.0
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x90fd10]

goroutine 1 [running]:
github.com/cnlh/nps/client.StartFromFile(0x7ffc4174cf03, 0x12)
	/home/ffdfgdfg/go/src/github.com/cnlh/nps/client/control.go:94 +0x70
main.main()
	/home/ffdfgdfg/go/src/github.com/cnlh/nps/cmd/npc/npc.go:100 +0x5cb
  • 正确dockerfile与docker run
root@hh:/volume1/server/source# cat npc-0.26.10.dockfile
FROM alpine:latest
LABEL maintainer="kk 2021.11.19"

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
	&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
	&& apk update \
	&& apk upgrade

RUN apk add --no-cache ca-certificates \
	&& apk add tzdata

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
	&& echo "Asia/Shanghai" > /etc/timezone

ENV TIME_ZONE Asia/Shanghai

ENV LANG=zh_CN.UTF-8
ENV LANGUAGE=zh_CN:zh

RUN mkdir -p /apps/npc
WORKDIR /apps/npc
COPY npc-0.26.10/ .

ENV CONF_FILE=$CONF_FILE
#CMD /apps/npc/npc -c $CONF_FILE
#CMD [" sh"," -c"," /apps/npc/npc -c $CONF_FILE "]
CMD ./npc -config ${CONF_FILE}
#CMD echo $CONF_FILE \
#	&& pwd \
#	&& ls -alhi /apps/npc \
#	&& ./npc -h

root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc-0.26.10:v2.1
2021/11/19 15:51:21.398 [I] [npc.go:231]  the version of client is 0.26.10, the core version of client is 0.26.0
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x84aeff5]

goroutine 17 [running]:
ehang.io/nps/client.StartFromFile(0xffff3f03, 0x12)
	/home/runner/work/nps/nps/client/control.go:94 +0x55
created by main.run
	/home/runner/work/nps/nps/cmd/npc/npc.go:244 +0x326	

2.2.1、frpc[客户端]打包镜像

root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# ./frpc
2021/11/18 23:10:08 [W] [service.go:104] login to server failed: stream closed
stream closed
root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# ./frpc -h
frpc is the client of frp (https://github.com/fatedier/frp)

Usage:
  frpc [flags]
  frpc [command]

Available Commands:
  help        Help about any command
  http        Run frpc with a single http proxy
  https       Run frpc with a single https proxy
  reload      Hot-Reload frpc configuration
  status      Overview of all proxies status
  stcp        Run frpc with a single stcp proxy
  sudp        Run frpc with a single sudp proxy
  tcp         Run frpc with a single tcp proxy
  tcpmux      Run frpc with a single tcpmux proxy
  udp         Run frpc with a single udp proxy
  verify      Verify that the configures is valid
  xtcp        Run frpc with a single xtcp proxy

Flags:
  -c, --config string   config file of frpc (default "./frpc.ini")
  -h, --help            help for frpc
  -v, --version         version of frpc

Use "frpc [command] --help" for more information about a command.

- 正确dockerfile

root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# vim Dockerfile
FROM alpine:latest
LABEL maintainer="kk 2021.11.18"

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
	&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
	&& apk update \
	&& apk upgrade

RUN apk add --no-cache ca-certificates \
	&& apk add tzdata

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
	&& echo "Asia/Shanghai" > /etc/timezone

ENV TIME_ZONE Asia/Shanghai

ENV LANG=zh_CN.UTF-8
ENV LANGUAGE=zh_CN:zh

RUN mkdir -p /apps/frp
WORKDIR /apps/frp
COPY . .

ENV CONF_FILE=$CONF_FILE
CMD /apps/frp/frpc -c $CONF_FILE

root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# docker build -t frp_0.38.0:v1.6 .
Sending build context to Docker daemon  10.24MB
Step 1/13 : FROM alpine:latest
 ---> 0a97eee8041e
Step 2/13 : LABEL maintainer="kk 2021.11.18"
 ---> Using cache
 ---> 322bfbe39e84
Step 3/13 : RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories 	&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories 	&& apk update 	&& apk upgrade
 ---> Using cache
 ---> 527c56314eee
Step 4/13 : RUN apk add --no-cache ca-certificates 	&& apk add tzdata
 ---> Using cache
 ---> 4e248d9b6bf2
Step 5/13 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 	&& echo "Asia/Shanghai" > /etc/timezone
 ---> Using cache
 ---> 4d615af4827b
Step 6/13 : ENV TIME_ZONE Asia/Shanghai
 ---> Using cache
 ---> 90ebf4244a79
Step 7/13 : ENV LANG=zh_CN.UTF-8
 ---> Using cache
 ---> 8b82101e6970
Step 8/13 : ENV LANGUAGE=zh_CN:zh
 ---> Using cache
 ---> d192ff1f6695
Step 9/13 : RUN mkdir -p /apps/frp
 ---> Using cache
 ---> 911a6e40af85
Step 10/13 : WORKDIR /apps/frp
 ---> Using cache
 ---> 62a7ea1c2f95
Step 11/13 : COPY . .
 ---> c678eac2d271
Step 12/13 : ENV CONF_FILE=$CONF_FILE
 ---> Running in 8bbc3f597611
Removing intermediate container 8bbc3f597611
 ---> 8d3b91053253
Step 13/13 : CMD /apps/frp/frpc -c $CONF_FILE
 ---> Running in 1e755622b5df
Removing intermediate container 1e755622b5df
 ---> f0d826ea85ba
Successfully built f0d826ea85ba
Successfully tagged frp_0.38.0:v1.6
root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# docker tag frp_0.38.0:v1.6 192.168.50.204:33080/mynas/frp_0.38.0:v1.6
root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# docker push 192.168.50.204:33080/mynas/frp_0.38.0:v1.6
The push refers to repository [192.168.50.204:33080/mynas/frp_0.38.0]
f9b34b2dcc87: Pushed
6e31eb7015d7: Layer already exists
a4509b6a36e3: Layer already exists
afe7d22f53a0: Layer already exists
e97f4b94949b: Layer already exists
1a058d5342cc: Layer already exists
v1.6: digest: sha256:82a37a1edf9f081468d1d7b1958de18af64f382015a8af85f4be034c2e90b5a5 size: 1574

root@hh:/volume1/server/source/frp_0.38.0/frp_0.38.0_linux_amd64# docker images | grep frp
192.168.50.204:33080/mynas/frp_0.38.0        v1.6              f0d826ea85ba   7 minutes ago   20.5MB
frp_0.38.0                                   v1.6              f0d826ea85ba   7 minutes ago   20.5MB
snowdreamtech/frpc                           latest            b17a99788fd8   12 months ago   15.5MB
stilleshan/frpc                              latest            58ad4842dc1f   12 months ago   14.3MB
leonismoe/frpc                               latest            bcd7047004f0   12 months ago   10.8MB
cloverzrg/frpc-docker                        latest            4ed388df9a32   12 months ago   18.5MB
oldiy/frpc                                   0.26.0            3d2d08293b5f   2 years ago     14.9MB
registry.cn-hangzhou.aliyuncs.com/kk06/frp   v1.0_0.21.0       a2cc69b783b6   3 years ago     75.2MB

2.2.2、frps[服务端]部署

[root@VM-4-6-centos data]# mkdir -p /data/source
[root@VM-4-6-centos data]# mv /root/frp_0.38.0_linux_amd64.tar.gz source/
[root@VM-4-6-centos source]# tar zxvf frp_0.38.0_linux_amd64.tar.gz

[root@VM-4-6-centos frp_0.38.0_linux_amd64]# cp frps /usr/bin
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# mkdir -p /etc/frp
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# cp frps.ini /etc/frp
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# cp systemd/frps.service /usr/lib/systemd/system/

[root@VM-4-6-centos frp_0.38.0_linux_amd64]# systemctl start frps
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# ps -ef | grep frps
nobody   24988     1  2 00:03 ?        00:00:00 /usr/bin/frps -c /etc/frp/frps.ini
root     24993 23535  0 00:03 pts/1    00:00:00 grep --color=auto frps
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# systemctl status fps
Unit fps.service could not be found.

# 无视报错

[root@VM-4-6-centos frp_0.38.0_linux_amd64]# mkdir -p /data/logs/
[root@VM-4-6-centos frp_0.38.0_linux_amd64]# cat /usr/lib/systemd/system/frps.service

[root@VM-4-6-centos frp_0.38.0_linux_amd64]# vim /etc/frp/frps.ini


[root@VM-4-6-centos frp_0.38.0_linux_amd64]# vim /usr/lib/systemd/system/frps.service
#User=nobody

systemctl daemon-reload

[root@VM-4-6-centos frp_0.38.0_linux_amd64]# systemctl restart frps

二、docker run

docker run 指令

docker run
	-d 后台、-i 交互、-t tty、-e 指定环境变量、-v 挂载、
	-it 一般同时时使用 ,并加 /bin/bash。如
		docker run -it nginx:latest /bin/bash
	-p 端口 宿主机端口:容器端口
	$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

- docker run范例

root@hh:/volume1/server/source# docker run -it -v /volume1/server/docker/npc/npc.conf:/apps/npc/npc.conf -e CONF_FILE=/apps/npc/npc.conf npc-0.26.10:v2.1