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.
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.
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.
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.
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"]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With