Tuesday, October 6, 2020

Intro to Docker



Containers are extraordinarily popular and open the doors to alternative service-oriented architectures.  This week I spent a few minutes with Docker, a quick intro to the technology.  I started with a quick intro from YouTube;

Following along, we first need to install Docker on our Ubuntu machine.

$ sudo apt-get install docker.io

After Docker is installed on our workstation we configure the container, build it, then run it. 

Our example will be trivial, a http server with a static welcome message.  The Dockerfile specifies the configuration of the container, the src/index.php serves up the welcome page.

~/docker$ tree .
.
├── Dockerfile
└── src
    └── index.php


~/docker$ cat -n Dockerfile
     1    FROM php:7.0-apache
     2    COPY src/ /var/www/html/
     3    EXPOSE 80

The Dockerfile specifies the container recipe, an Apache container, a couple configuration steps: 1) copying the index file to the container location, and 2) opening port 80 for incoming traffic.

~/docker$ cat -n src/index.php
     1    <?php
     2   
     3    echo "Hello, World";
     4    ?>


With the configuration information available, we build the container by issuing the following command:

~/docker$ sudo docker build -t hello-world .

Afterwards, we can launch the container:

~/docker$ sudo docker run -p 8080:80 hello-world

The port redirection redirects 8080 incoming ports to the host to the container port 80.

Then, we can connect to the container by opening a browser to connect to our host: http://localhost:8080

Enjoy!

No comments:

Post a Comment