是否可以在docker hub中的自动构建中添加环境变量? [英] Is it possible to add environment variables in automated builds in docker hub?

查看:176
本文介绍了是否可以在docker hub中的自动构建中添加环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动化构建过程,并且需要传递一个环境变量以运行 Dockerfile 中的某些命令。我想知道Dockerhub是否有任何方法可以做到这一点。我知道docker cloud有这样的东西,但是我想知道Dockerhub中是否存在功能,因为cli中的-build-args 参数用于正常构建。

解决方案

设置自动构建


Docker Hub https://hub.docker.com )可以自动从源代码构建图像一个外部存储库,并自动将构建的映像推送到您的Docker存储库,该存储库将托管在您的Docker Hub存储库帐户 例如: https://cloud.docker.com/u/binbash/repository/list


设置自动构建(也称为 autobuilds )时,将创建分支要在Docker映像中构建的标签。当您将代码推送到列出的那些图像标签之一的源代码分支(当前仅支持 GitHub / Bitbucket )时,推送将使用 webhook 触发一个新的构建,该构建生成一个Docker映像。然后将构建的映像推送到Docker Hub注册表。
有关详细的实现步骤,请参考 https://docs.docker.com/docker -hub / builds /


构建的环境变量


您可以设置环境变量的值 (实际上,它们被映射为构建ARG值- docker build --build-arg -仅在构建时使用- https://docs.docker.com/engine/reference/commandline/ build /#set-build-time-variables --- build-arg )。


不要与您的服务在以下位置使用的环境值ENV VARS混淆运行时( docker run --env MYVAR1 = foo - https://docs.docker.com/v17.12/edge/engine/reference/commandline / run /#set-environment-variables--e-env-env-file


从Docker Hub UI配置的此环境变量在以下情况下会在您的构建过程中使用您配置一个自动生成。通过单击构建环境变量部分旁边的加号,然后输入变量名称和值,添加您的构建环境变量。 p>

通过Docker Hub UI设置变量值时,您可以在挂钩文件中设置的命令使用变量值(这非常重要 >,并将在下面进行扩展),但是将它们存储起来,以便只有对Docker Hub存储库具有管理员访问权限的用户才能看到其值。这意味着您可以使用它们安全地存储访问令牌或其他应保密的信息。


构建钩子示例(以实现Docker Hub UI Env vars)


在自动构建的网络用户界面中添加变量,即可在钩子中使用它们。在挂钩中,您必须使用该值通过-build-arg 设置自定义生成arg 。最后,您必须在 Dockerfile 中使用此自定义生成arg,以使用 ENV 命令或 export


示例:


说您想要一个环境变量 TERRAFORM_VERSION ='0.12.0-beta2'在您的构建环境中


第1步。
将其添加到自动用于构建构建环境变量的构建的Web UI
[![在此处输入图片描述] [1]] [1]


第2步。
创建一个自定义构建挂钩,即在与 Dockerfile 相同的目录中创建一个名为 hook 文件夹。在hooks文件夹中,创建一个名为 build 的文件。这将创建自定义构建挂钩。 Docker将使用它来构建您的映像。构建内容:

 #!/ bin / bash 

docker build -t $ IMAGE_NAME --build-arg TERRAFORM_VERSION = $ TERRAFORM_VERSION。

注意:在这里 $ TERRAFORM_VERSION


Step3:
在您的Dockerfile中

  ARG TERRAFORM_VERSION 
ENV TERRAFORM_VERSION $ TERRAFORM_VERSION

注意:这里 $ TERRAFORM_VERSION 来自bash脚本文件中名为 build 的自定义生成args。


完整示例: https: //github.com/binbashar/public-docker-images/tree/master/terraform-resources


就是这样!现在应该可以工作了。可能在Docker Hub X中将'构建环境变量'重命名为'自定义钩子环境变量'将简化官方文档( https://docs.docker.com/docker-hub/builds/advanced/ )。


加分!


启动构建脚本时会设置许多关键环境参数,您可以在钩子中使用所有这些参数,这些参数在创建自定义build-args时很有用。

  SOURCE_BRANCH:当前正在测试的分支或标签的名称。 
SOURCE_COMMIT:正在测试的提交的SHA1哈希。
COMMIT_MSG:来自正在测试和构建的提交的消息。
DOCKER_REPO:正在构建的Docker存储库的名称。
DOCKERFILE_PATH:当前正在构建的dockerfile。
DOCKER_TAG:正在构建的Docker存储库标签。
IMAGE_NAME:正在构建的Docker存储库的名称和标记。 (此变量是DOCKER_REPO:DOCKER_TAG的组合。)


I want to automate my build process and need to pass an environment variable to run some of the commands in the Dockerfile. I was wondering if there was any way to do this in Dockerhub. I know docker cloud has something like this, but I was wondering whether the functionality was there in Dockerhub since there is the --build-args argument in the cli for normal building.

解决方案

Set up Automated builds

Docker Hub (https://hub.docker.com) can automatically build images from source code in an external repository and automatically push the built image to your Docker repositories which will be hosted under your Docker Hub repositories account Eg: https://cloud.docker.com/u/binbash/repository/list

When you set up automated builds (also called autobuilds), you create a list of branches and tags that you want to build into Docker images. When you push code to a source code branch (currently only GitHub / Bitbucket are supported) for one of those listed image tags, the push uses a webhook to trigger a new build, which produces a Docker image. The built image is then pushed to the Docker Hub registry. For detailed implementation steps please refer to https://docs.docker.com/docker-hub/builds/

Environment variables for builds

You can set the values for environment variables (actually they are mapped to build ARG values - docker build --build-arg - to be exclusively used at build-time - https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg).

NOT to be confused with the environment values, ENV VARS, used by your service at runtime (docker run --env MYVAR1=foo - https://docs.docker.com/v17.12/edge/engine/reference/commandline/run/#set-environment-variables--e-env-env-file)

This Environment Variables configured from the Docker Hub UI are used in your build processes when you configure an automated build. Add your build environment variables by clicking the plus sign next to the Build environment variables section, and then entering a variable name and the value.

When you set variable values from the Docker Hub UI, they can be used by the commands you set in hooks files (THIS IS VERY IMPORTANT and will be extended below), but they are stored so that only users who have admin access to the Docker Hub repository can see their values. This means you can use them to safely store access tokens or other information that should remain secret.

Build hook examples (to implement Docker Hub UI Env vars)

Adding variables from the auto-build’s web UI makes them available inside the hooks. In the hook, you’ll have to use that value to set a custom build arg using --build-arg. Finally you have to use this custom build arg inside your Dockerfile to manually set an environment variable using ENV command or export.

Example:

Say your want an environment variable TERRAFORM_VERSION='0.12.0-beta2' in your build environment

Step 1. Add this in the auto-build’s web UI for ‘build environment variables’ [![enter image description here][1]][1]

Step 2. Create a custom build hook i.e create a folder called hooks in the same directory as your Dockerfile. Inside the hooks folder, create a file called build. This creates the custom build hook. Docker will use this to build your image. Contents of build:

#!/bin/bash

docker build -t $IMAGE_NAME --build-arg TERRAFORM_VERSION=$TERRAFORM_VERSION .

NOTE: Here $TERRAFORM_VERSION is coming from the web UI.

Step3: In your Dockerfile

ARG TERRAFORM_VERSION
ENV TERRAFORM_VERSION $TERRAFORM_VERSION

NOTE: Here $TERRAFORM_VERSION is coming from the custom build args in your bash script file named build.

Complete example: https://github.com/binbashar/public-docker-images/tree/master/terraform-resources

Thats it! It should work now. Probably renaming ‘build environment variables’ to ‘custom hook environment variables’ in Docker Hub Xwill ease the understanding of this concept in the official documentation (https://docs.docker.com/docker-hub/builds/advanced/).

Extra Points!

There are a number of key environment arguments set upon launching a build script, all of which you can use in your hooks and which can all be useful in making custom build-args.

SOURCE_BRANCH: the name of the branch or the tag that is currently being tested.
SOURCE_COMMIT: the SHA1 hash of the commit being tested.
COMMIT_MSG: the message from the commit being tested and built.
DOCKER_REPO: the name of the Docker repository being built.
DOCKERFILE_PATH: the dockerfile currently being built.
DOCKER_TAG: the Docker repository tag being built.
IMAGE_NAME: the name and tag of the Docker repository being built. (This variable is a combination of DOCKER_REPO:DOCKER_TAG.)

这篇关于是否可以在docker hub中的自动构建中添加环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆