Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dockerfile that runs a python http server to display an html file

I have a directory with one simple HTML file (name: index.html) which displays some basic text. I run the python command:

python -m SimpleHTTPServer 7000

to run a server, at port 7000, in the same directory to display the page in a browser.

Now I want to be able to dockerize this process and need help with that.

Basically, the dockerfile should run a server at 7000 port using this python command and then display the html on a browser.

What I thought of:

FROM ubuntu:14.04
COPY index.html
FROM python:latest
EXPOSE 80
CMD ["python SimpleHTTPServer 7000", "-m"]

Also, how would I build and run this file once it is done?

I am pretty sure this won't work but since I am new to this, I don't know how to correct it.

like image 477
El Tikki Avatar asked Jun 25 '18 05:06

El Tikki


1 Answers

Also, how would I build and run this file once it is done?

You were close. Several pointers:

  • If you use python3 then you have to either use http.server or install SimpleHTTPServer separately
  • If you use python 2.7 then you can't use 'latest' tag in manner you are using it
  • Container port and your desired target local port are not the same

Here are Dockerfile variations for python 3:

FROM python:latest
COPY index.html /
EXPOSE 7000
CMD python -m http.server 7000

and python 2.7:

FROM python:2.7
COPY index.html /
EXPOSE 7000
CMD python -m SimpleHTTPServer 7000

alongside with build

docker build -t my-docker-image .

and run commnand:

docker run --rm -it --name my-docker-instance -p 80:7000 my-docker-image

After run you can go to http://localhost to get container's port 7000 there, providing your host doen't run something on port 80 (remap if so).

Notes:

  • Using latest image is ok for development, but problematic in production
  • work dir is set to root, maybe you would like to position files appropriately
  • running code off simple server is ok for defvelopment

Edit: I see that b0gusb beat me to it :)

like image 137
Const Avatar answered Sep 23 '22 11:09

Const