This is first part we will be “dockerizing” a simple node application and pushing the image to docker hub.
App structure will be simple and look like this.
We will run a simple node server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// server.js const http = require('http') const port = 3000 const requestHandler = (request, response) => { console.log(request.url) response.end('Hello Node.js Server!') } const server = http.createServer(requestHandler) server.listen(port, (err) => { if (err) { return console.log('something bad happened', err) } console.log(`server is listening on ${port}`) }) |
Dockerfile will have the following instructions to build our image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Dockerfile FROM node:latest # Create app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY server.js /usr/src/app # Bundle app source COPY . /usr/src/app EXPOSE 3000 CMD ["node", "server.js"] |
Now to build our image using the instructions in our Dockerfile. In our terminal window we will run this command.
1 2 3 4 |
docker build -t aldomatic/nodedemo . // [1] -t stands for tag, this is where we will name our image aldomatic/nodedemo // [2] . Tells the docker build to look in the current directory |
If everything checks out after running docker build then if you run docker images, you should see your newly created image.
Now to push our newly created image to https://hub.docker.com/. You will need to create a repository and name it according to docker hub specs.
Here is a link to the repo on docker hub.
https://hub.docker.com/r/aldomatic/nodedemo/