Run A NodeJs Application In a Docker Container
Tags: devops, docker, aws, container
Published: 2024-07-09 18:27:33


we will cover the following:
1.Installation of docker on ubuntu(AWS)
2.Containerisation of Nodejs application
3.update the application
4.share the application 

 Perequisites
- AWS Account with EC2 access

- Basic knowledge of Docker concepts and commands
**Steps to Setup Docker Project on Ubuntu (AWS EC2 Instance)**

- Create an Ubuntu EC2 Instance
Sign in to AWS Management Console
Go to the AWS Management Console and log in with your credentials.

- Launch an EC2 Instance
Navigate to EC2 Dashboard:
Click on Services and select EC2 under Compute.
Click Launch Instance.
Choose an Ubuntu Server AMI (e.g., Ubuntu Server 20.04 LTS).
Select an Instance Type (e.g., t2.micro).
Configure Instance Details and add storage as needed.(see image below)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/553r9vqhvbn5bn2qpi4d.png)

- Configure Security Group:
Add rules to open ports 22 (SSH), 80 (HTTP), and 3000 (Application port).
Review and Launch the instance, then SSH into the terminal.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c1pzjtb285y3n2mqa7a.png)
**2.Install Docker and Docker Compose**
Run this command to install the necessary tools and packages to handle secure software installations and manage additional software repositories effectively.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/216ndvmwvpkjjmk03z2r.png)
Add the Docker GPG key to your system's list of trusted keys using command below 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th2trh9j5mmfw8x9746v.png)
Add the Docker repository on your Ubuntu system which contains Docker packages including its dependencies, for that execute the below command:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjl12xjpptenkcjckhid.png)
Update the package index on your Ubuntu system and install Docker Community Edition suing commands below(Ps: To Check the Latest Version: Visit the Docker Compose Releases page and find the latest version)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hmu5rg6f6tid88rlp7m.png)
Run the following command to download the latest version of docker compose (ensure you have the latest: To Check the Latest Version: Visit https://github.com/docker/compose/releases)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olw4dujmngpcklby7cbh.png)
Set the correct permissions so that doxker-compose is executable
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sskhjto21yvbqaubzsby.png)
Run this command to verify sucessfule installation 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brc934884wffy2t2fmbq.png)
**3.Containerization of Node.js Application**
Start docker engine and check status 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6z4ont2915nqru70gcv.png)
Clone docker compose application:Cloning a Docker Compose application this is  important because it ensures consistency, ease of setup, and portability across different environments.Run the command below to clone the application
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3xe0q6tohz1mmedy9gv.png)
Run ls to see cloned files
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2402cufz94gn17mquen9.png)
To see content in directory run comand below 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tzqf46ym7q3v8l2nc08.png)
Build the app container image:To build the container image, you need to create a Dockerfile. A Dockerfile is a plain text file that contains a set of instructions which Docker uses to create a container image.
In the app directory of the cloned repository, where the package.json file is located, create a file named Dockerfile
Open the Dockerfile and paste the code provided below into it.
Save and close the Dockerfile.
Run the following command to create a Dockerfile 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjud5cesql0c9c5zt2vs.png)
Then, paste the following code into the Dockerfile
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hj3vwu39aevcn6mhm0rl.png)
Change directory to the app directory 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st8x63vy4yt6jkn9ecni.png)
Build the container image using the following command 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtr7522vftwu1x5x36gs.png)
Start application container by running the command below.This command ensures that the Docker container runs correctly, and the application is accessible on port 3000 of your host machine
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz8mi5isxzybszx4b5nb.png) 
After a few seconds, open your web browser and paste the following url http://18.169.10.56:3000/ replacing your-docker-server-ip with your Docker server's public IP to view the application
you can add one or two items as seen in the image below to ensure the application is working as expected 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njpuqaeisnw4nkuyeuao.png)

4. **Update The Application**
To modify the app.js file follow these steps 
Run the command to open the file in vi:sudo vi ~/getting-started/app/src//static/js/app.js-expected output, see image below 
-search for text "NO items yet! Add one above!"
press '/' to initiate a search and then type 'No' and press 'Enter' on your keyboard, the curson will move the first occureence of the text starting with "No" 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny9mxy517a8537qz1a6n.png)
Press 'a' to enter insert mode, allowing you to edit the text starting from the next character 
Change 'No items yet! Add one above! to you have no todo items yet! Add one above!
Press esc to exit insert mode, type :wq! and press enter to save chnages and close file 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv2k3neixl8xksezebtb.png)
Use the same docker build command you used before to create an updated version of the image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqe57f7r3xif5b0mjq3p.png)
After building start a new container 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq1wo5yv0rrtv46jrt4a.png)
The previous step failed because an existing container is already using the host’s port 3000. To resolve this issue, we need to stop and remove the old container before running the updated image:To do this run command below
Run sudo docker ps command to see the container ID of the running container 
Find the container ID from the output of the previous command and stop the container by running

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u18c4ntdrqivv8gw2h2t.png)
Remove the stopped container to free up the port

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vnwea05a9mm2197zihc.png)
Now that the old container is stopped and removed, run the updated image

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nta2np4fc7h02gj4lhjq.png)
After running the updated Docker container, you might notice that the data added previously has disappeared. This is because the data stored in the container does not persist when the container is removed.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9dsfyllkknbeb3jzhsq.png)

**5. Share  the Application** 
Go to Docker Hub and sign in with your Docker ID or create a new Docker ID if you don't have one
After signing in, click on your profile icon at the top right corner of the page and select Account Settings from the dropdown menu.
In the Account Settings page, click on Repositories in the left sidebar.
Click on the green Create Repository button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkgibzewe1rknfvc2dik.png)
Your repository is now created and ready to use. You will see the repository details and instructions for pushing your Docker images to this repository.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4p3y1xatmmap8exb06g.png)
Run the command below to login to Docker Hub, make you use you replace the username with your username . When prompted for password, type your Docker Hub password

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11v2sgxg7bwgpk6vqld7.png)
Push image to dockerhub using the command below and expected output 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zk0p66tj2pwc27675st7.png)
After pushing the image to docker hub pull image and run it as a container:Pulling Docker images and running them as containers ensures that your applications are portable, scalable, and secure. It simplifies deployment processes and enhances consistency across different environments, making Docker a powerful tool for modern software development and deployment practices

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqz8yom6675aax8mz0ql.png)

**SUMMARY**
This project involves setting up a Docker environment on an Ubuntu EC2 instance in AWS, containerizing a Node.js application, updating it, and sharing it on Docker Hub. The Node.js application runs in a Docker container, accessible via port 3000, and can be accessed and modified through commands and scripts.
Regarding data persistence, while the Docker container provides a portable and scalable environment for applications, data persistence strategies such as volume mounts or database containerization are recommended for ensuring that data remains intact across container restarts or updates. This aspect will be covered in subsequent posts or steps, focusing on maintaining data integrity and application reliability in Dockerized environments