{"id":41705,"date":"2016-11-15T15:00:17","date_gmt":"2016-11-15T13:00:17","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=41705"},"modified":"2019-03-29T14:14:48","modified_gmt":"2019-03-29T12:14:48","slug":"connect-docker-container-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/","title":{"rendered":"Connect to Docker Container Example"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p>This example introduces how to create a Docker container and connect to it. The previous posts discussed <a href=\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/docker-hello-world-example\/\">creating basic Hello World containers<\/a> 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.&nbsp; Let&#8217;s start<\/p>\n<h2>2. Understanding Docker container and image<\/h2>\n<p>A Docker container is based on a Docker image.&nbsp; A Docker image is based on a definition provided in a Dockerfile.&nbsp; Consider a Docker image as a read-only snapshot of a Docker Container.&nbsp; The Docker container adds a writable file-system layer on top of a Docker image to make the container usable.&nbsp; Let us understand this a little bit more by examining the official <a href=\"https:\/\/hub.docker.com\/_\/openjdk\/\">openjdk version 8u111<\/a> Docker image.&nbsp; What does its Dockerfile say?<\/p>\n<p>&nbsp;<br \/>\n<a href=\"https:\/\/github.com\/docker-library\/openjdk\/blob\/e6e9cf8b21516ba764189916d35be57486203c95\/8-jdk\/Dockerfile\">openjdk\/8-jdk\/Dockerfile<\/a> <em>(Removed comments from original file for brevity)<\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false\">FROM buildpack-deps:jessie-scm\n\nRUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \\\n\t\tbzip2 \\\n\t\tunzip \\\n\t\txz-utils \\\n\t&amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*\n\nRUN echo 'deb http:\/\/deb.debian.org\/debian jessie-backports main' &gt; \/etc\/apt\/sources.list.d\/jessie-backports.list\n\nENV LANG C.UTF-8\n\nRUN { \\\n\t\techo '#!\/bin\/sh'; \\\n\t\techo 'set -e'; \\\n\t\techo; \\\n\t\techo 'dirname \"$(dirname \"$(readlink -f \"$(which javac || which java)\")\")\"'; \\\n\t} &gt; \/usr\/local\/bin\/docker-java-home \\\n\t&amp;&amp; chmod +x \/usr\/local\/bin\/docker-java-home\n\nENV JAVA_HOME \/usr\/lib\/jvm\/java-8-openjdk-amd64\n\nENV JAVA_VERSION 8u111\nENV JAVA_DEBIAN_VERSION 8u111-b14-2~bpo8+1\n\nENV CA_CERTIFICATES_JAVA_VERSION 20140324\n\nRUN set -x \\\n\t&amp;&amp; apt-get update \\\n\t&amp;&amp; apt-get install -y \\\n\t\topenjdk-8-jdk=\"$JAVA_DEBIAN_VERSION\" \\\n\t\tca-certificates-java=\"$CA_CERTIFICATES_JAVA_VERSION\" \\\n\t&amp;&amp; rm -rf \/var\/lib\/apt\/lists\/* \\\n\t&amp;&amp; [ \"$JAVA_HOME\" = \"$(docker-java-home)\" ]\n\nRUN \/var\/lib\/dpkg\/info\/ca-certificates-java.postinst configure\n<\/pre>\n<p>Docker builds an image in layers. To understand this, pull the openjdk image locally and see what Docker does.<\/p>\n<p><figure id=\"attachment_41838\" aria-describedby=\"caption-attachment-41838\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-pull-openjdk-8u111-01.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41838\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-pull-openjdk-8u111-01.jpg\" alt=\"Output of docker pull\" width=\"860\" height=\"284\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-pull-openjdk-8u111-01.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-pull-openjdk-8u111-01-300x99.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/01-docker-pull-openjdk-8u111-01-768x254.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41838\" class=\"wp-caption-text\">Docker builds an image in several layers<\/figcaption><\/figure><\/p>\n<p>From the above image, it can be seen that Docker pulled the image in several layers.&nbsp; You can explore what goes into each of these layers by using <code>docker history<\/code> command<\/p>\n<p><figure id=\"attachment_41839\" aria-describedby=\"caption-attachment-41839\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-history-openjdk-8u111-01.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41839\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-history-openjdk-8u111-01.jpg\" alt=\"Output of docker history openjdk:8u111\" width=\"860\" height=\"221\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-history-openjdk-8u111-01.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-history-openjdk-8u111-01-300x77.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/02-docker-history-openjdk-8u111-01-768x197.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41839\" class=\"wp-caption-text\">Output of &#8220;docker history openjdk:8u111&#8221;<\/figcaption><\/figure><\/p>\n<p>What do you see? The <code>openjdk:8u111<\/code> image has 14 layers. These layers are listed in the below table. The details for these layers can be also be <a href=\"https:\/\/hub.docker.com\/r\/library\/openjdk\/tags\/8u111\/\">seen at Dockerhub<\/a><\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th><strong>Dockerfile line<\/strong><\/th>\n<th><strong>Produces layer<\/strong><\/th>\n<th><strong>Sized<\/strong><\/th>\n<\/tr>\n<tr>\n<td><code>FROM buildpack-deps:jessie-scm<\/code><\/td>\n<td><code>ADD file:41ea5187c50116884c38d9ec51d920d79cfaeb2a61c52e07a97f457419a10a4f in \/<\/code><\/td>\n<td>123 mb<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>CMD [\"\/bin\/bash\"]<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \\<br \/>\nbzip2 \\<br \/>\nunzip \\<br \/>\nxz-utils \\<br \/>\n&amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*<\/code><\/td>\n<td><code>\/bin\/sh -c apt-get update &amp;&amp; apt-get install -y --no-install-recommends ca-certificates curl wget &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*<\/code><\/td>\n<td>44.28 mb<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>\/bin\/sh -c apt-get update &amp;&amp; apt-get install -y --no-install-recommends bzr git mercurial openssh-client subversion procps &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*<\/code><\/td>\n<td>122.6 mb<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>\/bin\/sh -c apt-get update &amp;&amp; apt-get install -y --no-install-recommends bzip2 unzip xz-utils &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*<\/code><\/td>\n<td>1.286 mb<\/td>\n<\/tr>\n<tr>\n<td><code>RUN echo 'deb http:\/\/deb.debian.org\/debian jessie-backports main' &gt; \/etc\/apt\/sources.list.d\/jessie-backports.list<\/code><\/td>\n<td><code>\/bin\/sh -c echo 'deb http:\/\/deb.debian.org\/debian jessie-backports main' &gt; \/etc\/apt\/sources.list.d\/jessie-backports.list<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>ENV LANG C.UTF-8<\/code><\/td>\n<td><code>ENV LANG C.UTF-8<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>RUN { \\<br \/>\necho '#!\/bin\/sh'; \\<br \/>\necho 'set -e'; \\<br \/>\necho; \\<br \/>\necho 'dirname \"$(dirname \"$(readlink -f \"$(which javac || which java)\")\")\"'; \\<br \/>\n} &gt; \/usr\/local\/bin\/docker-java-home \\<br \/>\n&amp;&amp; chmod +x \/usr\/local\/bin\/docker-java-home<\/code><\/td>\n<td><code>\/bin\/sh -c { echo '#!\/bin\/sh'; echo 'set -e'; echo; echo 'dirname \"$(dirname \"$(readlink -f \"$(which javac || which java)\")\")\"'; } &gt; \/usr\/local\/bin\/docker-java-home &amp;&amp; chmod +x \/usr\/local\/bin\/docker-java-home<\/code><\/td>\n<td>87 b<\/td>\n<\/tr>\n<tr>\n<td><code>ENV JAVA_HOME \/usr\/lib\/jvm\/java-8-openjdk-amd64<\/code><\/td>\n<td><code>ENV JAVA_HOME=\/usr\/lib\/jvm\/java-8-openjdk-amd64<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>ENV JAVA_VERSION 8u111<\/code><\/td>\n<td><code>ENV JAVA_VERSION=8u111<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code> ENV JAVA_DEBIAN_VERSION 8u111-b14-2~bpo8+1<\/code><\/td>\n<td><code> ENV JAVA_DEBIAN_VERSION=8u111-b14-2~bpo8+1<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>ENV CA_CERTIFICATES_JAVA_VERSION 20140324<\/code><\/td>\n<td><code>ENV CA_CERTIFICATES_JAVA_VERSION=20140324<\/code><\/td>\n<td>0 b<\/td>\n<\/tr>\n<tr>\n<td><code>RUN set -x \\<br \/>\n&amp;&amp; apt-get update \\<br \/>\n&amp;&amp; apt-get install -y \\<br \/>\nopenjdk-8-jdk=\"$JAVA_DEBIAN_VERSION\" \\<br \/>\nca-certificates-java=\"$CA_CERTIFICATES_JAVA_VERSION\" \\<br \/>\n&amp;&amp; rm -rf \/var\/lib\/apt\/lists\/* \\<br \/>\n&amp;&amp; [ \"$JAVA_HOME\" = \"$(docker-java-home)\" ]<\/code><\/td>\n<td><code>\/bin\/sh -c set -x &amp;&amp; apt-get update &amp;&amp; apt-get install -y openjdk-8-jdk=\"$JAVA_DEBIAN_VERSION\" ca-certificates-java=\"$CA_CERTIFICATES_JAVA_VERSION\" &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/* &amp;&amp; [ \"$JAVA_HOME\" = \"$(docker-java-home)\" ]<\/code><\/td>\n<td>351.5 mb<\/td>\n<\/tr>\n<tr>\n<td><code>RUN \/var\/lib\/dpkg\/info\/ca-certificates-java.postinst configure<\/code><\/td>\n<td><code>\/bin\/sh -c \/var\/lib\/dpkg\/info\/ca-certificates-java.postinst configure<\/code><\/td>\n<td>418.2 kb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>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<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2>3. Start a new Docker container from a new image<\/h2>\n<p>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.<\/p>\n<pre class=\"brush:bash\">FROM openjdk:8u111\nCOPY . \/usr\/src\/hello-world\nWORKDIR \/usr\/src\/hello-world\nRUN javac HelloWorld.java\nCMD [\"java\", \"HelloWorld\"]\n<\/pre>\n<p>Next create a image from this Dockerfile and examine how many new layers are created.<\/p>\n<pre class=\"brush:bash\">$ docker build -t java-hello-world .<\/pre>\n<pre class=\"brush:bash\">$ docker history java-hello-world:latest<\/pre>\n<p><figure id=\"attachment_41840\" aria-describedby=\"caption-attachment-41840\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-docker-history-java-hello-world.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41840\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-docker-history-java-hello-world.jpg\" alt=\"Output of docker history java-hello-world:latest\" width=\"860\" height=\"259\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-docker-history-java-hello-world.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-docker-history-java-hello-world-300x90.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/03-docker-history-java-hello-world-768x231.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41840\" class=\"wp-caption-text\">Output of &#8220;docker history java-hello-world:latest&#8221;<\/figcaption><\/figure><\/p>\n<p>As can be seen 4 more layers have been added to the previous <code>openjdk:latest<\/code> image. Execute a few commands using the <code>java-hllo-world:latest<\/code> image and finally see what containers are created.<\/p>\n<pre class=\"brush:bash\">$ docker run java-hello-world:latest<\/pre>\n<pre class=\"brush:bash\">$ docker run java-hello-world:latest javac HelloWorld.java<\/pre>\n<pre class=\"brush:bash\">$ docker run java-hello-world:latest java -version<\/pre>\n<pre class=\"brush:bash\">$ docker ps --all<\/pre>\n<p><figure id=\"attachment_41841\" aria-describedby=\"caption-attachment-41841\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-ps-all-01-02.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41841\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-ps-all-01-02.jpg\" alt=\"Output of docker ps --all\" width=\"860\" height=\"141\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-ps-all-01-02.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-ps-all-01-02-300x49.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/04-docker-ps-all-01-02-768x126.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41841\" class=\"wp-caption-text\">Output of &#8220;docker ps &#8211;all&#8221;<\/figcaption><\/figure>[ulp id=&#8217;6PVIvOz3kDbYmNRn&#8217;]<\/p>\n<p>We see that for every invocation of the <code>openjdk:latest<\/code> image there is a new container created.<\/p>\n<p><figure id=\"attachment_41842\" aria-describedby=\"caption-attachment-41842\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-containers-concept.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41842\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-containers-concept.jpg\" alt=\"Docker containers concept\" width=\"860\" height=\"430\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-containers-concept.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-containers-concept-300x150.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/05-docker-containers-concept-768x384.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41842\" class=\"wp-caption-text\">Docker containers concept<\/figcaption><\/figure><\/p>\n<p>Next, we will see next how to connect to a running container.<\/p>\n<h2>4. Connect to an existing Docker container<\/h2>\n<p>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 <code>CMD<\/code> command in Dockerfile or through a command given through <code>docker exec<\/code>. So how can we create a Docker container that will remain in running state?<\/p>\n<h3>4.1 Create a Docker container that will remain in running state<\/h3>\n<p>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.<\/p>\n<p>For that, we write a simple shell script &#8211; <code>infinite.sh<\/code> &#8211; that runs an infinite loop like so<\/p>\n<pre class=\"brush:bash\">#!\/bin\/sh\n#infinite.sh\ni=0\nwhile [ 1 ]; do \n    i=1\ndone\n<\/pre>\n<p>Write a simple Dockerfile &#8211; called <code>Dockerfile<\/code> of course &#8211; that uses this script<\/p>\n<pre class=\"brush:bash\">FROM alpine:latest\nCOPY infinite.sh .\nENTRYPOINT [\"sh\", \"infinite.sh\"]\n<\/pre>\n<p>Build a Docker image from this Dockerfile next<\/p>\n<pre class=\"brush:bash\">$ docker build --tag infinite-loop-alpine:latest .<\/pre>\n<p>You may use the <code>docker images<\/code> to verify that the image <code>infinite-loop-alpine:latest<\/code> is created. Run the image now to see that it creates a perpetually running container<\/p>\n<pre class=\"brush:bash\">$ docker run infinite-loop-alpine:latest<\/pre>\n<p><figure id=\"attachment_41843\" aria-describedby=\"caption-attachment-41843\" style=\"width: 791px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-docker-build-and-docker-run.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41843\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-docker-build-and-docker-run.jpg\" alt=\"Output of docker build and docker run\" width=\"791\" height=\"408\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-docker-build-and-docker-run.jpg 791w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-docker-build-and-docker-run-300x155.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/06-docker-build-and-docker-run-768x396.jpg 768w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/a><figcaption id=\"caption-attachment-41843\" class=\"wp-caption-text\">Output of docker build and docker run<\/figcaption><\/figure><\/p>\n<p>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<\/p>\n<pre class=\"brush:bash\">$ docker ps<\/pre>\n<p><figure id=\"attachment_41844\" aria-describedby=\"caption-attachment-41844\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-docker-ps.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41844\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-docker-ps.jpg\" alt=\"Output of currently running containers\" width=\"860\" height=\"150\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-docker-ps.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-docker-ps-300x52.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/07-docker-ps-768x134.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41844\" class=\"wp-caption-text\">Output of currently running containers<\/figcaption><\/figure><\/p>\n<p>To run the container as a background task instead, it must be started as a daemon. This is how it can be done<\/p>\n<pre class=\"brush:bash\">$ docker run --detach infinite-loop-alpine:latest<\/pre>\n<p><figure id=\"attachment_41845\" aria-describedby=\"caption-attachment-41845\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-run-detach-and-docker-ps.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41845\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-run-detach-and-docker-ps.jpg\" alt=\"Output of running a docker container in detached mode\" width=\"860\" height=\"221\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-run-detach-and-docker-ps.jpg 860w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-run-detach-and-docker-ps-300x77.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/08-docker-run-detach-and-docker-ps-768x197.jpg 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><figcaption id=\"caption-attachment-41845\" class=\"wp-caption-text\">Output of running a docker container in detached mode<\/figcaption><\/figure><\/p>\n<p>Notice that calling <code>docker run<\/code> like this does not lock the terminal anymore.<\/p>\n<h3>4.2 Connect to the daemon container<\/h3>\n<p>Connecting to a running container is simple. Use the <code>docker exec<\/code> command for this like below<\/p>\n<pre class=\"brush:bash\">$ docker exec --interactive --tty stupefied_lalande \/bin\/sh<\/pre>\n<p><code>--interactive<\/code> opens an interactive STDIN input to the container<br \/>\n<code>--tty<\/code> opens a terminal to the container<br \/>\n<code>nauseous_wing<\/code> is the name of the container to connect to<br \/>\n<code>\/bin\/sh<\/code> is the command to be executed once connected to the container<\/p>\n<p><figure id=\"attachment_41846\" aria-describedby=\"caption-attachment-41846\" style=\"width: 606px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/09-docker-exec-interactive-tty-and-ps.jpg\"><img decoding=\"async\" class=\"size-full wp-image-41846\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/09-docker-exec-interactive-tty-and-ps.jpg\" alt=\"Connecting to a container in interactive mode\" width=\"606\" height=\"247\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/09-docker-exec-interactive-tty-and-ps.jpg 606w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/11\/09-docker-exec-interactive-tty-and-ps-300x122.jpg 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/a><figcaption id=\"caption-attachment-41846\" class=\"wp-caption-text\">Connecting to a container in interactive mode<\/figcaption><\/figure><\/p>\n<p>You can see that a new shell has opened to the container. Running <code>ps<\/code> on the prompt will show that the script <code>infinite.sh <\/code> is already running with PID 1. This shall always remain the primary process since it is specified in the Dockerfile through the <code>CMD<\/code> command. A new shell process <code>\/bin\/sh<\/code> is also added due the <code>docker exec<\/code> command above apart from the <code>ps<\/code> command of course<\/p>\n<p>This is how one connects to a running container using the Docker CLI. There are other ways too, like <a href=\"https:\/\/docs.docker.com\/compose\/overview\/\">docker-compose<\/a> for instance, which we will probably explore in more detail in another post<\/p>\n<h2>5. Summary<\/h2>\n<p>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<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&nbsp; Let&#8217;s start 2. Understanding Docker &hellip;<\/p>\n","protected":false},"author":103,"featured_media":31013,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1354],"tags":[],"class_list":["post-41705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Connect to Docker Container Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connect to Docker Container Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-15T13:00:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-29T12:14:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Hariharan Narayanan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hariharan Narayanan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\"},\"author\":{\"name\":\"Hariharan Narayanan\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3\"},\"headline\":\"Connect to Docker Container Example\",\"datePublished\":\"2016-11-15T13:00:17+00:00\",\"dateModified\":\"2019-03-29T12:14:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\"},\"wordCount\":986,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"articleSection\":[\"Docker\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\",\"name\":\"Connect to Docker Container Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"datePublished\":\"2016-11-15T13:00:17+00:00\",\"dateModified\":\"2019-03-29T12:14:48+00:00\",\"description\":\"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DevOps\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/devops\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Docker\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/devops\/docker\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Connect to Docker Container Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3\",\"name\":\"Hariharan Narayanan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg\",\"caption\":\"Hariharan Narayanan\"},\"description\":\"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.\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/hariharan-narayanan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connect to Docker Container Example - Java Code Geeks","description":"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/","og_locale":"en_US","og_type":"article","og_title":"Connect to Docker Container Example - Java Code Geeks","og_description":"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers","og_url":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-11-15T13:00:17+00:00","article_modified_time":"2019-03-29T12:14:48+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","type":"image\/jpeg"}],"author":"Hariharan Narayanan","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Hariharan Narayanan","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/"},"author":{"name":"Hariharan Narayanan","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3"},"headline":"Connect to Docker Container Example","datePublished":"2016-11-15T13:00:17+00:00","dateModified":"2019-03-29T12:14:48+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/"},"wordCount":986,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","articleSection":["Docker"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/","url":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/","name":"Connect to Docker Container Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","datePublished":"2016-11-15T13:00:17+00:00","dateModified":"2019-03-29T12:14:48+00:00","description":"In this post we understand more about Docker containers and how they relate to images, commands to create containers and connect to running containers","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/12\/docker-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/devops\/docker\/connect-docker-container-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"DevOps","item":"https:\/\/examples.javacodegeeks.com\/category\/devops\/"},{"@type":"ListItem","position":3,"name":"Docker","item":"https:\/\/examples.javacodegeeks.com\/category\/devops\/docker\/"},{"@type":"ListItem","position":4,"name":"Connect to Docker Container Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/780d96edfe3bce18c5440613fa88bce3","name":"Hariharan Narayanan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Hariharan-Narayanan-96x96.jpg","caption":"Hariharan Narayanan"},"description":"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.","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/hariharan-narayanan\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/41705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=41705"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/41705\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/31013"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=41705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=41705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=41705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}