Exercise 2 - Task 1 - Docker Containerization
Goal is to package our application into a docker image and deploy it to dockerhub. In this first task we are only going through this process once manually before automating it later.
2.1.1 Package the App as Docker Image
- Create a
Dockerfile
in the root of your project (useNew/File
in IntelliJ). - See course slides for how to write a simple
Dockerfile
for a usual Java Application.- For simplicity: just copy the already built jar file
build/libs/project-automation-chatbot.jar
into the docker image (the gradle build already produces this jar, without any version number in the name) - For advanced participants only: you could as well make the location of the jar to package into the docker a parameter that can be passed into the docker build by using docker's ARG command, as explained here: https://docs.docker.com/engine/reference/builder/#using-arg-variables
- For simplicity: just copy the already built jar file
You can build the docker image with docker tooling as follows:
docker build -t exercise4automation/project-automation:<put-your-version-here> .
-t
: this is the parameter to specify the full image name tag under which the image will be stored and under which you will later upload it to dockerhubexercise4automation
: this is the username or the organization of the repository under which you are going to publish your image (we prepared a dockerhub account with this nameexercise4automation
for you)project-automation
: this is the name of the image repository under which you want to store your image (we prepared a repository for image nameproject-automation
for you to be used in the exercise)<put-your-version-here>
this is the version tag of your image tag, choose something uniquely for your version, that you can use everywhere in this exercise where it says<put-your-version-here>
, something liketeam-xy-1.0.0
..
: do not forget the.
at the end! This is the path to the Dockerfile to build.- command ahs to be run inside the project directory where the Dockerfile is
You can test the built docker image locally as folows:
docker run -p 8080:8080 exercise4automation/project-automation:<put-your-version-here>
-p 8080:8080
this means to map port 8080 under which the application is running to host's port 8080, such that you can access it through http from the host<put-your-version-here>
use same version tag as during build!
- Check that the web application is running properly under http://localhost:8080
2.1.2 Publish your App on Dockerhub
- After the docker image has been built it can be uploaded to dockerhub as follows.
Ask the trainer for the password (hint: it is the same as for the jenkins server)docker login --username='exercise4automation' --password='<same-password-as-for-jenkins>' docker push exercise4automation/project-automation:<put-your-version-here>
- Browse to dockerhub and check you can see your just uploaded tag: https://cloud.docker.com/repository/registry-1.docker.io/exercise4automation/project-automation
2.1.3 Test the Released Docker Image
Now that you published it to dockerhub, you can run your image on every machine you can imagine. E.g. somewhere in the cloud.
Let's try it out on https://labs.play-with-docker.com/ which is a cool environment to test out some docker images.
- Go to https://labs.play-with-docker.com/
- Choose
Docker/Start/Add new instance
to get logged in to a fresh and clean machine where you can run your tests - Run the webapp with docker as follows from the play with docker console:
docker run -p 8080:8080 exercise4automation/project-automation:<put-your-version-here>
- Follow the link
8080
at the top of the page to browse to the webapp running now on this machine on port 8080 through a webproxy - Have some fun: try out some of the just deployed bots and chat to them using the Swagger UI