to dockerize an application, firstly we need the code, I am taking a sample Getting Started with Docker project.
After cloning, to dockerize the app, we neeed to create a Dockerfile which will have instructions to containerize the app.
Using touch Dockerfile command, we will create a Dockerfile.
then using vi Dockerfile command, it will open the file in a visual editor inside the terminal itself.
To begin the dockerization, first thing we will need a base OS image on which our app will be installed.
So, first instruction would be the base OS image, we will be using:
FROM node:18-alpine
Here, 18 is the node version and alpine is a lightweight linux based OS with minimum libraries and won't take a lot of space.
We can use a linux based OS image as well like ubuntu and then install node on top of it, but it will make the image heavier as ubuntu will have some default dependencies and libraries. So, we will be using
nodebase image as it provides an OS already.
Next, we define where we'll be doing all the work inside our container using the WORKDIR command.
WORKDIR /app
this creates a folder named app at the root level and everything will be done in this folder.
Next, we need to copy the files/source code from our local machine to the container using:
COPY . .
Here, the first
.represents the current local directory and the second.represents the work directory in the container.
Then, after copying the source code, we need to install the dependencies using:
RUN yarn install --production
Next, we run the application using the command:
CMD ["node", "src/index.js"]
it is equivalent to running
node index.json our local terminal. This would execute the index.js file in src folder.
The application would start running and your pages would render, but we also need to expose the port from our container in order to access it. We can do this using:
EXPOSE 3000
This would expose the app on port 3000
Complete Dockerfile is attached for reference and can also be found on github repository attached at the end of article.
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
Now, as our Dockerfile is ready, we need to build an image from it. To do so, we use the docker build -t day02-sample-app . command to build the image.
Here, day-02-sample-app is the name of the image and . represents the Dockerfile is in the current directory.
Each step inside the Dockerfile is executed as a separate step. Meaning, docker will create the image in layers which are disintegrated when you push and pull the image, making process faster, and then combine these layers to form the complete image. Docker exports image in layers.
Now, the image is ready in our local system, We can view it by using the command:
docker images it would list all the images present locally. To use it on a remote server, we need to push this image to a registry. In our case we will be using Docker Hub.
To do so, we create a repository on Docker Hub named "sample-app"
And to push the local image here, first we need to tag the local image using the command:
docker tag day02-sample-app:latest gambhirsamarth/sample-app:latest
Here, day02-sample-app is the local image name and latest represents the tag. gambhirsamarth is the user/organization and /sample-app signifies the repo name and latest is its tag.
Tags are nothing but version numbers of images, default is "latest"
Now, we are ready to push the image. using :
docker push gambhirsamarth/sample-app
After pushing the image to a remote registry, we are ready to pull this image and deploy on an environment.
How to use the image ?
To use the image we just created, we need to pull this image on our server using this command:
docker run -dp 3000:3000 gambhirsamarth/sample-app
Here, docker run is the standard command to run a container.
-dis for detached mode, meaning the container will be up and running without the terminal connecting to it.pis for port forwarding.3000:3000specifies the internal port to be mapped with the external port.
Summary
By writing a Dockerfile, we successfully packaged our application and its entire runtime environment into a single, portable image. You've now seen firsthand how to build, push, and run that image anywhere officially putting the "it works on my machine" problem to rest.
In the next step of our series, we will level up our Docker game by diving into Multi-Stage Docker Builds to optimize our images before we finally move on to orchestrating them.
Stay tuned for Part 3!




Top comments (0)