docker配置文件输出helloworld

lucky-di / 2023-07-21 / 原文

1.在root下创建dockerfile

sudo -i
touch Dockerfile
vim Dockerfile

2.编写helloworld.go

pakage main

import "fmt"

func main() {
    fmt.Println("Hello, World! ")
}

 

3.配置Dockerfile


FROM ubuntu:22.04 AS v1
ENV MYNAME="WANGYIDI"
WORKDIR /home

COPY ./helloworld.go .

RUN apt update
RUN apt install -y golang
RUN go mod init wangyidi
RUN go mod tidy
RUN go build -o helloworld .

# 将当前目录下的文件复制到镜像中的/home目录下
COPY . /home


# 编译并运行各种语言的helloworld程序
# 如果你有多个源文件,可以根据需要进行调整


# 指定镜像的入口命令




FROM ubuntu:22.04
COPY --from=v1 /home/helloworld .
CMD ["./helloworld"]


这里配置了go的依赖和环境,并且,为了减小容器大小,使用了二层镜像

docker build -t harbor.student.das.com/library/jingxiangname:20230719(生成镜像)

docker run --rm -it harbor.student.das.com/library/jingxiangname:20230719(运行go文件)

docker tag wanagyi-go docker run --rm -it harbor.student.das.com/library/jingxiangname:20230719(改tag)