简体   繁体   English

如何使用 docker buildx bake 替换 docker-compose up?

[英]How to use docker buildx bake to replace docker-compose up?

So I have this docker command that works fine with other machines but since mine is an M1 chip, I am having issues.所以我有这个 docker 命令可以在其他机器上正常工作,但由于我的是 M1 芯片,所以我遇到了问题。 I would like to use buildx and bake to be able to run the docker containers for multiple architectures.我想使用buildxbake来为多种架构运行 docker 容器。 This is the command,这是命令,

PY_VER=xxxx IMAGE=xxxx DISTRO=xxxxx \
PHARUS_VERSION=$(cat xxxxx/xxxx/xxxxx | tail -1 | awk -F\' '{print $2}') \
DJLABBOOK_VERSION=$(cat package.json | grep \"version\" | awk -F\" '{print $4}') \
HOST_UID=$(id -u) \
docker-compose -f docker-compose-dev.yaml up

I have a Dockerfile and everything set up but this isn't working with my M1 machine.我有一个 Dockerfile 并且一切都设置好了,但这不适用于我的 M1 机器。

Whenever I run this command, docker buildx bake -f env.hcl app -f docker-bake.hcl I get this output.每当我运行这个命令时,docker buildx bake -f env.hcl app -f docker-bake.hcl 我得到这个 output。

 => [internal] load build definition from Dockerfile                                                                            0.1s
 => => transferring dockerfile: 32B                                                                                             0.1s
 => [internal] load .dockerignore                                                                                               0.0s
 => => transferring context: 34B                                                                                                0.0s
 => [internal] load metadata for docker.io/library/node:lts-buster-slim                                                         0.4s
 => [internal] load metadata for docker.io/library/golang:alpine3.11                                                            0.4s
 => [stage-1  1/11] FROM docker.io/library/node:lts-buster-slim@sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 0.0s
 => [internal] load build context                                                                                               0.0s
 => => transferring context: 4.47kB                                                                                             0.0s
 => [go_tmp 1/3] FROM docker.io/library/golang:alpine3.11@sha256:xxxxxxxxxxxxxxx  0.0s
 => https://raw.githubusercontent.com/datajoint/miniconda3-docker/master/utilities/startup.go                                   0.1s
 => CACHED [stage-1  2/11] RUN apt-get update && apt-get install wget -y                                                        0.0s
 => CACHED [stage-1  3/11] WORKDIR /home/node                                                                                   0.0s
 => CACHED [stage-1  4/11] COPY --chown=node:node ./package.json ./package-lock.json  /home/node/                               0.0s
 => CACHED [stage-1  5/11] RUN npm install                                                                                      0.0s
 => CACHED [go_tmp 2/3] ADD https://raw.githubusercontent.com/datajoint/miniconda3-docker/master/utilities/startup.go /startup  0.0s
 => CACHED [go_tmp 3/3] RUN cd / && go build startup.go                                                                         0.0s
 => CACHED [stage-1  6/11] COPY --from=go_tmp /startup /startup                                                                 0.0s
 => CACHED [stage-1  7/11] COPY ./docker-entrypoint.sh /docker-entrypoint.sh                                                    0.0s
 => CACHED [stage-1  8/11] RUN   chmod +x /docker-entrypoint.sh &&   chmod 4755 /startup                                        0.0s
 => CACHED [stage-1  9/11] COPY --chown=node:node ./tsconfig.json /home/node/                                                   0.0s
 => CACHED [stage-1 10/11] COPY --chown=node:node ./public  /home/node/public                                                   0.0s
 => CACHED [stage-1 11/11] COPY --chown=node:node ./src  /home/node/src                                                         0.0s
 => exporting to image                                                                                                          0.0s
 => => exporting layers                                                                                                         0.0s
 => => writing image sha256:xxxxxxxxxxxxxxxxx                                    0.0s

This is the contents of my env.hcl file这是我的 env.hcl 文件的内容

PY_VER="3.8"

IMAGE="djbase"

DISTRO="alpine"

PHARUS_VERSION="$(cat pharus/pharus/version.py | tail -1 | awk -F\" '{print $2}')"

DJLABBOOK_VERSION="$(cat package.json | grep \"version\" | awk -F\" '{print $4}')"

HOST_UID="$(id -u)"

and the contents of my docker-bake.hcl以及我的 docker-bake.hcl 的内容

variable "PY_VER" {
    default = "xxx"
}

variable "IMAGE" {
    default = "xxx"
}

variable "DISTRO" {
    default = "xxxxx"
}

variable "PHARUS_VERSION" {
    default = "$(cat xxxx/xxxx/xxxxn.py | tail -1 | awk -F\" '{print $2}')"
}

variable "DJLABBOOK_VERSION" {
    default = "$(cat package.json | grep \"version\" | awk -F\" '{print $4}')"
}

variable "HOST_UID" {
    default = "$(id -u)"
}

target "app" {
    args = {
        PY_VER = "${PY_VER}"
        HOST_UID = "${HOST_UID}"
    }
}

buildx bake isn't meant to replace compose up , it's for building docker images. buildx bake并不是要替换compose up ,而是用于构建 docker 图像。 So it's more comparable to docker compose build or just docker build .所以它更类似于docker compose build或只是docker build Where you want to use buildx is in order to build images for multiple architectures.您想使用 buildx 的地方是为了为多个架构构建映像。

The problem you're having with your M1 chip is likely that your docker image isn't meant to run on that architecture ( linux/arm64 ) because it's probably been built for x86 ( linux/amd64 ).您在使用 M1 芯片时遇到的问题可能是您的 docker 映像不打算在该架构( linux/arm64 )上运行,因为它可能是为 x86 ( linux/amd64 )构建的。

Make sure you have Rosetta 2 installed, and your system should be able to emulate x86 and run your images anyway.确保您已安装 Rosetta 2,并且您的系统应该能够模拟 x86 并运行您的图像。 Check the docs here for more .此处查看文档以获取更多信息

Running emulated images under docker is slow though.在 docker 下运行模拟图像虽然很慢。 So what I've been doing is using bulidx to build images for multiple architectures then you can pull those images with docker composer.所以我一直在做的是使用 bulidx 为多种架构构建图像,然后您可以使用 docker 作曲家提取这些图像。

# docker-bake.hcl
variable "platforms" {
  default = ["linux/amd64", "linux/arm64"]
}

group "default" {
  targets = [
    "my_image",
  ]
}

target "my_image" {
  dockerfile = "myimage.Dockerfile"
  tags = ["myrepo/myimage:latest"]
  platforms = platforms
}
# Command
docker build bake --push
# docker-compose.yml
version: '3.9'
services:
    dev:
        image: myrepo/myimage:latest

Now you'll have a docker image pushed for both architectures and docker will automatically select the correct one depending on the system that it's running on.现在,您将为两种架构推送一个 docker 映像,并且 docker 将自动 select 正确的映像,具体取决于它运行的系统。

You'll probably have to configure buildx, check the docker multi-arch docs for more.您可能需要配置 buildx,查看docker 多架构文档了解更多信息。

  1. Build your images by using buildx and bake .使用buildxbake构建您的图像。
  2. Put --load argument or --push respectively following your case.--load参数或--push分别放在您的案例之后。

If you use single architecture, just docker-compose down and docker-compose up --force-recreate or whatever command you can use the latest built image.如果您使用单一架构,只需docker-compose downdocker-compose up --force-recreate或任何您可以使用最新构建图像的命令。

In the case of multi-platforms, you must pull the docker image from the remote repository and do compose down & up.在多平台的情况下,您必须从远程存储库中拉取 docker 映像并进行向下和向上组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM