-
Create new file in root directory of application. Name it
Dockerfile. -
Write configurations as given in this file.
-
Save the file. Open terminal and run command:
docker build -t order-service .This will build a docker image named
order-servicefrom Dockerfile. It may take few minutes to finish the process. -
Once finished, verify the image you built. To do so, run following command in terminal:
docker imagesThis will list out all docker images you have so far. Check whether
order-serviceis there in the list. -
To run docker container using this image, run command:
docker run -d -p 12345:12345 --name order-container order-service -
To see the running containes, execute following command:
docker psYou may see your
order-containerthere. -
To test application, go to browser and browse for
http://localhost:12345. You may see a message likeOrder-Management-Service is running on port 12345. You may now test api routes for various models usingPostman. -
To stop container, use command:
docker stop order-containerTo remove container, run command:
docker rm order-container
To test whole application in containerized environment, you need to use Docker-compose. We need three containers i.e. database-container, application-container and either Zipkin or Jaeger container. So instead of running them separately, we can run them in one go using Docker-compose. To do so, follow these steps:
-
Create a new file in root directory of application and name it
docker-compose.yml. -
Write configurations for three containers in
Yamlformat. Refer this file. -
Once finished with configurations, save the file and open terminal. Run following command:
docker compose up --build -dThis command will pull docker images for
MYSQLandZipkinfrom docker hub and use them to run their containers and it will also build docker image of application usingDockerfilethat we have already created. -
To see list of running containers, run command:
docker compose ps -
Now you may access application at
http://localhost:12345and Zipkin UI athttp://localhost:9411. Test application by sendingPostmanrequests. You may see thr traces of your requests on Zipkin UI. -
To stop and remove all containers, run command:
docker compose down