Welcome back to our blog series on becoming a Certified Kubernetes Administrator. This is post number two in our comprehensive series designed to cover all concepts, demos, and hands-on exercises for the Kubernetes certification exam, based on the latest 2024 curriculum.
In our previous post, we discussed container fundamentals: why they are necessary, how they work, and how they differ from virtual machines. If you are already familiar with the basics of containers, feel free to skip that post. However, if you are new to the concept, I highly recommend starting there.
In this post, we'll be diving into a hands-on demo on how to dockerize an application from scratch. This guide is intended for beginners and will cover everything from writing a Dockerfile, running multiple commands to build your container, and finally hosting your application.
Prerequisites
Before we begin, make sure you have Docker installed on your computer. Docker provides detailed installation instructions for various operating systems here. If you encounter any issues installing Docker locally, you can use Docker's sandbox environment at Play with Docker.
Step 1: Setting Up the Environment
First, we need a sample application to dockerize. We will use a simple to-do list application available on GitHub.
1. Clone the Repository
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app
2. Create a Dockerfile
Create a new file named Dockerfile in the root of your project directory.
touch Dockerfile
Open the Dockerfile in a text editor and add the following content:
Use an official Node runtime as a parent image
FROM node:18-alpine
Set the working directory in the container
WORKDIR /app
Copy the current directory contents into the container at /app
COPY . /app
Install any needed packages specified in package.json
RUN yarn install --production
Make port 3000 available to the world outside this container
EXPOSE 3000
Define environment variable
ENV NAME World
Run app.py when the container launches
CMD ["node", "src/index.js"]
Step 2: Building the Docker Image
With the Dockerfile in place, we can now build our Docker image.
docker build -t day02-todo .
This command will build the Docker image and tag it as day02-todo.
Step 3: Running the Docker Container
Now that we have our Docker image, we can run it as a container.
docker run -d -p 3000:3000 day02-todo
This command will run the container in detached mode and map port 3000 on the host to port 3000 in the container.
Step 4: Pushing the Image to Docker Hub
To make our Docker image available for others, we need to push it to Docker Hub.
1. Login to Docker Hub
docker login
2. Tag the Image
docker tag day02-todo <your-dockerhub-username>/day02-todo:latest
3. Push the Image
docker push <your-dockerhub-username>/day02-todo:latest
Step 5: Pulling and Running the Image on Another Machine
To verify that everything works correctly, you can pull the Docker image on another machine and run it.
docker pull <your-dockerhub-username>/day02-todo:latest
docker run -d -p 3000:3000 <your-dockerhub-username>/day02-todo:latest
Conclusion
In this post, we covered the steps to dockerize a simple application, build a Docker image, run a container, push the image to Docker Hub, and finally pull and run the image on another machine. We also touched on some best practices for optimizing Docker images.
In the next post, we'll delve into more advanced Docker concepts and Kubernetes integration. Stay tuned and happy learning!
Feel free to leave comments or questions below. Until next time!
Top comments (0)