Skip to content

Instantly share code, notes, and snippets.

@chendrix
Created June 21, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chendrix/97325a7dc2a9e6531adf14b3be38e895 to your computer and use it in GitHub Desktop.
Save chendrix/97325a7dc2a9e6531adf14b3be38e895 to your computer and use it in GitHub Desktop.
How to use docker-image-resource to build custom docker images used as inputs for later tasks
jobs:
- name: build-and-test
plan:
- task: build-image
privileged: true
config:
platform: linux
image_resource:
type: docker-image
source:
repository: concourse/docker-image-resource
outputs:
- name: image-output
params:
DESTINATION: image-output
WORKSPACE: /tmp/scratch
run:
path: /bin/bash
args:
- -c
- |
set -eux
## Need this, but depends on internals of docker-image-resource
## maybe can be replaced with your own scripts to start_docker
source /opt/resource/common.sh
start_docker "[]" ""
## This should be done via inputs
mkdir -p "${WORKSPACE}/git-master"
cat << EOF > "${WORKSPACE}/git-master/package.json"
{
"name": "node-js-sample",
"version": "0.2.0",
"description": "A sample Node.js app using Express 4",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.13.3"
},
"engines": {
"node": "4.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-sample"
},
"keywords": [
"node",
"heroku",
"express"
],
"author": "Mark Pundsack",
"contributors": [
"Zeke Sikelianos <zeke@sikelianos.com> (http://zeke.sikelianos.com)"
],
"license": "MIT"
}
EOF
cat << EOF > "${WORKSPACE}/Dockerfile"
FROM node:8.1.2-alpine
ADD git-master /tmp/git-master
RUN (cd /tmp/git-master && npm install)
RUN mv /tmp/git-master/node_modules .
EOF
## The meat of what you are doing
docker build -t test "${WORKSPACE}"
docker run \
--cidfile=/tmp/container.cid \
-v /opt/resource/print-metadata:/tmp/print-metadata \
--entrypoint /tmp/print-metadata \
test > "${DESTINATION}/metadata.json"
mkdir -p "${DESTINATION}/rootfs/"
docker export $(cat /tmp/container.cid) | tar --exclude="dev/*" -xf - -C "${DESTINATION}/rootfs/"
- task: check-if-it-worked
image: image-output
config:
platform: linux
run:
path: echo
args:
- /tmp/git-master/package.json
@neezer
Copy link

neezer commented Jun 29, 2017

Hmm, NPM doesn't seem to work in the exported image:

          docker run \
              --cidfile=/tmp/container.cid \
              -v /opt/resource/print-metadata:/tmp/print-metadata \
              --entrypoint /tmp/print-metadata  \
              test > "${DESTINATION}/metadata.json"
+
+         # Works when same image is run directly
+         docker run test echo "NPM Version is "
+         docker run test npm --version
              
          mkdir -p "${DESTINATION}/rootfs/"
          
          docker export $(cat /tmp/container.cid) | tar --exclude="dev/*" -xf - -C "${DESTINATION}/rootfs/"
          
  - task: check-if-it-worked
    image: image-output
    config:
      platform: linux
      run: 
+        # running with output of `docker export` fails, see screenshot below
-        path: echo
+        path: npm
        args:
-        - /tmp/git-master/package.json
+        - --version

Testing npm --version with the image directly:

ss

Testing npm --version with the exported image:

ss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment