Photo by Rinson Chory on Unsplash
Dockerizing the Application: A Step-by-Step Guide
Day 2 of #40daysofKubernetes
Welcome back to Day 2 of #40DaysOfKubernetes! Today, we embark on an exciting journey into Docker, focusing on the essential process of dockerizing an application. Let's dive right in and explore how Docker can streamline your application deployment process.
Prerequisites:
If you're not using the Kubernetes sandbox environment, Ensure Docker is up and running before we dive into the exciting world of containerization.
1. Playing with Docker (Sandbox)
Our journey began with the Docker playground, an invaluable online sandbox environment. Here, we experimented with Docker commands without the need to install anything locally. This hands-on experience allowed us to grasp Docker's fundamentals in a risk-free environment.
https://labs.play-with-docker.com/
https://labs.play-with-k8s.com/
2. Docker Desktop
Next, we leveraged Docker Desktop, a powerful tool offering a user-friendly interface for managing Docker containers. Installing Docker Desktop simplified the process of building, running, and managing containers on our local machine, laying a solid foundation for our Docker journey.
https://www.docker.com/products/docker-desktop/
3. Cloning a GitHub Repository
To put our Docker knowledge into practice, we cloned a sample GitHub repository housing a straightforward todo application. This application served as our testing ground as we proceeded to dockerize it.
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app/
4. Writing the Dockerfile
The Dockerfile is pivotal in dockerizing any application. It comprises instructions Docker uses to construct an image. Below is the Dockerfile we crafted for our Node.js todo app:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
5. Building the Docker Image
Equipped with our Dockerfile, we proceeded to build our Docker image. This image encapsulates everything essential to run our application.
docker build -t day02-todo .
6. Exploring Docker Hub
We delved into Docker Hub, a cloud-based repository for storing and sharing Docker images. Establishing an account, we set up a repository for our todo app.
docker images
7. Pushing the Docker Image
With our local image ready, we pushed it to our Docker Hub repository. This step enables seamless access for others to deploy our application.
docker push ishriram/test-repo:new
8. Pulling the Docker Image
To verify our deployment, we pulled the Docker image from Docker Hub into a different environment.
docker pull ishriram/test-repo:new
9. Running the Docker Container
Finally, we executed our Docker container, launching our todo application on port 3000.
docker run -dp 3000:3000 --name mycontainer day02-todo
Now we can access our todo app. Although I'm using Amazon EC2 instead of a local host, the process remains the same: just use <ec2-instance-ip:port\>.
docker exec: This command allows you to execute a command inside a running container. Here’s how you typically use it:
docker exec -it <container_name_or_id> <command>
-it
: Enables interactive mode (useful for accessing a shell inside the container).<container_name_or_id>
: Specifies the container where the command will be executed.<command>
: The command to run inside the container.
For example:
docker exec -it my_container sh
This command opens an interactive shell (sh
) inside the my_container
.
docker logs: This command fetches the logs of a container. Here’s the basic usage:
docker logs <container_name_or_id>
<container_name_or_id>
: Specifies the container whose logs you want to fetch.
For example:
docker logs my_container
This command retrieves the logs of the my_container
.
These commands are invaluable for troubleshooting, debugging, and managing Docker containers effectively during your learning process.
Summary
Today was dedicated to mastering Docker fundamentals and the process of dockerizing an application. From the Docker playground to Docker Desktop, and from writing a Dockerfile to deploying our application via Docker Hub, each step brought us closer to harnessing the power of containerization in Kubernetes.
For further reference: https://youtu.be/nfRsPiRGx74?si=an3TVgNizrgjeRgU