Skip to content

Latest commit

 

History

History
340 lines (251 loc) · 10.5 KB

ch04-run-container.adoc

File metadata and controls

340 lines (251 loc) · 10.5 KB

Run a Docker Container

The first step in running any application using Docker is to run a container from an image. There are plenty of images available from the official Docker registry (aka Docker Hub). To run any of them, you just have to ask the Docker Client to run it. The client will check if the image already exists on Docker Host. If it exists then it’ll run it, otherwise the host will download the image and then run it.

Pull Image

Let’s first check, if any images are available:

docker images

At first, this list is empty. If you’ve already downloaded the images as specified in the Setup Environments chapter, then all the images will be shown here. Now, let’s get a vanilla jboss/wildfly image:

docker pull jboss/wildfly

By default, docker images are retrieved from Docker Hub.

You can see, that Docker is downloading the image with it’s different layers.

List of images can be seen again using the docker images command. This will see the following output:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-java          latest              428f7ac0195d        2 hours ago         669.2 MB
helloworld          2                   f74665e81a75        2 hours ago         1.093 MB
helloworld          latest              a85376e122e2        2 hours ago         125 MB
ubuntu              latest              cf62323fa025        5 days ago          125 MB
jboss/wildfly       latest              4a2f70767057        12 days ago         580.2 MB
busybox             latest              2b8fd9751c4c        2 weeks ago         1.093 MB
java                latest              264282a59a95        4 weeks ago         669.2 MB

More details about the image can be obtained using docker history jboss/wildfly command:

docker history jboss/wildfly
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4a2f70767057        12 days ago         /bin/sh -c #(nop) CMD ["/opt/jboss/wildfly/bi   0 B
<missing>           12 days ago         /bin/sh -c #(nop) EXPOSE 8080/tcp               0 B
<missing>           12 days ago         /bin/sh -c #(nop) ENV LAUNCH_JBOSS_IN_BACKGRO   0 B
<missing>           12 days ago         /bin/sh -c cd $HOME     && curl -O https://do   160.8 MB
<missing>           12 days ago         /bin/sh -c #(nop) ENV JBOSS_HOME=/opt/jboss/w   0 B
<missing>           12 days ago         /bin/sh -c #(nop) ENV WILDFLY_SHA1=c0dd7552c5   0 B
<missing>           12 days ago         /bin/sh -c #(nop) ENV WILDFLY_VERSION=10.0.0.   0 B
<missing>           12 days ago         /bin/sh -c #(nop) ENV JAVA_HOME=/usr/lib/jvm/   0 B
<missing>           12 days ago         /bin/sh -c #(nop) USER [jboss]                  0 B
<missing>           12 days ago         /bin/sh -c yum -y install java-1.8.0-openjdk-   197.4 MB
<missing>           12 days ago         /bin/sh -c #(nop) USER [root]                   0 B
<missing>           12 days ago         /bin/sh -c #(nop) MAINTAINER Marek Goldmann <   0 B
<missing>           12 days ago         /bin/sh -c #(nop) USER [jboss]                  0 B
<missing>           12 days ago         /bin/sh -c #(nop) WORKDIR /opt/jboss            0 B
<missing>           12 days ago         /bin/sh -c groupadd -r jboss -g 1000 && usera   4.349 kB
<missing>           12 days ago         /bin/sh -c yum update -y && yum -y install xm   25.21 MB
<missing>           12 days ago         /bin/sh -c #(nop) MAINTAINER Marek Goldmann <   0 B
<missing>           12 days ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0 B
<missing>           12 days ago         /bin/sh -c #(nop) LABEL name=CentOS Base Imag   0 B
<missing>           12 days ago         /bin/sh -c #(nop) ADD file:170ecf2c9223a8a00d   196.8 MB
<missing>           12 days ago         /bin/sh -c #(nop) MAINTAINER https://github.c   0 B

Run Container Interactively

Run WildFly container in an interactive mode.

docker run -it jboss/wildfly

This will show the output as:

=========================================================================

  JBoss Bootstrap Environment

  JBOSS_HOME: /opt/jboss/wildfly

  JAVA: /usr/lib/jvm/java/bin/java

. . .

23:38:38,730 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-4) ISPN000128: Infinispan version: Infinispan 'Mahou' 8.1.0.Final
23:38:39,007 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
23:38:39,007 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
23:38:39,008 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.0.0.Final (WildFly Core 2.0.10.Final) started in 4377ms - Started 267 of 553 services (371 services are lazy, passive or on-demand)

This shows that the server started correctly, congratulations!

By default, Docker runs in the foreground. -i allows to interact with the STDIN and -t attach a TTY to the process. Switches can be combined together and used as -it.

Hit Ctrl+C to stop the container.

Run a Detached Container

Restart the container in detached mode:

docker run -d jboss/wildfly
119c9149e270dd511eff23c2ce14012eea645f2226e1855b5bc4758bdef6357e

-d, instead of -it, runs the container in detached mode.

The output is the unique id assigned to the container. Check the logs using docker logs <CONTAINER_ID> command. <CONTAINER_ID> is the id of the container.

We can check the status of the container by docker ps command:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
119c9149e270        jboss/wildfly       "/opt/jboss/wildfly/b"   20 seconds ago      Up 19 seconds       8080/tcp            elegant_euler

Also try docker ps -a to see all the containers on this machine.

Run Container with Default Port

If you want containers to accept incoming connections, you will need to provide special options when invoking docker run. The container, we just started, can’t be accessed by our browser. We need to stop it again and restart with different options.

docker stop `docker ps | grep wildfly | awk '{print $1}'`

Restart the container as:

docker run -d -P jboss/wildfly

-P map any exposed ports inside the image to a random port on Docker host. This can be verified using docker ps command:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
cc7bb18fc05e        jboss/wildfly       "/opt/jboss/wildfly/b"   6 seconds ago       Up 5 seconds        0.0.0.0:32768->8080/tcp   happy_ptolemy

The port mapping is shown in the PORTS column. Access WildFly server at http://localhost:32768. Make sure to use the correct port number as shown in your case.

Note
Exact port number may be different in your case.

The page would look like:

wildfly first run default page

Run Container with Specified Port

Stop the previously running container as:

docker stop `docker ps | grep wildfly | awk '{print $1}'`

Restart the container as:

docker run -d -p 8080:8080 jboss/wildfly

The format is -p hostPort:containerPort. This option maps container ports to host ports and allows other containers on our host to access them.

Now we’re ready to test http://localhost:8080 again. This works with the exposed port, as expected.

Let’s stop the container as:

docker stop `docker ps | grep wildfly | awk '{print $1}'`

Deploy a WAR file to Application Server

Now that your application server is running, lets see how to deploy a WAR file to it.

Use the following Dockerfile in a new directory:

FROM jboss/wildfly

RUN curl -L https://github.com/javaee-samples/javaee7-simple-sample/releases/download/v1.10/javaee7-simple-sample-1.10.war -o /opt/jboss/wildfly/standalone/deployments/javaee-simple-sample.war

Create an image:

docker build -t javaee-sample .

Start the container:

docker run -d -p 8080:8080 javaee-sample

Access the endpoint:

curl http://localhost:8080/javaee-simple-sample/resources/persons

See the output:

<persons>
	<person>
		<name>
		Penny
		</name>
	</person>
	<person>
		<name>
		Leonard
		</name>
	</person>
	<person>
		<name>
		Sheldon
		</name>
	</person>
	<person>
		<name>
		Amy
		</name>
	</person>
	<person>
		<name>
		Howard
		</name>
	</person>
	<person>
		<name>
		Bernadette
		</name>
	</person>
	<person>
		<name>
		Raj
		</name>
	</person>
	<person>
		<name>
		Priya
		</name>
	</person>
</persons>

Optional: brew install XML-Coreutils will install XML formatting utility on Mac. This output can then be piped to xml-fmt to display a formatted result.

Stop Container

Stop a specific container:

docker stop <CONTAINER ID>

Stop all running containers:

docker stop $(docker ps -q)

Stop only the exited containers:

docker ps -a -f "exited=-1"

Remove Container

Remove a specific container:

docker rm <CONTAINER_ID>

Remove containers meeting a regular expression

docker ps -a | grep wildfly | awk '{print $1}' | xargs docker rm

Remove all containers, without any criteria

docker rm $(docker ps -aq)

Additional Ways To Find Port Mapping

The exact mapped port can also be found using docker port command:

docker port <CONTAINER_ID>

This shows the output as:

8080/tcp -> 0.0.0.0:32786

Port mapping can be also be found using docker inspect command:

docker inspect --format='{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' <CONTAINER ID>