8

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?

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

How do I know what /some/path to use? I tried /usr/local/bin and it did not work :(
@TakeSoUp It's the path inside the container. It can easily be just / as you basically don't need to care about users inside the container. It's all isolated so why not...
Thanks! Is it possible to just copy my script to the home directory (i.e. ~)?
@TakeSoUp I don't know which home directory you mean (the one in container or the "normal" one, i.e. the host's) but you can certainly copy anything anywhere.
Great answer. You can use ENTRYPOINT too in place of CMD.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.