
A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).
The docker pull command can be used to pull down the latest Tomcat image.
~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
bb7d5a84853b: Pull complete
f02b617c6a8c: Pull complete
d32e17419b7e: Pull complete
c9d2d81226a4: Pull complete
fab4960f9cd2: Pull complete
da1c1e7baf6d: Pull complete
79b231561270: Pull complete
7d337880d8b4: Pull complete
2df65a31be06: Pull complete
10cbf519de23: Pull complete
Digest: sha256:7c30d3c92b191ec2b84f64be99326f9c83a4a4e079c899ee4d0a9a41abca8406
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
Or you could create Dockerfile so that the Dockerfile contains something like this.
FROM tomcat:latest
Then use the docker build command to create the image, running this command in the same directory as the Dockerfile.
docker build . --tag tomcat:latest
The docker images command can be used to display the Tomcat image.
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 4ce9babdd885 7 days ago 680MB
The following command can then be used to create and start the Tomcat container.
Let's break down this command.
- The docker run command is used to create and start the Tomcat container.
- The --detach flag is used to run the container in the background.
- The --publish option is used to configure both the Docker server and Tomcat container to listen on TCP port 8080, which adds a rule to iptables to allow connections between the Docker system and container on port 8080.
- The --name option is used to name the container tomcat.
- The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
- The tomcat image is used.
docker run --detach --publish 8080:8080 --name tomcat --restart unless-stopped tomcat
Use the docker container ls command to ensure the container is running.
~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24d6d3fa99a3 tomcat "catalina.sh run" 5 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat
You should now be able to navigate to http://<hostname or IP address of your Docker system>:8080 and 404 (not found) should be returned by Tomcat, which shows that Tomcat is up and running and listening on port 8080. You can now deploy a WAR to Tomcat on Docker.
If for some reason Tomcat is not running, you can try to issue the catalina.sh run command in the container.
docker container exec tomcat catalina.sh run
Did you find this article helpful?
If so, consider buying me a coffee over at