As developers continue to build and deploy applications in increasingly complex environments, containerization has revolutionized this by simplifying the process. Docker, a leading tool for Containerization, allows developers to package applications with their dependencies and collaborate efficiently while maintaining consistency across different environments. To package the application and share it with others, developers need to containerize it by building a Docker image of the app. Further, they publish it to Docker Hub, where others can pull the image and continue the development on their machines, similar to how we deal with codebases using GitHub.
In this blog, we’ll learn how to create a Dockerfile for an app, build a Docker image, and push it to Docker Hub, making it accessible to others.
Prerequisites
- A Virtual Machine (such as the ones provided by NodeShift) with at least:
- 2 vCPUs
- 2 GB RAM
- 10 GB SSD
- Ubuntu 22.04 VM
- Docker installed on Ubuntu
Note: The prerequisites for this are highly variable across use cases. A high-end configuration could be used for a large-scale deployment.
Step-by-step process to push Docker image to Docker Hub
For this tutorial, we’ll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. It also offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.
Step 1: Setting up a NodeShift Account
Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.
If you already have an account, login straight to your dashboard.
Step 2: Create a Compute Node (CPU Virtual Machine)
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the Compute Nodes option.
- Click on Start to start creating your very first compute node.
These Compute nodes are CPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations, such as vCPUs, RAM, and storage, according to your needs.
Step 3: Select configuration for VM
- The first option you see is the Reliability dropdown. This option lets you choose the uptime guarantee level you seek for your VM (e.g., 99.9%).
- Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).
- Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.
Step 4: Choose VM Configuration and Image
- After selecting your required configuration options, you’ll see the available VMs in your region and as per (or very close to) your configuration. In our case, we’ll choose a ‘1vCPUs/2GB/50GB SSD’ as the closest match to the “Prerequisites”.
- Next, you’ll need to choose an image for your Virtual Machine. For the scope of this tutorial, we’ll select Ubuntu, as we create the Docker image on Ubuntu.
Step 5: Choose the Billing cycle and Authentication Method
- Two billing cycle options are available: Hourly, ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly for long-term projects with a consistent usage rate and potentially lower cost.
- Next, you’ll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.
Step 6: Finalize Details and Create Deployment
Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We’re keeping this option as the default for now, but feel free to create a VPC according to your needs.
Also, you can deploy multiple nodes at once in the Quantity option.
That’s it! You are now ready to deploy the node. Finalize the configuration summary; if it looks good, go ahead and click Create to deploy the node.
Step 7: Connect to active Compute Node using SSH
As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!
Once your node shows this status, follow the below steps to connect to the running VM via SSH:
- Open your terminal and run the below SSH command:
(replace root
with your username and paste the IP of your VM in place of ip
after copying it from the dashboard)
ssh root@ip
2. In some cases, your terminal may take your consent before connecting. Enter ‘yes’.
3. A prompt will request a password. Type the SSH password, and you should be connected.
Output:
4. Also, if you want to use VS Code on Ubuntu VM, you need to connect your local VS Code editor to the Ubuntu VM via SSH with the following steps:
a) Install the “Remote-SSH” Extension by Microsoft on VS Code.
b) Type “Remote-SSH: Connect to Host
” on the Command Palette.
c) Enter the host details, such as username and SSH password.
d) Your SSH connection should be established!
Step 8: Create a Dockerfile for the app
Once you have installed Docker on Ubuntu (as mentioned in Prerequisites) and connected to the Visual Studio Code editor, proceed to create a Dockerfile for your application.
For demonstration purpose, we’ve created a very simple Express app with Node.js that displays “Hello Docker!” on the browser when the server is up and running. We will create a Docker image for this app in this tutorial.
- Create a file, namely
Dockerfile
(without extension), in the root directory of your project.
This is an example directory structure of our app:
2. Configure the Dockerfile
Next, add the app-specific instructions to the Dockerfile
,
# Use an official Node.js image as the base image
FROM node:19-alpine3.16
# Set the working directory inside the container and update the PATH environment variable
WORKDIR /app
ENV PATH="/app/node_modules/.bin:$PATH"
# Copy application files to the working directory
COPY . .
# Install application dependencies & build the app
RUN npm install
RUN npm run build
# Specify the command to start the application
CMD ["npm", "run", "start"]
which may look similar to this in the file:
Here’s what each term specifies:
FROM
: to select the Node.js base image.
WORKDIR
: to set the container’s working directory.
ENV
: to set environment variables (path).
COPY
: to add app files to the container.
RUN:
to install dependencies and build the app.
EXPOSE
: to specify the app’s port.
CMD
: to start the app.
Step 9: Build the Docker image
Once the configuration is done, run the following command to build the Docker image:
Note: Make sure to include a build script in your project directory and create a production build of your app using “npm run build
” (in case of a Node app) before building the Docker image.
(replace <IMAGE_NAME>
with a name for your Docker image)
docker build -t <IMAGE_NAME> .
Confirm if the image is created using the following command:
docker image ls
Output:
As you may see, our image has been successfully created. Let’s move on to pushing it to Docker Hub.
Step 10: Push the image to Docker Hub
Now, that we have created the Docker image, we need to push it to Docker Hub, to make it publicly/privately accessible. Docker Hub is a library of Docker images, from where you can pull images of other apps and push your own for others to use.
- Create an account or log in to Docker.
- Navigate to Docker Hub.
- Click on Create a Repository, as shown in the below image.
4. Fill in the necessary details and click Create to create the repository.
5. Once the repository is created, go to the Ubuntu terminal and login to Docker registery using your credentials.
(replace <USERNAME>
and )
docker login --username <USERNAME> --password <PASSWORD>
6. Next, tag the image before pushing it to Docker.
Tagging an image will help you version your Docker images for different releases and rollbacks when necessary. For example, we tag the current build image as “latest
” indicating that it’s the newest version of the image.
docker tag <IMAGE_NAME>:<TAGNAME> <USERNAME>/<REPOSITORY_NAME>:<TAGNAME>
8. Proceed to push the image to the Docker hub.
docker image push <USERNAME>/<REPOSITORY_NAME>:<TAGNAME>
Output:
9. Confirm the image in Docker Hub
Finally, navigate to the Docker Hub and check under your repository to verify the newly pushed Docker image.
As shown above, our Docker image has been successfully pushed to the Docker hub!
Conclusion
In this blog, we have covered how to create a Dockerfile for a Node.js application, build the Docker image, and push it to Docker Hub, all from a cloud-based Ubuntu VM. By containerizing your application, you ensure the app is consistent and scalable across environments, making deployment easy and smooth for anyone who needs to interact with the codebase. For developers seeking an efficient, reliable, and developer-friendly cloud platform to host and manage containerized applications, NodeShift offers powerful cloud computing, customizable for modern web applications. Whether you’re starting small or scaling big, NodeShift empowers you with the easiness you need to optimize your cloud infrastructure while shadowing the complexity under the hood.