

Basic Docker Hands-On Lab¶
- In this lab we will create a simple NodeJs server and run it inside a docker container.
- Next, we will create a second container which will print some text to the screen.
- We will also create a docker hub account and push our images to the cloud.
Table of Contents¶
- 1. Verify Docker installation
- 2. Building our NodeJs server
- 3. Test our server code
- 3.1. Test if the server is running
- 3.2. Stop the server
- 4. Creating Docker containers
- 4.1. Create
Dockerfile - 5. Build the Docker image
- 5.1. Verifying that the container was created
- 5.2. Testing the image
- 5.3 Test if the server is running and listening:
- 6. Container with arguments
1. Verify Docker installation¶
- Run the following command in the shell to verify that Docker is installed and running
2. Building our NodeJs server¶
- In this section we will create a simple NodeJs server and later on we will run it inside a docker container.
- First, we will create a folder for our server code and then we will create the server code file.
- Create our server code file (copy the code into this file)
server.js
// import the HTTP module
var http = require('http');
// Define a port we want to listen to
const PORT=8080;
// We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
// Create a server
var server = http.createServer(handleRequest);
// Start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
3. Test our server code¶
- You can run the server code using NodeJs by executing the following command.
- This will run the server and listen to port 8080
3.1. Test if the server is running¶
- If you are using Cloud Shell, you can test it by clicking on the most left icon (web preview) and open port 8080 which is our server port
- If you are running it locally, you can test it by opening your browser
- Use the following URL in your browser
http://localhost:8080 - You should see the following message in your browser or terminal
3.2. Stop the server¶
- Type CTRL+C twice to stop the server
4. Creating Docker containers¶
4.1. Create Dockerfile¶
- In the same folder create the create a file called
Dockerfileand add the following content:# Base image for Node.js FROM node:latest # Set the working directory in the container WORKDIR /usr/src/app # Copy the server file into the container # (Don`t forget the dot at the end) COPY server.js . # Expose port 8080 # This is the port our server will listen to EXPOSE 8080 # Start the Node.js server CMD [ "node", "server.js" ] - This Dockerfile does the following:
- Uses the latest Node.js image as the base image
- Sets the working directory inside the container to
/usr/src/app - Copies the
server.jsfile from the current directory into the container’s working directory - Exposes port 8080 so that it can be accessed from outside the container
- Specifies the command to run when the container starts, which is
node server.js
[!NOTE] Make sure that the
Dockerfileis in the same directory as theserver.jsfile.[!IMPORTANT] The
COPYcommand in the Dockerfile is used to copy files from the host machine into the container. The first argument is the source file (in this case,server.js), and the second argument is the destination path inside the container (.means the current working directory in the container).
5. Build the Docker image¶
-
In this step we will build the Docker image using the
Dockerfilewe created in the previous step.[!IMPORTANT] Make sure you are in the same directory where the
Dockerfileandserver.jsfiles are located. -
Build the image using docker build with the following parameters
5.1. Verifying that the container was created¶
- Let’s verify that the image was created successfully by listing all the images on our system
- Run the following command to list all Docker images:
- You should see an output similar to this:
5.2. Testing the image¶
- Now we can run the container using the image we just created
- Execute
docker runcommand with the following flags:
| Option | Description |
|---|---|
-d | Run container in background (daemon mode) |
-p | Map internal port 8080 to external port 8080 |
hello-node:v1 | The name of the image we just created |
5.3 Test if the server is running and listening:¶
- If you are using Cloud Shell, you can test it by clicking on the most left icon (web preview) and open port
8080which is our server port - If you are running it locally, you can test it by opening your browser
- Use the following URL in your browser
http://localhost:8080 - You should see the following message in your browser or terminal
6. Container with arguments¶
- Let’s test another container which will print content to screen
[!WARNING]
You might get the following error:docker: [DEPRECATION NOTICE] Docker Image Format v1 and Docker Image manifest version 2, schema 1 support is disabled by default and will be removed in an upcoming release.
- Run the following command to run a container with arguments
- You should see the following output in your terminal
