What and Why Docker Containers

Docker is the open source project. Its purpose is to package the application into an image and run the application into a container. These image are portable meaning that these image can be run on different operation system with different environment setup.

Difference between virtual machines or virtualization is that VM split the hardware into different virtual machine, whereas container split into virtual compartments.

There are many benefits of using containers.

  1. Provide a way to isolate application on single machine.
  2. Easy configuration to build an image and deploy
  3. Create multiple containers for fault tolerance

Let discuss about Docker and simple project how to step a docker container.

Docker image are the file system and configuration of our application which are used to create containers. You can either pull the image from docker hub or build the docker image from your project.

docker pull [image_name]

or if you want to build a image from your project you first need to defined Dockerfile. This docker file contains the configuration of how to build, configure and run the project.

Sample Dockerfile

FROM java:8
ADD target/spring-boot-hello-world-1.0-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongodb/widget", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

In this Dockerfile, FROM java:8 is the image. If image is not in local then the image are pulled by dockers. Volume is the persistent path. ADD will add jar file to into app.jar and EXPOSE will run the docker container on port 8080 locally. RUN bash -c will ensure the modification date on the app.jar and finally providing ENTRYPOINT will execute in this case spring boot project with mongodb database.

to build docker build -t tag_name director_path

This will create an image you can see image by

docker images

Lastly you can run the docker image, this will create and instance of the image called container. to run the docker image for example to run mongodb image

docker run -P -d --name mongodb mongo

This will run docker container type docker ps it view the running containers. If you need to run command on the container in this case mongodb container then type

docker exec -it mongodb sh

You can perform server orchestration using docker-compose in which you can configure different images, build images or run multiple and same containers.

Create docker-compose.yml file.

    build: .
     - "8080"
     - mongodb
   image: mongo

Then run the following command docker-compose up -d. Please make sure docker-compose is installed

If you want to create a microservices architecture then docker is your good friend however, I believe when combining docker with Kubernetes it will make microservices architecture complete. There are other project alternative to Kubernetes such as Docker Swamp. But first, I had learn about these projects and benefits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: