Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the Context to other dir? #365

Closed
HiColinn opened this issue May 17, 2021 · 9 comments
Closed

How to change the Context to other dir? #365

HiColinn opened this issue May 17, 2021 · 9 comments

Comments

@HiColinn
Copy link

The default context is . ,it's the root dir,but if my Dockerfile is in ./alpine/Dockerfile,and my root dir include multiple Dockerfile,I get the docker image by git msg.

@crazy-max
Copy link
Member

@HiColinn I'm not sure I understand. Do you have a link to your repo?

@HiColinn
Copy link
Author

@HiColinn I'm not sure I understand. Do you have a link to your repo?

Hi,thanks for you replay.I am a chinese,so my english is not good. I will try to tell you what I want.
I use a git repo to mange multiple docker images,I user one directory to manage all docker images,and when I push code to the github,I will sign the image name(directory) to the git commit message.The build.yml will get the infomation from
the git commit message ,like if need build new image,or push to DockerHub. This way is effective on action V1,but when I update the build.yml to V2,It doesn't work.
I found the reason is It can't get the true Context, so I want to know how to specify the context to the specified folder?
下载

name: Build Docker Image
on:
  push:
    branches:
        - master
jobs:
    build:
        # 命名工作
        name: Build docker image
        # 指定工作环境
        runs-on: ubuntu-latest
        if: "!contains(github.event.head_commit.message, '[skip ci]')"
        steps:
            -   name: Filter image name
                #获取提交日志中的变量并设置到env中
                run: |
                  ...
            # 显示变量值,方便调试
            -   name: Show var
                run: |
                    echo IMAGE_NAME=${{ env.IMAGE_NAME }}
                    echo IMAGE_RNAME=$IMAGE_RNAME
                    echo TAG_NAMES=$TAG_NAMES
            # 设置QEMU(多平台构建的时候需要)
            -   name: Set up QEMU
                uses: docker/setup-qemu-action@v1
            # 设置Buildx
            -   name: Set up Docker Buildx
                id: buildx
                uses: docker/setup-buildx-action@v1
            # 登录Dockerhub
            -   name: Login to DockerHub
                uses: docker/login-action@v1 
                with:
                    username: ${{ secrets.DOCKER_USERNAME }}
                    password: ${{ secrets.DOCKER_PASSWORD }}
            # 构建镜像并推送到Docker Hub
            -   name: Build and push
                if: ${{ env.IMAGE_NAME != '' }}
                id: docker_build
                uses: docker/build-push-action@v2
                with:
                    builder: ${{ steps.buildx.outputs.name }}
                    context: ./${{ env.IMAGE_NAME }}
                    file: ./${{ env.IMAGE_NAME }}/Dockerfile
                    target: prod
                    push: true
                    tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAMES }}
            # 显示构建的镜像的摘要
            -   name: Image digest
                run: echo "镜像摘要:"${{ steps.docker_build.outputs.digest }}      
            # 推送指定镜像的README到dockerhub
            -   name: Push Docker Hub Description
                # 当IMAGE_RNAME不为空的时候执行此任务 
                if: ${{ env.IMAGE_RNAME != ''}}
                uses: peter-evans/dockerhub-description@v2
                with:
                    username: ${{ secrets.DOCKERHUB_USERNAME }}
                    password: ${{ secrets.DOCKERHUB_PASSWORD }}
                    repository: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_RNAME }}
                    readme-filepath: ${{ env.IMAGE_RNAME }}/README.md

@crazy-max
Copy link
Member

Duplicate #346

@HiColinn
Copy link
Author

Duplicate #346

I know It's duplicate,but it's long time bother me.can you help me? thank you very much.

@CremaLuca
Copy link

CremaLuca commented May 21, 2021

I've been having kind of the same problem: I am trying to build multiple images in one repository and when I define the context to be the path to the folder the build-push-action raises an error unable to prepare context: path "./app" not found.

So the structure of my project is like

├───docker-compose.yml
├───app
│   └───Dockerfile
└───web
     └───Dockerfile

In the docker-compose file I am able to build both images

app:
    build: ./app
    image: user/name-app:latest
web:
    build: ./web
    image: user/name-web:latest

So after following the guide I setup the Github Actions script using two builders

 steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        uses: docker/setup-buildx-action@v1
        id: builder1
      -
        uses: docker/setup-buildx-action@v1
        id: builder2
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push 1
        id: docker_build1
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.builder1.outputs.name }}
          context: ./app
          file: ./app/Dockerfile
          push: true
          tags: user/name-app:latest
      -
        name: Build and push 2
        id: docker_build2
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.builder2.outputs.name }}
          context: ./web
          file: ./web/Dockerfile
          push: true
          tags: user/name-web:latest

But as soon as it reaches the first Docker build and push it raises the error error: unable to prepare context: path "./app" not found after executing the command /usr/bin/docker buildx build --tag ***/name-app:latest --iidfile /tmp/docker-build-push-uPhN6t/iidfile --builder builder-afa6a99d-5fe7-42b3-b1a3-22e34e20b4ab --push ./app.

When running the command used by the build and push step docker buildx build --tag user/name-app:latest --push ./app locally in the main folder the build works correctly and produces an image.

Removing context does help because the Dockerfiles are found and partially build but fail because they use files contained in their respective folder which could not be found, unless I were to edit them and specify that the CWD is the root folder and files are found under ./app or ./web.

The repository I'm working on is private so I won't' be able to share it entirely.

@CremaLuca
Copy link

CremaLuca commented May 21, 2021

Solved: adding

- name: Checkout
  uses: actions/checkout@v2

At the beginning of the Github actions file

@HiColinn
Copy link
Author

Solved: adding

- name: Checkout
  uses: actions/checkout@v2

At the beginning of the Github actions file

Can you share your file ,I add the check out ,it is also did not work

@daffodilistic
Copy link

Solved: adding

- name: Checkout
  uses: actions/checkout@v2

At the beginning of the Github actions file

Can you share your file ,I add the check out ,it is also did not work

@HiColinn I have a sample Actions markup file in my repository here: daffodilistic/docker-odoo@95352c3

@CremaLuca
Copy link

Solved: adding

- name: Checkout
  uses: actions/checkout@v2

At the beginning of the Github actions file

Can you share your file ,I add the check out ,it is also did not work

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - 
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        uses: docker/setup-buildx-action@v1
        id: builder1
      -
        uses: docker/setup-buildx-action@v1
        id: builder2
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push 1
        id: docker_build1
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.builder1.outputs.name }}
          context: ./app
          file: ./app/Dockerfile
          push: true
          tags: user/name-app:latest
      -
        name: Build and push 2
        id: docker_build2
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.builder2.outputs.name }}
          context: ./web
          file: ./web/Dockerfile
          push: true
          tags: user/name-web:latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants