3 simple steps to Deploy on Docker
In this article, you will learn, how to Deploy on Docker a static website using Docker container. Static websites are HTML pages, directly severed by any web servers. It may also include some other static assets like CSS, JS, and images.
1) Create Dockerfile
Docker Images start from a base image. The base image must include the platform dependencies required by your application, for example, having the JVM or CLR installed.
This base image is defined as an instruction in the Dockerfile. Docker Images are built based on the contents of a Dockerfile. The Dockerfile is a list of instructions describing how to deploy your application.
In this example, our base image is the Alpine version of Nginx. This provides the configured web server on the Linux Alpine distribution.
Create DockerFile for building Docker Image by copying the contents below into the editor.
Copy to EditorFROM nginx:alpine
COPY . /usr/share/nginx/html
The first line defines our base image. The second line copies the content of the current directory into a particular location inside the container.
2) Build Docker Image
The Dockerfile is used by the Docker CLI build command. The build command executes each instruction within the Dockerfile. The result is a built Docker Image that can be launched and run your configured app.
The build command takes in some different parameters. The format is docker build -t <build-directory>. The -t parameter allows you to specify a friendly name for the image and a tag, commonly used as a version number. This allows you to track built images and be confident about which version is being started.
Build Static HTML Image using the Docker Build command as below.
docker build -t testserver-image:v1 .
You can view a list of all the images on the host using docker images.
The built image will have the name testserver-image with a tag of v1.
3) Run
The built Image can be launched in a consistent way to other Docker Images. When a container launches, it's sandboxed from other processes and networks on the host. When starting a container you need to give it permission and access to what it requires.
For example, to open and bind to a network port on the host you need to provide the parameter -p <host-port>:<container-port>.
Launch Docker Image providing the friendly name and tag. The name of the image should be as it's a web server, bind port 80 to our host using the -p parameter.
docker run -d -p 80:80 testserver-image:v1
Once started, you'll be able to access the results of port 80 via curl docker
. To render the requests in the browser use the links provided by Docker.
Now, you have a docker image now. Use this docker image to launch a new container on your system. To run your Docker container using the newly created image.
Once the container is up and running. All the on port 8080 of the host machine will be redirected to the container’s port 80. Access your Docker host using the IP address (or hostname/domain name) on port 8080 to view the application.
You now have a static HTML website being served by Nginx.
Check out other articles in this series: Docker Tutorials
1) Introduction to Docker and Docker Containers
2) Ultimate Comparision between Docker Containers vs Virtual Machine
3) Complete Setup to Install Docker
4) Hello World Program in Docker
5) 3 Simple Steps to Deploy on Docker