Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a dockerfile to execute a simple bash script?

I'm trying to write a docker image to run a simple webserver though netcat.

So I have in my docker build folder:

Dockerfile
index.html
run_netcat_webserver.sh

The run_netcat_webserver.sh is very simple and it works fine:

#!/bin/bash

while true ; do nc -l 8080  < index.html ; done

Here is my naive Dockerfile that of course is not working:

FROM ubuntu:14.04

CMD run_netcat_webserver.sh

How should I proceed to make this work inside a docker container?

like image 638
TakeSoUp Avatar asked May 26 '16 21:05

TakeSoUp


2 Answers

You need to make the script part of the container. To do that, you need to copy the script inside using the COPY command in the Docker file, e.g. like this

FROM ubuntu:14.04
COPY run_netcat_webserver.sh /some/path/run_netcat_webserver.sh
CMD /some/path/run_netcat_webserver.sh

The /some/path is a path of your choice inside the container. Since you don't need to care much about users inside the container, it can be even just /.

Another option is to provide the script externally, via mounted volume. Example:

FROM ubuntu:14.04
VOLUME /scripts
CMD /scripts/run_netcat_webserver.sh

Then, when you run the container, you specify what directory will be mounted as /scripts. Let's suppose that your script is in /tmp, then you run the container as

docker run --volume=/tmp:/scripts (rest of the command options and arguments)

This would cause that your (host) directory /tmp would be mounted under the /scripts directory of the container.

like image 135
zegkljan Avatar answered Nov 15 '22 05:11

zegkljan


Simple example

  1. Write a "Dockerfile"

FROM ubuntu:14.04 - # image you are using for building the container

ENTRYPOINT ["/bin/echo"] - # command that will execute when container is booted

  1. Verify that you are in the same directory as the "Dockerfile".

  2. sudo docker build .

and you are done

sample output:

ahanjura@ubuntu:~$ vi Dockerfile

ahanjura@ubuntu:~$ sudo docker build .

Sending build context to Docker daemon 1.39 GB Step 1 : FROM ubuntu:14.04 ---> 67759a80360c Step 2 : ENTRYPOINT /bin/echo ---> Running in c0e5f48f7d45 ---> 7a96b36f528e Removing intermediate container c0e5f48f7d45 Successfully built 7a96b36f528e

like image 34
user1833058 Avatar answered Nov 15 '22 04:11

user1833058