Giter VIP home page Giter VIP logo

docker-starter's Introduction

docker app lifecycle

使用 go-zero 创建一个简单的 api 程序

go mode init docker-starter
goctl api new app
go mod tidy

生成 Dockerfile

cd app
goctl docker --port 8888

基于生成的 dockerfile 进行调整

FROM golang:alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /build

ADD go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod,sharing=locked \
    go mod download
COPY . .
# 构建 go app, 并将构建结果,写到容器 build 目录下
RUN go build -ldflags="-s -w" -o ./docker-starter ./app

# 构建配置文件 & 复制到容器内
FROM bhgedigital/envsubst as config
ARG ENVFILE=preview.env
WORKDIR /build
COPY ./app/etc/templates/ ./etc
RUN export $(cat ./etc/${ENVFILE} | xargs) && \
    envsubst < ./etc/config.tmpl >> ./etc/app-api.yaml

# 将应用跑在 alpine 上
FROM alpine:3.16

WORKDIR /app
COPY --from=config /build/etc/app-api.yaml ./etc/app-api.yaml
COPY --from=builder /build/docker-starter ./
RUN chmod +x ./docker-starter

EXPOSE 8888
ENTRYPOINT ["/app/docker-starter"]

创建 Makefile

BUILD_DEST_DIR ?= build

docker-build:
	docker buildx build -f ./app/Dockerfile -t docker-starter:001 . 

build-app:
	go build -ldflags="-s -w" -o ./{BUILD_DEST_DIR}/docker-starter ./app

.PHONY: docker-build build-app

构建 docker 镜像

make docker-build

docekr images 即可看到生成的镜像

运行 docker

docker run -itd --name docker-starter -p 8888:8888 docker-starter:001

# 查看当前运行的容器
docker container ls -a

调用 app 接口

curl http://localhost:8888/for/you
> {"message":"you"}

查看容器内 app 打印的日志

# 获取容器 id
docker container ls -a

# 查看容器日志
docker logs <container id>

查看容器内文件

docker exec -it <container id> ls -al

改动容器内容

# alpine 中的 shell 叫 ash
docker exec it <container id> ash 

# 进入容器内部, 修改配置
vi ./etc/app-api.yaml

# 保存后退出容器,查看变更,会列出容器内文件的变更
docker diff <container id>

docker-starter's People

Watchers

xiawan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.