Git Product home page Git Product logo

docker-urlarchiver's Introduction

Use Docker to package a Node.js script with all its dependencies, including Node.

This repo serves double-duty as:

  1. An example of using Docker to package a Node.js script with all dependencies, including Node. I hope both that this is useful to someone and that I hear some suggestions for improvement from those more experienced with Docker.

  2. If you happen to need to save a URL's response body to Amazon S3, a useful script that can be executed with Docker or directly with Node.

What is Docker?

If you're unfamiliar with Docker, please read The Whole Story if you have a few minutes. In short, it is a powerful new way to think about deploying software that decouples an application and its dependencies from the various types of environments where it may be deployed.

Example files

  • app.js is a single-purpose Node.js script that takes its configuration from environment variables and saves the response from a configurable URL to a configurable place on Amazon S3. It knows nothing about Docker.

  • Dockerfile allows you to bundle app.js with all Node.js dependencies and OS dependencies, including Node itself! With Docker, you can run this image even on a machine that doesn't have Node installed. The Dockerfile doesn't care what's happening inside app.js, so it serves as a basic example of using Docker to create an image that contains the desired version of Node to be used to execute a script.

Configuration

In case you are interested in the example logic that saves a URL's response to Amazon S3, the following environment variables are used for configuration:

  • AWS_ACCESS_KEY Your IAM access key, for saving content to your S3 account. Familiarize yourself with the IAM Best Practices to limit your exposure when using these credentials.

  • AWS_SECRET_KEY The secret associated with the above key

  • S3_BUCKET The target S3 bucket

  • S3_OBJECT The target filename

  • TARGET_URL The URL from which to download the response that will be saved in the above S3 location

  • SUCCESS_URL An optional URL to GET when the script completes successfully. This is great if you use a service like Dead Man's Snitch to alert you when your scheduled tasks stop working.

Usage

Run using Docker

As the primary use case, this is how you use Docker to run the example script. Note that the only dependency for this is Docker itself.

# Pull the preconfigured image to speed things up the first time you run
$ docker pull adamalex/urlarchiver

# Execute the example script within a Docker container
$ docker run \
  -e "AWS_ACCESS_KEY=..." \
  -e "AWS_SECRET_KEY=..." \
  -e "S3_BUCKET=..." \
  -e "S3_OBJECT=..." \
  -e "TARGET_URL=..." \
  -e "SUCCESS_URL=..." \
  adamalex/urlarchiver

Build a custom Docker image

Should you want to customize the Dockerfile or the script itself, or just feel safer using your own Docker image, you can follow the below instructions to build your own image.

# Build the image
$ docker build -t="yourname/imagename" .

# Run the image. The environment variables will of course be different
# if you have replaced the example script with your own logic.

$ docker run \
  -e "AWS_ACCESS_KEY=..." \
  -e "AWS_SECRET_KEY=..." \
  -e "S3_BUCKET=..." \
  -e "S3_OBJECT=..." \
  -e "TARGET_URL=..." \
  -e "SUCCESS_URL=..." \
  yourname/imagename

Run using Node.js

Although this example logic is not the primary focus of this repository, this is how you would run the script outside of Docker by using Node.js directly.

$ npm install
$ export AWS_ACCESS_KEY="..."
$ export AWS_SECRET_KEY="..."
$ export S3_BUCKET="..."
$ export S3_OBJECT="..."
$ export TARGET_URL="..."
$ export SUCCESS_URL="..."
$ node app.js

Next Steps

docker-urlarchiver's People

Contributors

adamalex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

docker-urlarchiver's Issues

Lots of things seems wrong to me in this Dockerfile

You ask on twitter, so I open an issue to discuss :) .

I'm not an expert but I think you use lots of bad practices.

NVM

You install a multi-version manager and use it only to install one NodeJS, it's odd.
NVM is a developpement tool, a production environnement should not have this kind of things.
It's cleaner to just directly install the nodeJS version that you need.

NPM Dependencies

It's not a problem in this case but I think resolving dependencies outside the Dockerfile is a bad idea.
Libraries can have to compile things and rely on OS files, so your build works only when host and container use the same OS.
It's mean that someone with a different OS may not be able to rebuild your container from this repo.

Bash as entrypoint

Why using Bash as entrypoint?
Why using the l flag?
This image hasn't other purpose that running this Node app, you should run it directly.


Anyway, good job, it's nice to have more examples of Docker uses in the wild :) .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.