Run a simple webpage using docker

Docker is a container technology that helps easier application deployment by providing an abstraction over Linux OS. In this tutorial, I will go through creating a simple (simplest) docker container and deploy a very basic web-page that runs on port 3000.

To install docker run sudo apt-get install docker.io (in ubuntu). You might have to restart the OS after the installation.

1. Dockerfile

To create a docker file (configuration file used by docker to create a docker image). Run following commands.

$ mkdir docker-example
$ cd docker-example
$ gedit Dockerfile

Add following text into the file.

FROM ubuntu:14.04

USER root
RUN apt-get update && apt-get install -y \
    python3 \
    curl && \
    rm -rf /var/lib/apt/lists/*
RUN groupadd -r nonroot && \
    useradd -r -g nonroot -d /home/nonroot -s /sbin/nologin -c "Nonroot User" nonroot && \
    mkdir /home/nonroot && \
    chown -R nonroot:nonroot /home/nonroot

USER nonroot
WORKDIR /home/nonroot/
RUN curl -o index.html http://info.cern.ch/hypertext/WWW/TheProject.html

USER nonroot
EXPOSE 3000
WORKDIR /home/nonroot/
CMD ["python3", "-m", "http.server", "3000"]

Edit the apt-get section to add packages you need to run your application. The next block of code downloads a web page to host inside /home/nonroot/ directory. The final block of code runs python’s SimpleHTTPServer to host the directory containing the file.

2. Build docker image.

$ sudo docker build -t=docker-example .

Successfully built <image-id>

3. Run docker image

$ sudo docker run -p 3000:3000 -i -t docker-example

Browse http://localhost:3000/ to view the webpage.

screen-shot

Conclusion

After this installation, you should be able to modify this tutorial to bundle your application specific packages and distribute your application without conflicting with dependencies. You can publish your package to https://registry.hub.docker.com/search?q=library for easier lookup and installation.

References

1. https://zeltser.com/docker-application-distribution/

2. https://www.docker.com

2 Comments

Filed under Uncategorized

2 responses to “Run a simple webpage using docker

  1. Unfortunately I followed all steps and it still failed with this error: The command ‘/bin/sh -c apt-get update && apt-get install -y python3 curl && git nginx nodejs rm -rf /var/lib/apt/lists/*’ returned a non-zero code: 127

Leave a comment