├── docker-build ├── package.json ├── docker-run ├── docker-push └── README.md /docker-build: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -z "$VERSION" ]; then 4 | VERSION=$(node -e "console.log(require('./package.json').version)") 5 | fi 6 | 7 | if [ -z "$NAME" ]; then 8 | NAME=$(node -e "console.log(require('./package.json').name.split('/').pop())") 9 | fi 10 | 11 | echo "$VERSION" 12 | 13 | docker build -t "$NAME:$VERSION" "$@" . 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "docker-build-run-push", 3 | "version": "3.0.0", 4 | "description": "npm scripts to automate working with docker images.", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/ryanramage/docker-build-run-push.git" 8 | }, 9 | "keywords": [ 10 | "docker" 11 | ], 12 | "author": "Ryan Ramage", 13 | "license": "ISC", 14 | "bugs": { 15 | "url": "https://github.com/ryanramage/docker-build-run-push/issues" 16 | }, 17 | "homepage": "https://github.com/ryanramage/docker-build-run-push", 18 | "bin": { 19 | "docker-build": "./docker-build", 20 | "docker-push": "./docker-push", 21 | "docker-run": "./docker-run" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /docker-run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -z "$VERSION" ]; then 4 | VERSION=$(node -e "console.log(require('./package.json').version)") 5 | fi 6 | 7 | if [ -z "$NAME" ]; then 8 | NAME=$(node -e "console.log(require('./package.json').name.split('/').pop())") 9 | fi 10 | 11 | 12 | if [ "$#" -ne 0 ]; then 13 | PRE=$(echo "$@" | cut -d "+" -f 1) 14 | POST=$(echo "$@" | cut -d "+" -f 2) 15 | echo "$PRE and $POST" 16 | if [ "$POST" == "$PRE" ]; then 17 | echo "single version" 18 | echo docker run "$NAME:$VERSION" "$@" 19 | docker run "$NAME:$VERSION" "$@" 20 | 21 | else 22 | echo "split version" 23 | echo docker run "$PRE" "$NAME:$VERSION" "$POST" 24 | docker run $PRE "$NAME:$VERSION" $POST 25 | 26 | fi 27 | 28 | 29 | else 30 | docker run "$NAME:$VERSION" 31 | fi -------------------------------------------------------------------------------- /docker-push: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -z "$VERSION" ]; then 4 | VERSION=$(node -e "console.log(require('./package.json').version)") 5 | fi 6 | 7 | if [ -z "$NAME" ]; then 8 | NAME=$(node -e "console.log(require('./package.json').name.split('/').pop())") 9 | fi 10 | 11 | PREFIX=$(node -e "console.log(require('./package.json')['docker-registry'] || '')") 12 | if [ -z "$PREFIX" ]; then 13 | PREFIX=$(node -e "console.log(require('./package.json')['docker-user'] || '')") 14 | fi 15 | 16 | if [ -z "$PREFIX" ]; then 17 | echo "docker push requires either docker-registry or docker-user in package.json" 18 | exit 1 19 | fi 20 | 21 | echo "$VERSION" 22 | 23 | docker tag "$NAME:$VERSION" "$PREFIX/$NAME" 24 | docker push "$PREFIX/$NAME" 25 | docker tag "$NAME:$VERSION" "$PREFIX/$NAME:$VERSION" 26 | docker push "$PREFIX/$NAME:$VERSION" 27 | docker tag -f "$NAME:$VERSION" "$PREFIX/$NAME:latest" 28 | docker push "$PREFIX/$NAME:latest" 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | docker-build-run-push 2 | ===================== 3 | 4 | npm scripts to automate docker images. 5 | 6 | An easy way to excute the typical sequence of building, running and pushing a docker image out of a node project to a private docker repo, 7 | or the public docker hub. 8 | 9 | Uses the name and version number from package.json to keep your docker image name and version aligned. 10 | 11 | Usage 12 | ----- 13 | 14 | in your project, execute 15 | 16 | npm install docker-build-run-push --save 17 | 18 | If you are pushing to a private docker repo, add the following to your package.json 19 | 20 | "docker-registry": "docker.your-private.com", 21 | 22 | Or if you are pushing to the public docker hub, add the following to your package.json 23 | 24 | "docker-user": "ryanramage". 25 | 26 | Add some scripts that look like this. You might adjust to your liking 27 | 28 | "scripts": { 29 | "docker-build": "rm -rf node_modules && npm i --production && docker-build", 30 | "docker-run": "docker-run", 31 | "docker-push": "docker-push", 32 | }, 33 | 34 | Now in your project, when you are ready to make a new image, bump your version number in package.json. 35 | 36 | To build a local docker image quickly, for running later 37 | 38 | npm run docker-build 39 | 40 | Run the local docker image just built 41 | 42 | npm run docker-run 43 | 44 | Push the docker image to the docker-registry specified in your package.json 45 | 46 | npm run docker-push 47 | 48 | 49 | ## docker-run arguments 50 | 51 | It's common to have to pass arguments when running a docker. The '+' character to split the arguments for the docker command. 52 | 53 | Here are some example of what they mean 54 | 55 | 56 | Run a different command in the docker image then the CMD specified 57 | 58 | npm run docker-run — node ./bin/cli.js 59 | 60 | 61 | Pass a port and an env file as arguments. 62 | 63 | npm run docker-run -- -p 3000:3000 --env-file .env + 64 | 65 | Start an interactive terminal 66 | 67 | npm run docker-run -- -i -t + /bin/bash 68 | 69 | You can also simply this by adding to your npm script directly. Here is an example of setting enviroment variables for docker: 70 | 71 | "docker-run": "docker-run \"-e SPOT_TASKS=geocode,khatba,es-sync +\"", 72 | 73 | 74 | --------------------------------------------------------------------------------