The tutorial Running WebSphere Liberty Containers described how to run one or more Liberty servers under Docker. In this tutorial you will add an application to the mix. There are a number of different approaches to running an application on the Liberty Docker Hub image which you'll explore in this section of the lab.
The first method is simply to mount an application or directory from the host when the container is started.
-
Change down in to the
app
directory containing the README for this tutorial:$ cd app
-
Use the following command to run a container with WebSphere Liberty:
$ docker run -d -p 80:9080 --name=app -v $PWD/ServletApp.war:/config/dropins/app.war websphere-liberty
The important part of the command is the
-v
option which indicates that the ServletApp.war from the directory on the Docker host should be mounted in to thedropins
directory of the Liberty server configuration. You need to specify the full pathname to ServletApp.war on your host machine. -
Use the following command to watch the server start:
$ docker logs --tail=all -f app
You should see log entries indicating that the application has been started.
-
Use this docker command to check the IP address the server is on:
$ docker ps
The output should look similar to this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aaefa5315ed2 websphere-liberty "/opt/ibm/docker/d..." About a minute ago Up About a minute 9443/tcp, 0.0.0.0:80->9080/tcp upbeat_fermi
The section under ports will provide the IP Address (0.0.0.0 in this scenario.)
-
View the running application by opening a web browser to either localhost, if running on your local machine, or the IP address of your VM, if running on a remote VM, followed by '/app'.
-
Clean up the server with the following commands:
$ docker kill app $ docker rm app
The approach above is fine for development purposes, allowing you to rapidly update the application but mounting a file from the host breaks the portability that is often a driver to use Docker in the first place. The next approach is to build an image with the application contained within a layer.
-
In the current directory, list the contents of
Dockerfile
with the following command:$ cat Dockerfile
It simply starts from the
websphere-liberty
image and adds the ServletApp.war from the current directory in to thedropins
directory. -
Build an image tagged
app
using the current directory as context using the command:$ docker build -t app .
-
To run the newly built image, use the command:
$ docker run -d -p 80:9080 --name=app app
-
Repeat steps 4-6 from the previous section to watch the server start, verify that it is running, and then clean up.
The approach from the previous section means that you have neatly packaged a single image that contains everything needed to run the application. It does, however, mean that if we want to move up to a new version of the Liberty image we have to rebuild our application as well. The following approach using a data volume container to separate the application and the server runtime in two separate images.
-
Run the image that you created in the previous section with the following command:
$ docker run -v /config --name=app app true
This time, the
-v
flag is being use to export a volume from the server. The last parameter oftrue
is the command that the container will run. This will exit immediately leaving you with a stopped container that exposes the server configuration containing the application. Note: You have just reused the existing image for convenience here. You are not going to use the Liberty server runtime in it and could equally have used an image created fromubuntu
. -
Now run the following command to start a Liberty container:
$ docker run --volumes-from app -d -p 80:9080 websphere-liberty
The key option this time is the
--volumes-from
flag which indicates that the volumes exported by the containerapp
should be mounted on this container. -
Once again, use the following command to watch the server start (where the
docker ps -lq
command returns the ID of the last container run):$ docker logs --tail=all -f $(docker ps -lq)
You should see log entries indicating that the application has been started.
-
Repeat step 4 in
Mounting an application on the image
to find the IP address of the server -
View the running application by opening a web browser at this IP address followed by '/app'
-
Clean up the server and the two containers with the following commands:
$ docker kill $(docker ps -lq) $ docker rm $(docker ps -aq)
-
Remove the application image with the command:
$ docker rmi app
Congratulations on completing this tutorial. You may now like to look at the tutorial on Working with Related Containers.