loading...

How to dockerize an application?

radiumsharma06 profile image Radium Sharma ・3 min read

This article is mainly to show basic steps we need to follow in order to dockerize an application based on different tech stack.

Lets start by dockerizing a nodejs application.
First of all create a file named 'Dockerfile' without any extension in the parent directory of your project.

This is a basic Dockerfile we need to dockerize a node application

FROM node:4-onbuild
RUN mkdir /app
COPY . /app/
WORKDIR /app
RUN npm install
EXPOSE 8234
CMD [ "npm", "start" ]

I will explain each command and what it does.
If you look carefully you will notice that this Dockerfile is actually doing basic steps we do to run any nodejs application.

Let us look at each command one by one.

1. FROM node:4-onbuild

This command pulls/downloads a base image from docker hub which is a public hub for docker images.For running a node appication you need to install node in your system or for running java you need jdk same way we need to add a base node image in our docker environment.

2. RUN mkdir /app

In this command we make create an empty directory which will be our workin directory with the code files.

3. COPY . /app/

This command copies all files in current directory to the newly created app directory.Your Dockerfile should be in the parent directory of your project.

4. WORKDIR /app

Here we switch from current directory to the app directory where we will run our application.

5. RUN npm install

This npm command is related to node application.When we copied all dependencies in step 3, our main file - package.json would have been copied.So running above command installs all dependencies from the file and creates a node_modules folder with mentioned node packages.

6. EXPOSE 8234

This command is to expose a port we want our docker image to run on.

7. CMD [ "npm", "start" ]

This is a command line operation to run a node application. It may differ based on projects.

Now once we have our Dockerfile ready lets build an image out of it.
Assuming you all have docker installed on your system lets follow some simple steps:-

  • Navigate to directory containing Dockerfile.

  • Run the following command on your terminal:-

    docker build -t 'any image name' .
    

    This command will start executing steps in Dockerfile one by one.
    It will first look for the base image in your local docker repository if it doesn't find it it will download the image from docker hub.
    Considering Dockerfile is correctly written your image will be built.

  • Run the following command to look for your image.

    docker images
    

    This command will list down all images in your local docker repo.Considering you gave your image name as hello-docker, you can see this name when you run the above command.

  • Now we have image ready but we need a container to see our application running.A container is nothing but a running instance of your image.Run the following command to start an image or build a container.

    docker run -p 8080:8080 'your image name'
    

    This will create a container with your image.You can give port number of your choice.

  • Now to after running the previous command we can go to the mentioned port and see our application running.To see the running container run the following command:-

    docker ps
    

    To see all containers in your docker run the following command:-

    docker ps -a
    

    You will see containers with random names and ids.

  • To delete a container run following command:-

    docker rm 'container name/ container id'
    
  • To stop container run folloing :-

    docker stop 'container name/ container id'
    
  • To start container run following command:-

    docker start '<container name/ container id>'
    
  • You can also delete an image but make sure it doesn't have any containers.Run the following command:-

    docker rmi 'your image name'  
    

Hope this article helps in dockerizing a basic application there is lot more we could do with docker.

Below is another Dockerfile which I created for a java project I was working on.You can easily relate it to previous Dockerfile.

FROM openjdk:8-jdk-alpine
RUN mkdir /apollo-services
COPY . /app
WORKDIR /app
RUN ./gradlew raml-generate
RUN ./gradlew clean build
WORKDIR /app/service-impl/build/libs
EXPOSE 8080
ENTRYPOINT ["java","-jar","main.jar"]        

Thank you all and have a great day :) !!

Discussion

markdown guide