Hello again. In this article i will explain how to run an application on a docker container. Docker is an open-source project that automates the deployment of applications inside software containers build from images. This gives the opportunity of flexibility and portability for submitted applications. The management is extremely easy. It is managed on linux command line. For more information and more commands, please see Docker official page https://www.docker.com/
To deploy an application with docker, one has to use docker containers. Docker containers can be pulled by docker run or docker pull command. If the image is not installed for that container, Docker will install it automatically. Or the images can be created manually according to the necessary software.
Before we start, i feel like i need to give some pre-informations about the usage of docker. The relation between a container and an image can be taught as a sculpture and mold. The container is sculpture and the image is mold. Which means a container is made of an image and to run an application on docker, one needs to create a container from an image. When docker creates the container automatically, that is pretty much the way it achieves that goal. More than one container can run at the same time. If you want to see your containers, please type on the command line
You will see your running containers. If nothing is listed, this means that there is no container running at the current moment. Please type
docker ps -a
If you haven’t created any container, this list will be empty too. Don’t worry, when we create our image, we also will create our container. Now it is time to create our image.
As i said, the images can be created manually. This is what we will do. If you want to create an image, you have to be familiar with dockerfiles. A dockerfile is a file that keeps the necessary information about associated image and container. And it has a specific language for its own. For example, there is a RUN command that runs the application or file that is typed. There are many commands for dockerfile but we will be dealing with only a few of them.
In our example, i will try to run a JSF application which is configured with a tomcat server from a docker container. Now we need the .war file of our application. You can use ant build to get your file or you can create it from the command line of the compiler or you can execute a goal for building it. My compiler is intellij idea but that is pretty the same for the other Java compilers. If you are using idea, please type
execute maven goal
in the search button. Make sure the checkbox that says “include non-project items” is selected. Select it. Make sure that your project path is typed in the first line. In second line, type
After the execution, go to your target class. You will see a war file. If that is not created, try to see if there is any error by typing
mvn build tomcat7
Look at the rightest side of the window. You will see “Maven Projects“. There are options like clean, install and compile. Try to find the error by double clicking those options. In the command line or in the logs, you can find the errors easily.
Now we need to copy that war file into tomcat files. Please open your tomcat folder. It is in the location where you installed it. In that folder, try to find webapps. This is the folder where you will copy your war file. But from the interface that is not possible you have to do this operation from command line. Please type in the command line
cp <your .war file location> <your webapps location>
Before you ensure that the war file is there, please do not continue. This is a must.
Now it is time to create our image. Please type in the command line
sudo nano Dockerfile
there will an empty page appear if you haven’t created a dockerfile before. Below you can find my dockerfile.
FROM command is to include the base image from public repositories(since docker is an open-source software).This command is a must.
RUN command is to run the necessary commands for the container. In my file, i am trying to run tomcat and the war file. Just like writing in the command line.
EXPOSE is to allocate the necessary port. Since we are running a web application, it is 8080.
ENV is for assignments. Sets the http port to 8080.
WORKDIR is to give an appropriate path to application.
CMD is to execute the container. There can only one cmd command in a dockerfile. If you insert more than one, the last one will be executed(tomcat is run from catalina.sh).
Now it is time to create our image from that dockerfile. Please type
docker build -t <name_of_your_image> .
Please don’t forget to put a dot at the end of the command. You must eventually see “Succesfully build <id_of_the_image>”
Next, we need to create our container. Please type
docker run -p <a_port_number>:8080 -d <name_of_your_image>
Instead of <a_port_number> we could have wrote 8080, but since it is allocated by tomcat, we need to assign a different port. Now type docker ps -a to see your container. It must be something like in the following picture;
Look at the top-most container. It is the port that i am running. For now we are done.
Without docker, we can run our application with localhost:8080. When it is docker, we need to use docker’s ip address.
in the command line. At the top-most, you must be seeing a docker part. You can see docker ip.
Open your web browser now. Please type
You should be seeing your application there.My output is something like that ( It is an application that gets the users for radius server as input and lists them. You can see that application in https://github.com/aktasrozerin/jsfapplication ) Yours should be seen as following;
If there is any error, i would be happy to help. That is the way we deploy our applications in a better and efficient way with docker.
That is all for this article, hope to meet you again and enjoy your coding!