Learn to Dockerize Your App: Day 2 of 40daysofkubernetes

ยท

4 min read

In this blog, we will learn how to dockerize an application.

Let's get started

  1. 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
  1. cd into the getting-started-app directory.

  2. Create a file named Dockerfile using the command below.

nano Dockerfile 
or 
vim Dockerfile
  1. 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 subsequent COPY, ADD, RUN, CMD, or other instructions will be executed in this directory.
  • 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.
  • RUN yarn install --production:

    • This line runs the yarn install --production command inside the container. It installs the production dependencies listed in your package.json file. The --production flag ensures that only dependencies required for running the application are installed, excluding development dependencies.
  • 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. The CMD 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).
  • 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.
  1. Now build the Docker image with the following command:

     docker build -t day02-todo .
    
  2. Verify the image has been created and stored locally using the below command

     docker images
    
  3. 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
    

  4. Now you can use this application in any environment by pulling the image from Docker Hub to your machine.

  5. To verify, pull the image to your local machine using the following command:

     docker pull username/new-reponame:tagname
    
  6. 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 the EXPOSE 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 include latest, v1.0, etc.

  1. Verify your app. Your app should be listening on localhost:3000

Resource:

Congrats, you learned how to dockerize your application!

ย