Learn to Dockerize Your App: Day 2 of 40daysofkubernetes
In this blog, we will learn how to dockerize an application.
Let's get started
- Clone a sample git repository using the below command or use your project for the demo:
git clone https://github.com/docker/getting-started-app.git
cd into the
getting-started-app
directory.Create a file named Dockerfile using the command below.
nano Dockerfile
or
vim Dockerfile
- Paste the following code into your Dockerfile.
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
Explanation of this code:
FROM node:18-alpine
This line specifies the base image for your Docker image. In this case, it uses the official Node.js image with version 18, based on the Alpine Linux distribution. Alpine is a minimal Docker image, which helps keep the final image size small.
WORKDIR /app
- This line sets the working directory inside the Docker container to
/app
. Any subsequentCOPY
,ADD
,RUN
,CMD
, or other instructions will be executed in this directory.
- This line sets the working directory inside the Docker container to
COPY . .
- This line copies the contents of your local directory (where the Dockerfile is located) into the
/app
directory in the Docker container. The first.
represents the source on your local machine, and the second.
represents the destination inside the container.
- This line copies the contents of your local directory (where the Dockerfile is located) into the
RUN yarn install --production
:- This line runs the
yarn install --production
command inside the container. It installs the production dependencies listed in yourpackage.json
file. The--production
flag ensures that only dependencies required for running the application are installed, excluding development dependencies.
- This line runs the
CMD ["node", "src/index.js"]
:- This line specifies the command to run when the container starts. It uses the Node.js runtime to execute the
src/index.js
file. TheCMD
instruction is an array where the first element is the executable (node
) and the subsequent elements are the arguments to that executable (src/index.js
).
- This line specifies the command to run when the container starts. It uses the Node.js runtime to execute the
EXPOSE 3000
:- This line informs Docker that the container listens on port 3000 at runtime. It's a way of documenting the port that the application inside the container uses. However, it doesn't actually publish the port on the host machine; you need to use the
-p
flag when running the container to map the container's port to a port on the host.
- This line informs Docker that the container listens on port 3000 at runtime. It's a way of documenting the port that the application inside the container uses. However, it doesn't actually publish the port on the host machine; you need to use the
Now build the Docker image with the following command:
docker build -t day02-todo .
Verify the image has been created and stored locally using the below command
docker images
Create a public repository on Docker Hub and push the image to the remote repo
docker login docker tag day02-todo:latest username/new-reponame:tagname docker push username/new-reponame:tagname
Now you can use this application in any environment by pulling the image from Docker Hub to your machine.
To verify, pull the image to your local machine using the following command:
docker pull username/new-reponame:tagname
Run the container by following command
docker run -dp 3000:3000 username/new-reponame:tagname
Explaination of this command:
docker run
:
- This command is used to create and start a new container from a specified Docker image.
-d
:- This flag runs the container in detached mode, meaning it runs in the background and doesn't block your terminal. You can still interact with the container using other Docker commands.
-p 3000:3000
:This flag publishes a container's port to the host. The format is
hostPort:containerPort
.3000:3000
means that port 3000 on the host machine is mapped to port 3000 inside the container.The first
3000
is the port on the host machine.The second
3000
is the port inside the container. This should match the port your application is listening on inside the container, which is defined by theEXPOSE 3000
instruction in your Dockerfile.
username/new-reponame:tagname
:This specifies the image to run.
username
is the Docker Hub username or the repository owner.new-reponame
is the name of the repository (or image) on Docker Hub.tagname
is the tag of the image you want to run. Tags are used to version images. Common tags includelatest
,v1.0
, etc.
Verify your app. Your app should be listening on localhost:3000
Resource:
Congrats, you learned how to dockerize your application!