docker_practice/image/buildx.md

149 lines
5.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 使 `Buildx`
**BuildKit** https://github.com/moby/buildkit 开源。
**使Docker Hub Docker 18.09BuildKit 使使 BuildKit 使 Dockerfile Dockerfile.buildkit**
Docker CE 19.03+ 使 `BuildKit` `Dockerfile` Docker
### `Dockerfile`
`BuildKit` 使使 `BuildKit` **** 使 `$ docker buildx build` Docker
#### `RUN --mount=type=cache`
使 `Go` `go mod``Node.js` `npm`
`npm`
```docker
FROM node:alpine as builder
WORKDIR /app
COPY package.json /app/
RUN npm i --registry=https://registry.npm.taobao.org \
&& rm -rf ~/.npm
COPY src /app/src
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /app/dist
```
使 `dist` `package.json` `RUN npm i && rm -rf ~/.npm`
**** `node_modules` `node_modules` `node_modules`
`BuildKit` `RUN --mount=type=cache`
```docker
FROM node:alpine as builder
WORKDIR /app
COPY package.json /app/
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
--mount=type=cache,target=/root/.npm,id=npm_cache \
npm i --registry=https://registry.npm.taobao.org
COPY src /app/src
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
# --mount=type=cache,target=/app/dist,id=my_app_dist,sharing=locked \
npm run build
FROM nginx:alpine
# COPY --from=builder /app/dist /app/dist
# 为了更直观的说明 from 和 source 指令,这里使用 RUN 指令
RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
# --mount=type=cache,target/tmp/dist,from=my_app_dist,sharing=locked \
mkdir -p /app/dist && cp -r /tmp/dist/* /app/dist
```
`RUN` `id` `my_app_npm_module` `/app/node_modules`
`RUN` `node_modules` `node_modules`
`RUN` `from` `builder` `source`
`Dockerfile` `--mount=type=cache,...`
|Option |Description|
|---------------------|-----------|
|`id` | `id` 便|
|`target` () | |
|`ro`,`readonly` | |
|`sharing` | `shared` `private` `locked` `sharing` 使 `BuildKit` 使 `id``shared` `private` 使使`locked` 使|
|`from` | |
|`source` | |
#### `RUN --mount=type=bind`
```docker
RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoint,target=/docker-php-entrypoint \
cat /docker-php-entrypoint
```
#### `RUN --mount=type=tmpfs`
`tmpfs`
```docker
RUN --mount=type=tmpfs,target=/temp \
mount | grep /temp
```
#### `RUN --mount=type=secret`
```docker
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
cat /root/.aws/credentials
```
```bash
$ docker buildx build -t test --secret id=aws,src=$HOME/.aws/credentials .
```
#### `RUN --mount=type=ssh`
`ssh`
```docker
FROM alpine
RUN apk add --no-cache openssh-client
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh ssh git@gitlab.com | tee /hello
```
```bash
$ eval $(ssh-agent)
$ ssh-add ~/.ssh/id_rsa
(Input your passphrase here)
$ docker buildx build -t test --ssh default=$SSH_AUTH_SOCK .
```
###
```bash
$ docker builder prune
```
###
* https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md