1. What is Docker?
Docker is a platform designed to create, deploy, and run applications using containers. Containers allow developers to package applications with all necessary dependencies, ensuring they run seamlessly in any environment.
2. Why Dockerize an application?
Consistency: Docker eliminates the "works on my machine" issue.
Portability: Docker containers can be moved across different environments.
Scalability: Docker integrates easily with orchestration tools like Kubernetes.
By Dockerizing your application, you ensure it can run in any environment, be it local development, testing, or production, without configuration issues.
3. Cloning the Repository
First, clone your project repository from GitHub. If you don’t have a project, you can use an existing app from GitHub.
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app
4. Exploring docker init
Command
Docker recently introduced a new command called docker init
to generate Docker-related files (like Dockerfile
, .dockerignore
, etc.) automatically. This command simplifies the initial setup of Dockerization.
Run the following command in the project directory:
docker init
This will prompt Docker to analyze the project and auto-generate the Dockerfile. You can then modify the file if needed, but the generated Dockerfile usually serves as a great starting point.
If docker init
is not available, you can manually create the Dockerfile, which we’ll cover next.
5. Creating a Dockerfile
The Dockerfile is a script that contains instructions on how to build a Docker image for your application. Below is a simple Dockerfile for a Node.js project.
#base image
FROM node:18-alpine
#working Dir
WORKDIR /app
#copy code into contaioner
COPY . .
#compile code
RUN yarn install --production
#run
CMD ["node" , "src/index.js"]
#expose port
EXPOSE 3000
1. FROM node:18-alpine
This line defines the base image for your Docker container. You're using node:18-alpine
, a lightweight version of Node.js 18 based on the Alpine Linux distribution. The Alpine image is smaller in size, making the Docker image more efficient and faster to build and deploy.
Why Alpine?: It’s minimal and has a reduced attack surface, which improves security.
Why Node.js 18?: This specifies that your container will run with Node.js version 18.
2. WORKDIR /app
This command sets the working directory inside the container to /app
. It means that any subsequent commands (like COPY
or RUN
) will be executed inside this directory.
- Purpose: This ensures that your application files will be copied and run in a specific, isolated directory inside the container. This also helps prevent file clashes in other parts of the container.
3. COPY . .
This command copies the content of your project’s root directory (on your host machine) into the working directory inside the container (/app
).
First dot (.): Refers to the current directory on your host machine (where the Dockerfile is located).
Second dot (.): Refers to the current directory inside the container (
/app
in this case).
4. RUN yarn install --production
This command runs the yarn install --production
command inside the container. This installs the necessary dependencies for the application.
yarn install --production
: It installs only the production dependencies listed in thepackage.json
file. Development dependencies are not installed, which reduces the container's size and increases its security.Why
yarn install
and notnpm install
?: Yarn is another package manager for JavaScript, like npm, and it’s used if your project setup includes Yarn. It offers performance benefits like faster dependency resolution and deterministic installs.
5. CMD ["node", "src/index.js"]
This line specifies the default command that will be executed when the container starts.
CMD ["node", "src/index.js"]
: It tells Docker to run Node.js, and specifically execute theindex.js
file located in thesrc
folder. This is usually the main entry point of your application.
6. EXPOSE 3000
This command informs Docker that the container will listen on port 3000 at runtime.
- Port 3000: This is the typical port used for Node.js applications. Exposing this port allows external traffic to communicate with your app when the container is running.
6. Building an Image using Dockerfile
docker build -t todo-app .
7. Check docker images
docker images
8. Pushing the Docker Image to Docker Hub
Once your Docker image is built and tested locally, you can share it with others by pushing it to Docker Hub. Follow these steps to log in to Docker Hub and push your image.
Step 1: Log in to Docker Hub
Before you can push your image, log in to your Docker Hub account. If you don't have an account, you can create one at Docker Hub.
Run the following command in your terminal:
docker login
You will be prompted to enter your Docker Hub username and password. After entering your credentials, you should see a message confirming that the login was successful:
Login Succeeded
Step 2: Tag the Image
Next, you need to tag your image so that Docker knows where to push it on Docker Hub. Use the following command to tag the image:
docker tag todo-app:latest <username>/todo-app:latest
Step 3: Push the Image
Now that the image is tagged, you can push it to Docker Hub:
docker push <username>/todo-app:latest
Docker will begin uploading the image to Docker Hub. Once the process is complete, you should see the image listed in your Docker Hub account under "Repositories."
Step 4: Pull and Run the Image
Now that your image is available on Docker Hub, anyone can pull and run it using the following commands:
docker pull harshitsahu2311/todo-app:latest
docker run -d -p 3000:3000 harshitsahu2311/todo-app:latest
This will pull the image from Docker Hub and run the container on port 3000.
Step 5: Check the running container
docker ps
9. Enter into a running container
docker exec -it boring_mcnulty sh
Thanks for reading my blog and keep learning! ☸️♾️