These are generic npm scripts that you can copy & paste into your package.json
file as-is and get access to convinience scripts to manage your Docker images all in one place.
Looking for npm scripts for AWS ECS? Go here!
Watch the video: Do More With Less JavaScript
docker pull duluca/minimal-nginx-web-server
- Documentation: https://hub.docker.com/r/duluca/minimal-nginx-web-server/
docker pull duluca/minimal-node-web-server
- Documentation: https://hub.docker.com/r/duluca/minimal-node-web-server/
- Cross-Platform: Works on Windows 10 and macOS.
docker:build
: Builds your Docker image, using the rootDockerfile
and tags the image aslatest
and whatever version is specificed inpackage.json
like1.0.0
.docker:run
: Run the image you built on your local Docker instance. When you rundocker ps
your image will identified by theimageName
you specify inpackage.json
.docker:debug
: Builds and runs image; tails console logs, so you can see what's happening inside the container; launche target app URLhttp://localhost:imagePort
in a browser; all in one command.docker:publish
: Publishes the image to theimageRepo
specified. This can be on Docker Hub, AWS ECS or any other Docker repository you may create.
- Install Docker for Mac or Windows
npm i --save-dev cross-conf-env npm-run-all
: Needed to ensure cross platform functionality for scripts.
In your package.json
file add a new config property with three sub-properties using your own values, as shown below:
"config": {
"imageRepo": "[namespace]/[repository]",
"imageName": "custom_app_name",
"imagePort": "0000"
},
Copy & paste these new scripts under the scripts
property in package.json
:
Note that
docker:runHelper
assumes that your code is listening to port 3000. Update the value in the scripts, if this is not the case.
"scripts": {
"predocker:build": "npm run build",
"docker:build": "cross-conf-env docker image build . -t $npm_package_config_imageRepo:$npm_package_version",
"postdocker:build": "npm run docker:tag",
"docker:tag": " cross-conf-env docker image tag $npm_package_config_imageRepo:$npm_package_version $npm_package_config_imageRepo:latest",
"docker:run": "run-s -c docker:clean docker:runHelper",
"docker:runHelper": "cross-conf-env docker run -e NODE_ENV=local --name $npm_package_config_imageName -d -p $npm_package_config_imagePort:3000 $npm_package_config_imageRepo",
"predocker:publish": "echo Attention! Ensure `docker login` is correct.",
"docker:publish": "cross-conf-env docker image push $npm_package_config_imageRepo:$npm_package_version",
"postdocker:publish": "cross-conf-env docker image push $npm_package_config_imageRepo:latest",
"docker:clean": "cross-conf-env docker rm -f $npm_package_config_imageName",
"predocker:taillogs": "echo Web Server Logs:",
"docker:taillogs": "cross-conf-env docker logs -f $npm_package_config_imageName",
"docker:open:win": "echo Trying to launch on Windows && timeout 2 && start http://localhost:%npm_package_config_imagePort%",
"docker:open:mac": "echo Trying to launch on MacOS && sleep 2 && URL=http://localhost:$npm_package_config_imagePort && open $URL",
"docker:debugmessage": "echo Docker Debug Completed Successfully! Hit Ctrl+C to terminate log tailing.",
"predocker:debug": "run-s docker:build docker:run",
"docker:debug": "run-s -cs docker:open:win docker:open:mac docker:debugmessage docker:taillogs"
},
You can customize the build command to run your tests before building the image like:
"predocker:build": "npm run build -- --prod && npm test",
You're done. Now run your scripts. To build and publish an image you only need to use two of the commands frequently.
0. npm run docker:build
: Builds and Tags the image. After first run, you can just use npm run docker:debug
.
npm run docker:debug
: Test (optional), Build, Tag, Run, Tail and launch your app in a browser to test.npm run docker:publish
: Voila, you results are published on the repository you've defined.
You've two options, easy and hard.
- Easy: Use now.sh
After you install the cli tools and login. You'll need to run the following command on the same folder as your
Dockerfile
:
$ now
That's it!
- Hard: Use AWS ECS This is Amazon's Elastic Container Service and it's pretty excellent to use with Docker. However, it is complicated to setup. But worry not for step-by-step instructions head over to npm scripts for AWS ECS.