Tuesday, April 7, 2020

Containerizing your apps 2 - Important docker commands with sample rest service


In this post, it will explain how to run simple rest service with Docker environment. Deploying a single entity in Docker environment is not the purpose of using docker. Docker is a container orchestration platform that can address auto-scalable, fault-tolerant features that cannot be addressed in monolithic models. But bellow commands will be important when you do your local developments.


1. Install docker

Remove previous docker versions : - sudo apt-get remove docker docker-engine docker.io
Update your Linux :- sudo apt-get update
Install docker :- sudo apt install docker.io
Start docker service :- sudo systemctl start docker
Enable docker at startup :- sudo systemctl enable docker
Check docker version :- sudo docker version
Add user group permission :- sudo usermod -aG docker $USER
Reboot your Linux :- sudo reboot

2. Create jar file that will expose REST API ( ex: testing.jar )

Create your own java application that will expose simple rest servcei

2. Create a file name called Dockerfile where the jar file is located

3. Add bellow content to Docker file

FROM openjdk:10
EXPOSE 8888
ADD testing.jar ./testing.jar
ENTRYPOINT ["java","-jar","-Duser.timezone=GMT+0530","testing.jar"]

4. Command to build and run the jar file
  1. sudo docker build --tag=testimage .
  2. sudo docker run -p 8888:8888 testimage &
  3. open browser and access your rest service http://localhost:port/yourservice
5. List Docker images


sudo docker image ls

6. List Docker containers ( Container is an instance of image can be run isolated )


sudo docker container ls

7. Go to Docker container console ( has small linux console )

sudo docker exec -it container_id  /bin/sh

8. Stop Docker container ( Then your rest service will be stopped )

sudo docker stop container_id

9. Remove Docker container

sudo docker rm container_name

10. Remove Docker image

sudo docker image rm -f image_id