2

The reason why I'm asking is because we're using AWS codebuild & I need to do DB migrations. If a DB migration breaks, I want to cancel the codebuild & rollback the migration which was just made. I've got this part working, all I need to do now is cancel the dockerbuild mid way. How can I do this?

This is my .sh file with the knex migration commands:

#!/bin/bash
echo "running"
function mytest {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        knex migrate:rollback 
      echo "Rolling back knex migrate $1" >&2
      exit
    fi
    return $status
}

mytest knex migrate:latest 

Running exit will not cancel/break the docker build.

My Dockerfile (just incase):

FROM node:6.2.0

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app
RUN chmod +x /usr/src/app/migrate.sh
RUN /usr/src/app/migrate.sh


EXPOSE 8080

CMD npm run build && npm start
James111
  • 15,378
  • 15
  • 78
  • 121

1 Answers1

3

Running exit will not cancel/break the docker build.

running exit 1 should

Docker should respond to the error codes returned by the RUN shell scripts in said Dockerfile.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Great! I looked everywhere and couldn't find anything. I was close though – James111 Jan 05 '17 at 05:51
  • @James111 yes, you have a similar case (for an opposite effect) in http://stackoverflow.com/a/30717108/6309 – VonC Jan 05 '17 at 05:53
  • Good to know :) If this isn't documented somewhere on the docker page, I think it should. I can see it coming in handy for a multitude of uses :D – James111 Jan 05 '17 at 05:58