Creating your first Docker Image from scratch

Intro

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession --view docs

Requirements

  • Basic knowledge about Docker

Tutorial

In this tutorial we will create a hello world webserver, that will print Hello World, based on nginx.

  1. create a file called Dockerfile
  2. we will use ubuntu 16.04 as our base
  3. update & upgrade the os packages
  4. install nginx
  5. create a file called /var/www/html/hello-world.html and write <h1>Hello World</h1> in it.
  6. expose the 80 port
  7. start the nginx service
  8. set the main entrypoint to a long-running-process to keep the container running
  9. build the image
  10. use it !
FROM ubuntu:16.04  
RUN apt update && apt full-upgrade -y  
RUN apt install nginx -y  
RUN echo '<h1>HelloWorld</h1>' > /var/www/html/hello-world.html  
EXPOSE 80  
ENTRYPOINT service nginx start && /bin/bash  

now open your terminal and run docker build . -t hello/world, then docker run -itd -p 8080:80 hello/world, goto localhost:8080/hello-world.html .

Notes

  • take a look at main docs, to see all available commands.