Simple Docker container running a http server
In this exercise we will create a Docker container which will run a simple http server.
Before starting, setup Docker on your local machine. You can easily setup Docker on your Windows machine using Chocolatey.
Creating a Dockerfile
We will create the Docker container using a
Dockerfile. A Dockerfile contains a set of instructions to tell docker how to create and configure the container.
Create a directory with the name
Inside this directory, create a file name
Dockerfile without extension.
Add the following line to the file.
This tells Docker that we want to create a new image using
ubuntu:latest as the base image. Here
ubuntu is the name of a repository and
latest is a tag. This should be the first line in the file.
Optionally we can specify the name and email of the maintainer.
MAINTAINER Hemanshu Bhojak <firstname.lastname@example.org>
Now we will update
apt-get and use it to install
RUN apt-get update RUN apt-get install -y nodejs RUN apt-get install -y npm
http-server is a simple, zero-configuration command-line http server. It serves files from the current directory over http. We can install it using
RUN npm install -g http-server
Create a symlink to the
nodejs directory so that it can be run using the
RUN ln -s /usr/bin/nodejs /usr/bin/node
Create a html file with some content in the
docker-playground directory and name it index.html. We will add this file to the container.
ADD index.html /usr/apps/hello-docker/index.html
Now we will set the working directory of the container to this path.
Lastly we will specify a command to run the http-server in silent mode by specifying the
CMD ["http-server", "-s"]
Your Dockerfile should look like this.
FROM ubuntu:latest MAINTAINER Hemanshu Bhojak <email@example.com> RUN apt-get update RUN apt-get install -y nodejs RUN apt-get install -y npm RUN ln -s /usr/bin/nodejs /usr/bin/node RUN npm install -g http-server ADD index.html /usr/apps/hello-docker/index.html WORKDIR /usr/apps/hello-docker/ CMD ["http-server", "-s"]
Build the Dockerfile
Run the following command in your
docker build -t "playground:hello-docker" .
This command builds a container image in the
playground repository and tags it
To see the image you have just created, run the following command.
Run the Docker Container
The following command will run the Docker container we just created.
docker run -pt 8080:8080 "playground:hello-docker"
"playground:hello-docker" specifies the container to run.
8080 as the port. In order to access it on our host machine we need to expose it.
8080:8080 binds the host port to the container port. Here the first value is the container port and the other is the host port.
In order to test this we need to know the IP Address of the virtual machine which runs the container.
Run the following command to find the IP Address of your Docker virtual machine. It is under the URL column and looks like
If everything went well you can see your
index.html in the browser of your host machine using the following url.
Some useful Docker commands
List all Images
List all Containers
docker ps -a
Delete an Image
docker rmi <imageid>
Delete a Container
docker rm <containerid>