简体   繁体   中英

Docker Hub Automated Builds - How to access cloned repository content?

I have set up automatic builds on github repository commits and docker hub starts to build on every repository update but within the Dockerfile I can not access any of the source files in my repo. Even if I add RUN ls -R / to see the build logs, I can verify that none of my source files are pulled in that machine. So how do I access my repository content from the Dockerfile script?

First step is to configure BUILD RULES in docker hub.

Build Context is the location of directory you want to have access in cloned repository. If you need full source content you can leave it / .

Dockerfile location indicates path from Build Context . If it is in the root directory of source repo. It shall be just Dockerfile , if it is inside any directory (for example /app) it shall be app/Dockerfile

For example, to access full repository content at any given build stage, you shall COPY cloned content into the current build stage

FROM ubuntu:20.04 AS base
WORKDIR /src
COPY . .
RUN ls

Line 1 - initialize a build stage and gives name 'base'

Line 2 - sets current build stage working directory as /src, so everything will havven to this location

Line 2 - copies all files and directories from the cloed repo root direcotry to the current build stage's working directory. The Build Context takes action here as you have access to repo content from this build context, if you left it as / you will have full repo copied in the /src directory.

Line 3 - runs ls command just to check if root repo directory content is available.

Keep in mind that since you have copied source code into the /src directory it is accessible only from the current build stage. If you have initialized another build stage by adding another FROM some-image as final line like this, here you have no direct access to the /src directory. In order to access files from one build stage to other build stage, you need to know the name of build stage and the line to access the content will look like this:

FROM some-image as final
WORKDIR /finalFiles
COPY --from=base /app/someFile .
RUN ls

Here we have copied someFile from the soure repo's root folder (if build context in docker hub was specified as / ) into the /finalFiles directory.

If you are using GitHub Actions you need to have this small line

steps:
  - uses: actions/checkout@v2

before you can access the code. See this minimal example:

on:
  push:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Run tests
        run: |
          docker build .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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