In this tutorial you'll run your first Liberty server under Docker using the websphere-liberty image that is available from the online Docker Hub repository.
-
Open the Docker Quickstart Terminal on Windows or a Mac terminal.
-
Populate the local image cache with WebSphere Liberty by using the following command to pull it down from Docker Hub:
$ docker pull websphere-liberty
This will pull down the layers that make up the image. The total image size, including the base Ubuntu image on which it builds, is just over 500MB.
-
Execute the following command to see the layers that make up the Liberty image:
$ docker history websphere-libertyThe layers represent the commands that were issued when the image was built (specify the --no-trunc option if you want to see the full commands). The bottom layers represent the
ubuntuimage. After setting the maintainer and installing wget and unzip, the Dockerfile downloads and installs an IBM JRE and the WebSphere Liberty kernel. It then creates a server and then sets the metadata around the image such as the ports to expose and the command to run. It then adds additional features using theinstallUtilitycommand to build up to the full set of Java EE7 features. -
You can see the total size of the image by running the command:
$ docker images websphere-liberty
-
Run the image with the following command (the
-iand-toptions specify that an interactive terminal is required):$ docker run -i -t websphere-liberty
-
The server isn't much use though as we didn't expose any ports to access it. Type
Ctrl-Cto stop the server. (If you failed to specify the-i -toptions then you will have to kill the process from another terminal.) -
This time map the port 9080 from the container to port 80 on the host virtual machine:
$ docker run -d -p 80:9080 --name wlp websphere-liberty
Note that this time you have specified the
-doption to run the container in the background, and we have given it a name so that we can subsequently refer to it by this name rather than using the generated name or id. -
Use the following command to follow the logs of the container as it starts up:
$ docker logs -f wlp
Once the server is ready to run, type
Ctrl-Cto return to the command prompt. -
Open a web browser and enter the IP address of the machine running the container to view the Liberty welcome page.
- This will be
localhostif you are running on your laptop, or, if you are running on a VM, the IP address of the VM.
- This will be
-
Return to the terminal and enter the following command to see the processes running in the container:
$ docker top wlp
You will see the
serverscript and the child Java process. -
Docker permits us to run other processes inside the namespaces of an existing container. This is primarily used for diagnostic purposes.
-
For example, type the following command to start an interactive session inside the running container:
$ docker exec -it wlp bashYou will see the command prompt change indicating that you are now running as the root user within the container.
-
Execute the following command to show all of the processes running within the container namespace.
$ ps aux
Note that within the container you do not have visibility of other processes running on the host.
-
Execute the following command inside the container to view the contents of the server log file:
$ cat /logs/messages.log
-
Enter the following command to end the terminal session. The container will continue to run.
$ exit
-
-
It is also possible to copy files out of the container.
-
For example type the following command to copy the messages.log file from the container
wlpto the current directory:$ docker cp wlp:/logs/messages.log . -
Then run execute the following command to view the file:
$ cat messages.log
-
-
Using
docker inspect wlpreveals lots more interesting information about the running container. The command accepts filters in the golang template format so, for example, it is possible to retrieve just the IP address for the container using:$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' wlp -
Finally, run the following commands to clear up the container:
$ docker stop wlp $ docker rm wlp
Congratulations on completing this tutorial. You may now like to look at the tutorial on Adding an Application.