Docker 与 BitBucket

Posted

技术标签:

【中文标题】Docker 与 BitBucket【英文标题】:Docker with BitBucket 【发布时间】:2019-09-27 15:50:22 【问题描述】:

我正在尝试使用 docker + bitbucket 管道进行自动发布;不幸的是,我有一个问题。我阅读了 Docker Hub 上的管道部署说明,并创建了以下模板:

# This is a sample build configuration for Docker.
# Check our guides at https://confluence.atlassian.com/x/O1toN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: atlassian/default-image:2

pipelines:
  default:
    - step:
        services:
          - docker
        script: # Modify the commands below to build your repository.
          # Set $DOCKER_HUB_USERNAME and $DOCKER_HUB_PASSWORD as environment variables in repository settings
          - export IMAGE_NAME=paweltest/tester:$BITBUCKET_COMMIT

          # build the Docker image (this will use the Dockerfile in the root of the repo)
          - docker build -t paweltest/tester .
          # authenticate with the Docker Hub registry
          - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
          # push the new Docker image to the Docker registry
          - docker push paweltest/tester:tagname

我已经完成了数据,但是在做push之后,构建开始时出现如下错误:

无法准备上下文:lstat/opt/atlassian/pipelines/agent/build/Dockerfile:没有干文件或目录

我想实现什么?将更改发布到存储库后,我希望自动构建映像并将其发送到 Docker 中心,最好是发送到应用程序所在的目标服务器。

我已经寻找解决方案并尝试了不同的组合。目前,我有大约 200 个提交失败状态并且没有进一步的想法。

【问题讨论】:

【参考方案1】:

Bitbucket 管道是一项 CI/CD 服务,您可以构建应用程序并将资源部署到生产或测试服务器实例。您也可以构建和部署 docker 映像 - 除非您做错了什么,否则这应该不是问题...

bitbucket-pipelines.yml 文件中所有定义的脚本都在从指示的图像创建的容器中运行(在您的情况下为atlassian/default-image:2

你应该在项目中有Dockerfile,你可以从这个文件中构建和发布一个docker镜像。

我创建了没有 Dockerfile 的简单存储库并开始构建:

无法准备上下文:无法评估 Dockerfile 中的符号链接 路径:lstat /opt/atlassian/pipelines/agent/build/Dockerfile:没有这样的 文件或目录

我需要在我的项目中使用Dockerfile 来构建映像(与bitbucket-pipelines.yml 文件处于同一级别):

FROM node:latest

WORKDIR /src/
EXPOSE 4000

在下一步中,我创建了一个公共 DockerHub 存储库:

我还更改了您的bitbucket-pipelines.yml 文件(您忘记用标签标记新图像):

image: atlassian/default-image:2

pipelines:
  default:
    - step:
        services:
          - docker
        script: 
          # build the Docker image (this will use the Dockerfile in the root of the repo)
          - docker build -t appngpl/***-question-56065689 .
          # add new image tag
          - docker tag appngpl/***-question-56065689 appngpl/***-question-56065689:$BITBUCKET_COMMIT
          # authenticate with the Docker Hub registry
          - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
          # push the new Docker image to the Docker registry
          - docker push appngpl/***-question-56065689:$BITBUCKET_COMMIT

结果:

一切正常 :)

Bitbucket 存储库:https://bitbucket.org/krzysztof-raciniewski/***-question-56065689

GitHub 镜像仓库:https://hub.docker.com/r/appngpl/***-question-56065689

【讨论】:

一切都很好,但现在的问题是,是否可以将这样的图像推送到 docker 所在的服务器?也就是说,在构建之后它会自动运行,例如,到 dockerhub 或 Web 服务器。因为这就是我的意思,我在 Docker 的 VPS 服务器上,bitbucket 构建了我的图片,现在我希望他将工作的内容发送到 docker hub,并让 web 服务器在那里更新图像并启动新版本的应用程序。 是的,这是可能的。您需要拉取最新的镜像版本,停止运行容器,移除旧容器并启动新容器。有关此操作的更多信息,您可以在此处阅读:***.com/questions/26734402/…。您可以使用带有-t 参数的SSH 从bitbucket 管道运行所有这些命令(更多信息:garron.me/en/go2linux/ssh-sudo-run-commands-remote-server.html)。请记住,您需要将公共 RSA 密钥添加到 repo 配置,以允许从 Bitbucket Pipelines Service 连接到您的 VPS 服务器。祝你好运:)

以上是关于Docker 与 BitBucket的主要内容,如果未能解决你的问题,请参考以下文章

docker与docker-compose介绍,对比与使用

docker介绍与应用

docker介绍与应用

Docker 容器网络与 Docker-in-Docker

Docker简介与安装

Docker入门与实践之 docker安装与了解