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:

  • Docker SSH or Detach/Attach
  • wrapping DB in docker image
  • How to improve Kubernetes security especially inter-Pods?
  • How to access the metadata of a docker container from a script running inside the container?
  • graphql and serverless offline running in docker
  • How to restart a process inside a container / Restarting PhpStorm using original restart.py
  • 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?

  • Should I create a volume for project files when using Docker with git?
  • Vagrant using docker as provider on MacOS, impossible to mount directory in container
  • Filter docker-compose output
  • Running GUI in docker (no ssh, no VNC)
  • Difference between production and development docker using
  • Docker restart not showing the desired effect
  • One Solution collect form web for “How do I write a dockerfile to execute a simple bash script?”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.