1

Hi helpful People of the World!

i am getting into containerisation and docker and i am trying to create some basic containers. I am running Docker on Windows.

I want to create a container that runs a shell script with a curl command. i have found an image on docker with curl built in.

My Directory structure in windows looks like
Curl_container (folder)
Dockerfile (file)
curl_registry.sh (file)

the curl_registry.sh code is

#!/bin/bash
    
while :
do
HOSTNAME=$(hostname)
DATE=$(date +'%Y-%m-%d')
TIME=$(date +'%Y/%m/%d %X')

CURL_RESULT=`http://www.google.com | head -1`
    
echo $TIME  $HOSTNAME  $CURL_RESULT

sleep 5
done
exit

and my Docker file looks like this

FROM eamonwoortman/alpine-python-curl-zip
MAINTAINER red_badger@burrow.uk
WORKDIR /
COPY . /
RUN ["chmod", "+x","curl_registry.sh"]
ENTRYPOINT [ "./curl_registry.sh" ]

i am able to build the container;

PS C:\temp\Docker\Curl_container> docker build -t curl_container:1.18 .
[+] Building 1.0s (8/8) FINISHED
 => [internal] load build definition from Dockerfile  0.0s
 => transferring dockerfile: 238B                     0.0s
 => [internal] load .dockerignore                     0.0s
 => [internal] load metadata for docker.io/eamonwoortman/alpine-python-curl-zip:latest               0.0s
 => CACHED [1/4] FROM docker.io/eamonwoortman/alpine-python-curl-zip                                 0.0s
 => [2/4] COPY . /                                                                                   0.1s
 => [3/4] RUN ["chmod", "+x","curl_registry.sh"]                                                         
 => writing image sha256:497b03c55d8e90e7eff43c8868e420498516477b9225ad6d3bdf0076ebe9d9ed         0.0s
 => naming to docker.io/library/curl_container:1.18                                               0.0s

However when i run it, I get the following error

PS C:\temp\Docker\Curl_container> docker run 497b03c55d8e
standard_init_linux.go:228: exec user process caused: no such file or directory
I would like to have the script running in a folder /apps, but i thought this was my problem and so tried to put the file into the root directory. I have tried the ADD command in place of COPY and i got the same result. the curl_registry.sh is showing as a .sh file in windows.

I have run my sh file through notepad++ and converted to unix LF.

Toto
  • 89,455
  • 62
  • 89
  • 125
Red_badger
  • 119
  • 1
  • 15

2 Answers2

1

Your alpine image doesn't have a bash interpreter. You need to change your interpreter to ash and fix your curl command

#!/bin/ash
    
while :
do
HOSTNAME=$(hostname)
DATE=$(date +'%Y-%m-%d')
TIME=$(date +'%Y/%m/%d %X')

CURL_RESULT=`curl -s -N http://www.google.com | head -1`
    
echo $TIME  $HOSTNAME  $CURL_RESULT

sleep 5
done
exit
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
pahool
  • 316
  • 2
  • 16
  • note that `hostname` inside a docker container is going to return the container id, which is likely not what you want. What are you expecting to get with hostname? If it's the hostname of the host, that is not available to docker without some gymnastics. You could add the `--hostname` option to your docker run command to inject a hostname at runtime, but it's unclear to me what this actually gains you. What is your intention here? – pahool Aug 20 '21 at 19:18
  • 1
    Hey Pahool, Thanks, I will give it a bash. (ash!) It worked! I read that before on another thread and they suggested installing bash, but this is neater, thank you. I dont really understand why not having a bash interpreter would give me a file not found error? surely i would expect a different error? I am happy for the hostname to be the container as I will be streaming the result to Splunk, this will also act as an alert if the container gets trashed and restarted. Cheers, – Red_badger Aug 21 '21 at 10:12
  • I believe that the "file not found" error is caused by not finding /bin/bash. – pahool Aug 21 '21 at 15:53
0

Use bash inside of the container and run the commands and troubleshoot the errors which occur, and then when the command is working, you can add these commands to the dockerfile.

sudo docker exec -it (container name) /bin/bash

For your information by default, you are the root user in a docker container.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • HI Shaqil, I have already run the image as standalone. I have done a mkdir -p /app Inside that directory I created my bash script and run it successfully. I get that I am root, i just wanted inside the folder for neatness. – Red_badger Aug 20 '21 at 16:41
  • I'd be interested to see the output from your successful run. Does your image even have a bash interpreter? I tried building it and don't see bash available. – pahool Aug 20 '21 at 18:57