Handy Docker Commands and Links to Start With (Tomcat Example)

Dafna Rosenblum
Extend
Published in
4 min readAug 28, 2017

--

Six months ago I wanted to learn to use Docker. I started with watching this Introduction to Docker talk, given by dotCloud founder (where Docker started as a side project). That’s a great start and overview of the cause and vision.

Then I went to the official documentation and started following it, especially the Learn by example section, but after a while, it felt too slow and out of focus. Nonetheless, here’s a summary of the commands described there:

After downloading the Docker installation and running it, a whale icon is displayed in the top bar, and you’re able to run Docker commands from anywhere.

Some commands to start with

docker run ubuntu

Will Download the ubuntu image from Docker Hub (unless you already have it), and run it — will create docker container from the image and run the container.

docker ps

Will show you the active containers. If you run this command right after running docker run ubuntu, you won’t see any record, because no command was specified for the container.

This command will open a bash client on the container:

docker run -t -i ubuntu /bin/bash

And if you run docker ps on a different tab, you will see the details of this container, such as ID, the image it’s based on, and more. Running exit on the bash client will stop the container, and then you can see it only if you run:

docker ps -a

Which shows also stopped containers. And you can delete the container using:

docker rm f4fb45d24b49 4d3583b79bac

Every string following the rm here is a container’s ID (I chose to delete two containers in this example). You can also delete the image itself using:

docker rmi f753707788c5

The string following the rmi is the Image ID. You cannot delete an image if there are containers based on it (even stopped).

docker images

Will show you all the images available on your host. If you want to download only, without running, you can use:

docker pull ubuntu

More Resources

Later I’ve watched a bit of this taped official tutorial, and since I wanted to develop a Java server, I looked for “deploying tomcat on docker best practice” on Google and found this nice post. I needed a tomcat running on a container with my code, so I decided to use Tomcat image.

Creating New Image with Dockerfile

To create a new image, one needs to create a Dockerfile. The Dockerfile looks like that:

FROM docker/whalesay:latestRUN apt-get -y update && apt-get install -y fortunes

Building a new image from the docker file is done by running:

docker build -t <the name you choose for your new image> .

The dot represents the path to the Dockerfile.

The Dockerfile mentions the Image that we build upon, and the changes we add to it.

Searching the Hub

sudo docker search tomcat

Will help to find Tomcat official repository on Docker Hub. Run:

docker run -it --rm -p 8888:8080 tomcat

The output is Tomcat starting status, ending with org.apache.catalina.startup.Catalina.start Server startup in 807 ms, and http://localhost:8888/ will load the default HTML that tomcat returns.

Theory

Later I’ve watched a few short YouTube videos about Docker and it was really great. Watched on 1.5 speed, which was very comfortable.

Learn Docker in 12 Minutes of Jake Wright is an excellent explanation. Here are the main things I’ve learned from it:

Three advantages of Docker:

  1. Run the same stack on every env. No more ”it works for me”.
  2. Sandbox projects — isolation.
  3. Get quickly into someone else’s project (no installation).

It’s not a VM, there’s no Hypervisor nor VM Kernel. Hence it uses less memory, fewer resources, and starts quicker.

A container is a running instance of an image. An image we’re building our image on top, can be an extension of another image. The Dockerfiles are piled on one another. This is what makes the deployment light — it’s only downloading the difference every time.

Ports and File System

Docker run -p 80:81 ...

Means port 80 on the host is mapped to port 81 on the container.

To copy code to the container, add to the Dockerfile the text:

COPY <from path> <to path>

But without using volumes, changing the code won’t affect the container, and we’ll have to rebuild and rerun. One of the options to solve this is to mount a volume from the host to the container. For that, add to docker run command the following:

-v <from path>:<to path>

It’s only a view, it’s not a copy. If the container runs on another host, the code won’t be there.

More Resources about Docker

Other interesting and rather short videos I’ve seen: What is Docker? of CBT Nuggets and this ignite on Docker called Docker in 5 minutes of Opensource.com by Vincent Batts.

Apache Tomcat

I found this documentation of sample Apache Tomcat application, which allows downloading a War file. According to the sample application documentation, I should put the War I downloaded under CATALINA_BASE/webapps. It will be automatically expended and deployed, available at http://localhost:8080/sample.

The official repository of Tomcat on Docker Hub says CATALINA_BASE and CATALINA_HOME are located in this path: /usr/local/tomcat. This is also printed to the console when running the container.

docker run -it --rm -p 8888:8080 -v /Users/myUser/Personal/Docker/tomcat/sample.war:/usr/local/tomcat/webapps/sample.war tomcat

Running the above command will make the app available from the browser.

--

--

Dafna Rosenblum
Extend

Engineering Manager at Volvo Cars. Co-Founder of Baot.org.