
Lab 002 - Create a basic container using Dockerfile¶
- In this lab we will create our first container using
Dockerfile - The container will be used to serve a simple
NodeJsweb server - No NodeJs knowledge is required
- You will need to create, build, tag & push your container to DockerHub
-
The lab is divided into the several tasks.
01. Prepare the server code¶
- Our container will include the following NodeJs simple web server
- Copy the code below and save it to a file named
server.js// // Filename: server.js // // Simple NodeJs Server // The server is listening by default to port 8888 // // import the HTTP module var http = require('http'); // Define a port we want to listen to // Later on we will pass the port as env parameter // Default port is set to 8888 const PORT= process.env.PORT || 8888; // Create the server and listen for requests // Create the server and listen for requests http.createServer((request, response)=>{ response.end('Server is running.!! You asked for: ' + request.url); }).listen(PORT, ()=>{ // Callback is triggered when server is getting a request console.log("Server listening on: http://localhost:%s", PORT); });
02. Test the server.js code¶
- Before we “pack” our code in Docker image lets test the code
- We will test the code inside
nodejsdocker# Test the node code # --rm = remove the container when done # -d = run in detached mode # -p = open the required ports # -v = volume # -w = workdir # --name = the container name # node = Execute a nodejs container to test our code # node server.js = Execute the code docker run --rm -d \ -v $(pwd):/usr/src \ -w /usr/src \ -p 8888:8888 \ --name node_server \ node \ node server.js
03. Write the Dockerfile¶
- Now lets create a
Dockerfilewith the code we just created above - The
Dockerfilewill be based uponnodejsimage and will include ourserver.js# # Filename: Dockerfile # # Use node as our base image FROM node # Optional: Set working directory WORKDIR /usr/src # Copy the server code to our working directory [.] COPY server.js . # Mark the port which will required for the server EXPOSE 8888 # Start the server when the container is started CMD ["node", "./server.js"]
04 - Build the image¶
- Once we have the docker file we can build the image
- Once the image is ready we will push it to DockerHub so you will need an account.
- We will name the image: “Your Dockerhub username/repository:version”
05. Login to DockerHub¶
- Login to DockerHub
- Execute
docker loginand enter your Docker Hub credentials when prompted - If you don’t have a DockerHub account, create one at https://hub.docker.com/signup
- You will need to push the image to DockerHub in the next step
06. - Push the image to DockerHub¶
Docker Login Required
You must login to Docker Hub before you can push to DockerHub
- Example:
docker push username/image:tag
07. Verify the push¶
- Login to your DockerHub account and verify that the image exists under your DockerHub account.
08 - Test the image¶
- Last step is to test our image
- To do so we will pull and run the image from DockerHub
- Once the container is started we will test the server
09. Test the server¶
-
Test the server that he is running on docker.
10. Clean up¶
- Stop and remove the container
