Prerequisits
We need some things installed on the local machine.- node.js installed
- npm installed
- docker installed
Install node.js sampleapp
If you already have a node.js to pack you can leave this stepWe want to use a simple express app with a hello world text, to demonstrate that the webapp is running on our container
Install an express and express-generator
$ npm install -g express express-generator
$ npm install
Create an express project
$ express sampleAppThis will generate our skeleton app in the current folder.
Create our DockerFile
With a DockerFile we are able to describe what we want to install on the container in a easy readable configfile. If you want to see the documentation, what commands the DockerFile can execute, here is the documentationNow we want to to following things automated by the dockerfile. This can be managed by the following dockerfile
Save following content in a file "Dockerfile" in your project-skeletons root
FROM node:latest MAINTAINER Peter Böthig ENV NODE_ENV=production ENV PORT=3000 COPY . /var/www WORKDIR /var/www RUN npm install EXPOSE $PORT ENTRYPOINT ["npm", "start"]
To explain that
- Line 1 load a node baseimage
- Line 2 define a maintainer (your username in thinat case)
- Line 3 & 4 define enviromentvariables, to switch from dev to prod
- Line 5 copy our sourcecode to the container in "var/www"
- LINE 6 define a workingdirectory
- Line 7 run npm install
- Line 8 Expose the default webserverport (prod/live)
- Line 9 start the express webserver
Create our image from the Dockerfile
Now, that we have created our Dockerfile we can create our first image from it.Open a terminal in the folder where your Dockerfile lives
docker build -f Dockerfile -t <yourdockerhub-username>/node-expressThis will create your image
You can see you new image with
docker images
Create a new container from your image
Now, that we have created our image from the dockerfile we can create a new container and run our sample appOpen a terminal in the folder where your Dockerfile lives
docker run -d -p 8081:3000 <yourdockerhub-username>/node-express
Now you can reach your app under http://localhost:8081
Keine Kommentare:
Kommentar veröffentlichen