About Hariharan Narayanan

Hari graduated from the School of Computer and Information Sciences in the University of Hyderabad. Over his career he has been involved in many complex projects in mobile applications, enterprise applications, distributed applications, micro-services, and other platforms and frameworks. He works as a consultant and is mainly involved with projects based on Java, C++ and Big Data technologies.

Connect to Docker Container Example

1. Introduction

This example introduces how to create a Docker container and connect to it. The previous posts discussed creating basic Hello World containers and understanding your Docker installations. This post carries it further to talk about working with containers. This post assumes that you have a working Docker installation. Let’s start

2. Understanding Docker container and image

A Docker container is based on a Docker image. A Docker image is based on a definition provided in a Dockerfile. Consider a Docker image as a read-only snapshot of a Docker Container. The Docker container adds a writable file-system layer on top of a Docker image to make the container usable. Let us understand this a little bit more by examining the official openjdk version 8u111 Docker image. What does its Dockerfile say?

So you see how Docker creates layers for every image that you create or pull. Let us see how to create a Docker container and learn further about the containers-and-images story

3. Start a new Docker container from a new image

A new Docker image is created using a Dockerfile. As an example, let us create a new Docker image from the openjdk image we pulled earlier. We will create a Dockerfile that will setup a Helloworld class and execute it. This is the Dockerfile.

Next create a image from this Dockerfile and examine how many new layers are created.

$ docker build -t java-hello-world .

$ docker history java-hello-world:latest

Output of “docker history java-hello-world:latest”

As can be seen 4 more layers have been added to the previous openjdk:latest image. Execute a few commands using the java-hllo-world:latest image and finally see what containers are created.

$ docker run java-hello-world:latest

$ docker run java-hello-world:latest javac HelloWorld.java

$ docker run java-hello-world:latest java -version

$ docker ps --all

Output of “docker ps –all”

We see that for every invocation of the openjdk:latest image there is a new container created.

Docker containers concept

Next, we will see next how to connect to a running container.

4. Connect to an existing Docker container

In the previous section we saw that Docker creates a new container for every new command that we execute using an image. But this need not be so. One can also connect to an existing container provided that the container is running. A container gows into stopped state once it is done executing the command it was assigned either through the CMD command in Dockerfile or through a command given through docker exec. So how can we create a Docker container that will remain in running state?

4.1 Create a Docker container that will remain in running state

A Docker container executing a server-side component will always remain in running state. For a simple example, create a Docker container that runs a infinite loop so it remains in running state forever (or until explicitly stopped). Create a new folder and write the code below in that folder.

For that, we write a simple shell script – infinite.sh – that runs an infinite loop like so

#!/bin/sh
#infinite.sh
i=0
while [ 1 ]; do
i=1
done

Write a simple Dockerfile – called Dockerfile of course – that uses this script

You may use the docker images to verify that the image infinite-loop-alpine:latest is created. Run the image now to see that it creates a perpetually running container

$ docker run infinite-loop-alpine:latest

Output of docker build and docker run

If it ran fine, the above command must lock the terminal in a foreground task. You can check if the command succeeded by checking the running containers in a new terminal

$ docker ps

Output of currently running containers

To run the container as a background task instead, it must be started as a daemon. This is how it can be done

$ docker run --detach infinite-loop-alpine:latest

Output of running a docker container in detached mode

Notice that calling docker run like this does not lock the terminal anymore.

4.2 Connect to the daemon container

Connecting to a running container is simple. Use the docker exec command for this like below

$ docker exec --interactive --tty stupefied_lalande /bin/sh

--interactive opens an interactive STDIN input to the container--tty opens a terminal to the containernauseous_wing is the name of the container to connect to/bin/sh is the command to be executed once connected to the container

Connecting to a container in interactive mode

You can see that a new shell has opened to the container. Running ps on the prompt will show that the script infinite.sh is already running with PID 1. This shall always remain the primary process since it is specified in the Dockerfile through the CMD command. A new shell process /bin/sh is also added due the docker exec command above apart from the ps command of course

This is how one connects to a running container using the Docker CLI. There are other ways too, like docker-compose for instance, which we will probably explore in more detail in another post

5. Summary

In this example we understood the concepts behind how Docker spawns containers from an image. Later we saw how we can create a container and run it in the background so that we can connect to it whenever needed

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.