Building a Container that Loops: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(16 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Docker_build#Image_Recipes|Docker build - Image Recipes]]
* [[Docker_build#Image_Recipes|Docker build - Image Recipes]]
=Overview=
This is a simple container based on [[Centos Base Image|centos base image]] whose command sleep-loos, allowing the container to "hang around" without doing much. It also carry a "probe" script that can be exercised externally.
=Procedure=
==Create the Image==
Place the scripts (loop and probe) in an empty directory that will serve as [[Docker_build#The_Build_Context|build context]].
loop:
<syntaxhighlight lang='bash'>
#!/bin/bash
function sigint() {
  echo "process got SIGINT and it is exiting ..."
  run=false
}
function sigterm() {
  echo "process got SIGTERM and it is exiting ..."
  run=false
}
trap 'sigint' INT
trap 'sigterm' TERM
while ${run}; do
  sleep 1
done
</syntaxhighlight>
probe:
<syntaxhighlight lang='bash'>
#!/bin/sh
echo "executed in $(uname -n)"
</syntaxhighlight>
Dockerfile:
<syntaxhighlight lang='Dockerfile'>
FROM centos:latest
COPY loop probe /opt
CMD /opt/loop
</syntaxhighlight>
The build command:
cd workarea
docker build -t centos-loop:1 .
The image thus created can be listed in the local registry:
docker images
REPOSITORY          TAG                IMAGE ID            CREATED              SIZE
centos-loop        1                  89c2ce131758        About a minute ago  207MB
centos              latest              ff426288ea90        9 days ago          207MB
==Run the Container==
docker run -d centos-loop:1
docker ps
CONTAINER ID  IMAGE              COMMAND                  CREATED            STATUS              PORTS              NAMES
bcff8efd8a9a  centos-loop:1      "/bin/sh -c /opt/loop"  2 minutes ago      Up 2 minutes                            brave_yonath
==Log into the Container==
docker exec -it bcff8efd8a9a /bin/bash
==Execute the Probe==
docker exec bcff8efd8a9a /opt/probe

Latest revision as of 20:16, 29 January 2018

Internal

Overview

This is a simple container based on centos base image whose command sleep-loos, allowing the container to "hang around" without doing much. It also carry a "probe" script that can be exercised externally.

Procedure

Create the Image

Place the scripts (loop and probe) in an empty directory that will serve as build context.

loop:

#!/bin/bash

function sigint() {
   echo "process got SIGINT and it is exiting ..."
   run=false
}

function sigterm() {
   echo "process got SIGTERM and it is exiting ..."
   run=false
}

trap 'sigint' INT
trap 'sigterm' TERM

while ${run}; do
  sleep 1
done

probe:

#!/bin/sh

echo "executed in $(uname -n)"

Dockerfile:

 FROM centos:latest
 COPY loop probe /opt
 CMD /opt/loop

The build command:

cd workarea
docker build -t centos-loop:1 .

The image thus created can be listed in the local registry:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos-loop         1                   89c2ce131758        About a minute ago   207MB
centos              latest              ff426288ea90        9 days ago           207MB

Run the Container

docker run -d centos-loop:1
docker ps
CONTAINER ID  IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bcff8efd8a9a  centos-loop:1       "/bin/sh -c /opt/loop"   2 minutes ago       Up 2 minutes                            brave_yonath

Log into the Container

docker exec -it bcff8efd8a9a /bin/bash

Execute the Probe

docker exec bcff8efd8a9a /opt/probe