Coder Social home page Coder Social logo

smetal1 / docker-express Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chrispauley/docker-express

0.0 2.0 0.0 5 KB

This is a hello-world app for an expressjs server inside a docker image. Created from egghead.io tutorial.

Dockerfile 18.60% JavaScript 81.40%

docker-express's Introduction

docker-express

This is a bare minimum Expressjs server app that is wrapped in a Docker image.

Installation

Install Docker

Node JS is not required on your development machine, that is part of this image.

Create the image

First create your site.

Here we will use index.js to server 'hello world' text message from our express server.

var http = require('http');

var server = http.createServer(function (request, response){
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World from expressjs in a Docker container.");
});

server.listen(8000);

console.log("Server running at http://127.0.0.1:8000/");

Create the Dockerfile

The contents of the Dockerfile build script will reference

# Use the base image created and maintained by mhart
# It has a bare minimum installation the server and the libraries needed for an expressjs server.
FROM mhart/alpine-node
COPY index.js .
EXPOSE 8000
CMD node index.js

Create and run a container from the image. This will build a container with the tag "express-helloworld" and the dot indicates the Dockerfile location.

docker build -t express-helloworld .

View the image on your machine

docker images

Run and test your image in a container

The -ti for "terminal interactive" allows you to use ^C to kill the process. The -p 8000:8000 options indicate the exposedPort:internalPort accessiblility. The express-helloworld option indicates the image name to create a container.

docker run -ti -p 8000:8000 express-helloworld

Switch to your browser and get: http://127.0.0.1:8000/

Clean up

Kill the running process

docker kill express-helloworld

List container instances to be removed

docker ps -a

Remove container instances docker rm

docker rm ea0ea

Remove images

docker rmi express-helloworld
docker rmi mhart/alpine-node

Bonus

Add to your github repo

git init
git add .
## create your github repo
git remote add origin https://github.com/chrispauley/docker-express.git
git push origin master

Deploy to a Docker Host

Login to your server host. At a terminal window

git clone https://github.com/chrispauley/docker-express.git
cd docker-express
docker build -t express-helloworld .
docker run -ti -p 8000:8000 express-helloworld

Make sure your server host port 8000 is accessible and test it: http://192.168.0.84:8000/ Update a cname record on your domain and your up.

Clean Up

Remember to clean up your containers and images!

Deploy to Docker Hub

First tag your image with your hub.docker.com user account name:

docker tag a234b052333a chrispauley/express-helloworld
docker images

Next, login and push to Docker hub

docker push chrispauley/express-helloworld

Now you can deploy using a docker pull command on a docker host server. Switch to the docker host machine.

docker run -ti -p 8000:8000 chrispauley/express-helloworld

Test it from your browser and enjoy.

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.