Containers & Docker

Let’s get right to it.

Install Docker

(This is  for Linux. See the Windows or Mac OS X version)

Get the latest Docker package by typing/copying this code on your Terminal:

$ sudo wget -qO- https://get.docker.com/ | sh

 

Run “Hello World” Example

After installing Docker, try running this simple code:

$ sudo docker run hello-world

Output:

As the output says the docker daemon “downloaded” hello-world image then loaded the image into the container and “ran” it.

 

Theory

And now for the boring parts -lol.

Docker

Docker allows you to package an application with all of its dependencies into a standardized unit. It manages containers that wrap up a piece of software in a complete filesystem which contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. Think of it as a shawarma – with all the juicy ingredients kept intact by a Panini press (docker). This guarantees that it will always run the same, regardless of the environment it is running in.

Images

The build component of Docker, which consists of a read-only template of the application operating system. For example, an image could be the Fedora operating system with WildFly and your Java EE application installed. You can easily create new images or update existing ones.

Container

A runtime representation created from images. Containers are the run component of Docker. They can be run, started, stopped, moved, and deleted. Each container is an isolated and secure application platform2.

 

Running an Image

Now try to run an image from the docker hub (Docker’s distribution component, where images are uploaded and downloaded).

  • In a terminal start docker daemon:

$ sudo docker daemon

  • Run the container in the new terminal by typing

$ docker run docker/whalesay cowsay boo

You can replace “boo” with any string of your choice.

This basically runs a container with the whalsay image.

You can always search the Docker’s Registry for containers and also create your repository to push images, pull others or even use official images.

Now, run a container with the glassfish image and access the default page on a web browser (Hint: check for the docker ip using $ sudo docker network inspect bridge):

$ docker run glassfish

Build your own Image

Now let’s build our own image. We’re going to create an image and deploy a sample application on a tomcat server.

Steps:

  1. Write a Dockerfile

A Dockerfile contains all the commands needed to build the image. It’s more like the recipe needed to “bake” an image 😊

Create a text file Dockerfile

Add the following line to the file:

1 FROM tomcat
2 ADD http://tomcat.apache.org/tomcat-9.0-doc/appdev/sample/sample.war $CATALINA_HOME/webapps/
3 CMD [“catalina.sh”, “run”]

 

The first line uses the FROM keyword to tell Docker which image your image is based on.

Line 2 using the ADD keyword copies sample. war file from apache’s website to the webapps directory in your container(i.e Ttomcat’s deployment directory).

Line 3 instructs the tomcat to run when the image is loaded.

  • Build  Image

Navigate to the directory that has the Dockerfile and build the new image by typing $ sudo docker build -t tomcat-sample .  in your terminal (don’t forget the . period). The image could be named anything although here it was given tomcat-sample . The . indicates that the file with the instructions to build the image is in the current directory.

Notice how it runs each line step by step. What it does is to load the base image then executes the next line adding to the image and the next till it gets to the CMD line.

  • Run Image

Running $ sudo docker images Will show you all the images available locally.

Run the new image typing:

$ sudo docker run –rm -it -p 8080:8080 tomcat-sample

  • –rm removes the container as soon as you stop it
  • -p forwards the port to your host
  • -it allows interactive mode, so you see if something get’s deployed

Now on a web browser access the application from http://docker-ip:8080/sample (Note that you can map any host to the docker-ip e.g docker as in the image below).

 

Comments

comments