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.
- Provide a way to isolate application on single machine.
- Easy configuration to build an image and deploy
- 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.
FROM java:8 VOLUME /tmp ADD target/spring-boot-hello-world-1.0-SNAPSHOT.jar app.jar EXPOSE 8080 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.
docker build -t tag_name director_path
This will create an image you can see image by
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.
employee: build: . ports: - "8080" links: - mongodb 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.