Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a simple webserver in Docker?

Tags:

docker

I am trying to build a simple docker container that serves static html. I have the following Dockerfile:

FROM    ubuntu

# Install python3
RUN     apt-get update
RUN     apt-get install -y python3

# Copy html
ADD static/ /src
RUN cd /src

# Run http server on port 8080
EXPOSE  8080
CMD ["python3", "-m http.server 8080"

However when I build + run it I get the following error:

/usr/bin/python3: No module named  http

I have tried the same steps via the interactive shell and they work fine, however as soon as I use a Dockerfile it fails.

like image 993
Scott Greenlay Avatar asked Sep 12 '13 14:09

Scott Greenlay


People also ask

Is Docker good for Web server?

Is Docker good for Web servers? Yes, Docker is an excellent tool for web servers. It can be compared to a virtual machine packed with all the code and dependencies needed for an application. But it is much lighter and more powerful than a virtual machine.

Can I host a website with Docker?

Ad. Docker is an extremely useful platform that enables developers to easily develop and deploy applications. In this article, we'll look at how to use Docker containers to host multiple websites on a single server.

Can Docker be used as a server?

Run Anywhere. Docker has the same promise. Except instead of code, you can configure your servers exactly the way you want them (pick the OS, tune the config files, install binaries, etc.) and you can be certain that your server template will run exactly the same on any host that runs a Docker server.


2 Answers

I think your CMD syntax is wrong. i just tried and it works fine:

FROM    ubuntu

# Install python3
RUN     apt-get update
RUN     apt-get install -y python3

# Copy html
ADD static/ /src
RUN cd /src

# Run http server on port 8080
EXPOSE  8080
CMD ["python3", "-m", "http.server", "8080"]
like image 195
creack Avatar answered Oct 08 '22 19:10

creack


I ran into an issue with the accepted answer where network issues prevented me from running apt-get update on my corporate network..

However, the following Dockerfile worked and I believe is more lightweight than the ubuntu image for something as simple as sharing static html files.

FROM python:3.6.0-alpine
ADD static/ /src
WORKDIR /src
EXPOSE  8080
ENTRYPOINT ["python3", "-m", "http.server", "8080"]

Also, as it relates to the original question would not the following also work? I am not sure I understand the benefit of breaking the command into an array.

CMD python3 -m http.server 8080
like image 26
Collin Yeadon Avatar answered Oct 08 '22 20:10

Collin Yeadon