docker run的命令行参数将添加到exec形式的ENTRYPOINT的所有元素之后,并覆盖使用CMD指定的所有元素。这允许我们将参数传递给入口(entry point),即docker run -d将-d参数传递给入口(entry point)。我们可以使用docker run --entrypoint覆盖ENTRYPOINT指令。
Demon1:
main.go:
packagemainimport ("flag""fmt")typeUsagestruct { Name string Age string Address string Sex string}const ( name =`The name flag is used to set a new name.The default value is TonyYang.` age =`The age flag is used to set a new age.The default value is 31.` address =`The address flag is used to set a new address.The default value is ChangZhou.` sex =`The sex flag is used to set a new sex.The default value is TonyYang.`)var usage =Usage{ Name: name, Age: age, Address: address, Sex: sex,}funcmain() {var ( fName = flag.String("name", "TonyYang", usage.Name) fAge = flag.String("age", "31", usage.Age) fAddress = flag.String("address", "ChangZhou", usage.Address) fSex = flag.String("sex", "male", usage.Sex) ) flag.Parse() infos :=map[string]*string{"name": fName,"age": fAge,"address": fAddress,"sex": fSex, }for k, v :=range infos { fmt.Printf("The %s is %s\n", k, *v) }}
Dockerfile:
FROM golang:1.16 AS go
WORKDIR /my_executable/
COPY . .
RUN go build main.go && rm main.go
FROM alpine:latest
WORKDIR /tmp/executable/
COPY --from=go /my_executable/main .
ENTRYPOINT ["./main"]
CMD ["--name=Mae", "--age=30", "--address=ShangHai", "--sex=female"]
上面定义了一个测试ENTRYPOINT的Dockerfile,通过go语言编写。
$dockerruncamelgem/entrypoint_demo:v1
默认不为run指定运行的参数,输出结果如下所示:
The name is Mae
The age is 30
The address is ShangHai
The sex is female
#!/bin/sh# Note: I've written this using sh so it works in the busybox container too# USE the trap if you need to also do manual cleanup after the service is stopped,# or need to start multiple services in the one containertrap"echo TRAPed signal"HUPINTQUITTERM# start service in background here/usr/sbin/apachectlstartecho"[hit enter key to exit] or run 'docker stop <container>'"read# stop service and clean up hereecho"stopping apache"/usr/sbin/apachectlstopecho"exited $0"
如果我们通过docker run -it --rm -p 80:80 --name test apache,我们可以通过docker exec或docker top来查看容器的进程,然后让脚本停止Apache进程。