Running a shell script in docker container

Viewed 1317

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.

2 Answers

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

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.

Related