Docker: It is a containerization tool based on a software container platform (Container-as-a-Service platform) that provides a consistent computing environment to package the application and all its dependencies into a container and to automate the deployment of applications in light weigh containers so that isolated applications can work efficiently in different environments. Docker containers can be used throughout the SDLC lifecycle to provide a consistent computing environment.
Dockerizing: A web application is the same as containerizing the application i.e. the process of transforming any web applications to run inside Docker container. We can run this Docker container on any Linux, Ubuntu and Mac machines (Windows Container runs a Windows executable compile for the Windows Server Kernel)
- Dockerizing a Simple Python Web Application:
Docker must have installed in your machine. To install docker, Please refer(https://runnable.com/docker/install-docker-on-windows-10)
Both for a Python flask application and a Spring boot application, we have described the steps for dockerization.
Step 1: Creating the Application (Python app)
The app written with Python and Flask Web Framework. Here all dependencies are managed by, Docker so no need to install python or Flask in local box.
You need to write two files: 1: app.py 2. Dockerfile
Step 2: Creating Dockerfile
Explanations of each line of the above Docker file:
- From: It is the Form instruction were using Python 3.5 as a base image that already has python installed.
- RUN: Used to build the image, and it is the central executing directive for Docker files. It takes a command as its arguments and runs it to form the image. There can be multiple RUN steps inside a docker file.
- Then we will install Flask 0.11.1 using pip install. (a pip is python package management tool)
- Next, we add a new user admin. “-ms” Option to make the default home directory for the admin user. We are setting this default shell of this admin user to bash.
- The Following user instructions in shorts that will be running the app server under the admin user.
[Note: In general, you should always set the user instruction in your docker file to run your process as a non-privileged user within the containers. Otherwise, your process will be running as a root within the container. If attackers break the container, then he would have root access on the host machine because uids are the same. It is best practice that a non-privileged user to run the app server]
- WORKDIR: This sets the working directory for any run cmd: Entrypoint, copy and add instructions
- Here /app as our working dir. We copied the app directory to the container
- CMD: “CMD” command is like RUN used for executing a specific command, but unlike the “RUN”, it is not executed during the build but when the container is instantiated using the image being built. With cmd, the instruction should be used to run the software contained by your image, along with any arguments.
Now we can just use the relative path of the app.py file. Now we are ready to build the container for our flask application.
Step 3: Containerize our application
- Docker Build:
- Issue the below command for building the application
- docker build -t dockerapp:v01 . (“.” For the current directory because docker file sit here)
- docker app is the directory name where we have placed both the python file as well as the docker file.
- V0.1 is the version name of the image for maintaining the image version.
- It will download the python base image, and finally, after everything is done, you can see that successfully built log.
- Now Let’s check out the docker app image by running Docker Images. Enter the command: docker images
- Docker RUN:
Now it’s time to run our docker app container.
- -d: To run a container in the background and -p: Expose the port.
- By default, the python flask server will run on port number: 5000 inside the container. Her I map port 5000 on the container to 0000 on the host so that we can access that container from our local box. 7488186d0f22: image ID of docker app.
- Docker builds the container and starts up in the background and returns us the container ID. See the above image.
Step 4: Test our Application if up and running
Now let’s find out on which IP the Python service is running on. We can find out the IP of the docker-machine by running the command:
Docker-machine ls (This provides various information about the default docker virtual machine)
- Now copy the IP of the docker machine which, is 192.168.99.100 in my case. But the port we have exposed it 5000. SO open your browser and type localhost:5000/ 192.168.99.100:5000 and see the result.
Our App is up and running.
- We can also login to our container. To do that, we have a command docker exec. It allows you to run a command in running a container.
- -it: To run in interactive mode. B8a0cc94180c: This the container ID
Now, after issuing this command, we are inside the container. We are logged in as an admin user.
- We are at the /app directory, which is the working directory we defined in the docker file. Now we can enter the home directory of the admin user: cd /home/admin
Here if we use the command: “ps axu “, it will show all the processes under the running container.
Now you can see our python application is running under the admin user which we exactly defined in our docker file.
Step 5: Push the docker image to a docker repository
Till now, you have learned to build the docker image. After the docker image builds and runs step gets completed, then we can push this image to a repository inside the docker hub. We can later pull this already pushed image inside any docker container using the docker hub credentials. Here I use DockerHub repository which is configured as default repository by docker until or unless specified to use a different repo.
Login to from the command line using the docker hub credentials. For any docker image “push and pull” operation, you need to login to your docker hub. (docker login –username=<Your_user_name>). If you don’t have a Docker ID, head over to https://hub.docker.com to create one.
Username: myusername || Password: ********* || Login Succeeded
Now issue the docker push command to push the image to docker hub
Step 6: Pulling the image from docker hub and running it
After we publish the image to the Docker hub, anyone can pull this image and run it in their environment. Type the following command to pull and run the image on your machine that we just pushed to docker hub –
The docker run command pulls the image from the docker hub if it is not available locally, and then runs it.
You see how easy it is to share your image with others. People don’t need to install anything whatsoever to run your application. All they need to do is pull an image and run it with Docker
1. Dockerizing a Simple Spring Boot application:
We will implement this Simple spring boot web app as a single service.
- Creating Spring boot Application:
Create a basic spring boot application in sts or eclipse.
Then Create a rest end-point to show a sample message:
Build the application using maven/Gradle
- Creating Docker file:
FROM OpenJDK:8 -> Here it will download OpenJDK 8(8 is the version number)image from the docker hub( As java is deprecated, so we took openJDK8)
ADD: We need to add the jar which is, built by our application into the docker container. Here we are adding the jar under the target folder to the current directory (docker container) so that it can run directly from there.
Expose: Exposing our application on the port 8085
Entrypoint: Tell the command with which Docker container needs to run our application Jar
- Docker Build:
- Now we are going to build the image based on the docker file which, we have written above. Go to the current directory of the project and issue the “docker build” command
- Here -f is for the filename. -t is for tag name i.e. what you are going to build i.e. the image name and after that “.” It is for the current directory.
Once the build is successful, then check is image has created. To see the images, issue command: “docker images”. It will show all the images inside the docker container.Docker Run:
- Docker Run:
- As spring boot uses default port 8080. So to redirect the port to 8085 create a file called application.properties and add the below line:
Now we will run the image which we have created.
- Here docker-spring-boot is the image name.
-p 8085:8085 is to push/expose the application on the port 8085 correspondings to the 8085 port of the application which, is running inside the docker container.
- Test our Application is a browser:
Now open your browser and type http://localhost:8085/rest/docker/hello and you will get the output as
“Hello Youtube” as below image
The application that we dockerized in this article is very simple. It doesn’t use any database or communicate with other services. Hire dedicated Java Developers for java application development project solutions.