This content originally appeared on DEV Community and was authored by Louretta
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)
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.
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.
Add the Docker GPG key to your system's list of trusted keys using command below
Add the Docker repository on your Ubuntu system which contains Docker packages including its dependencies, for that execute the below command:
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)
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)
Set the correct permissions so that doxker-compose is executable
Run this command to verify sucessfule installation
3.Containerization of Node.js Application
Start docker engine and check status
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
Run ls to see cloned files
To see content in directory run comand below
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
Then, paste the following code into the Dockerfile
Change directory to the app directory
Build the container image using the following command
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
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
- 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"
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
Use the same docker build command you used before to create an updated version of the image.
After building start a new container
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
Remove the stopped container to free up the port
Now that the old container is stopped and removed, run the updated image
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.
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.
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.
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
Push image to dockerhub using the command below and expected output
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
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
This content originally appeared on DEV Community and was authored by Louretta
Louretta | Sciencx (2024-07-09T18:27:33+00:00) Run A NodeJs Application In a Docker Container. Retrieved from https://www.scien.cx/2024/07/09/run-a-nodejs-application-in-a-docker-container/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.