• Articles
  • Twitter
  • Bsky
  • Github
  • Podcast
  • Articles
  • Twitter
  • Bsky
  • Github
  • Podcast
  • Hello, my name is Docker

    25 Sep • 2014

    Let’s start to build together a simple application using docker. This posts is actually a short guide for beginners about how to use this technology.

    What’s Docker?

    Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. This means it can automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating system–level virtualization on Linux. Docker uses resource isolation features of the Linux kernel such as cgroups and kernel namespaces to allow independent “containers” to run within a single Linux instance, avoiding the overhead of starting virtual machines.

    With Docker, developers can build any app in any language using any toolchain and sysadmins use it to provide standardized environments for their development. Docker can be integrated into various infrastructure tools, including Ansible, CFEngine, Chef, Jenkins, OpenStack Nova, OpenSVC,Puppet, Salt and Vagrant.

    Any aditional question about how docker works, you can find here: what is docker?

    Docker Containers

    Docker containers are basically directories which can be packed like any other, then shared and run across various different machines and platforms.

    Docker Images

    Snapshots of containers or base OS (example: Ubuntu) images. Similar to default operating-system disk images which are used to run applications on desktop computers.

    Dockerfiles

    Dockerfiles are scripts containing a series of instructions and commands which are to be executed to form a new docker image. They basically replace the process of doing everything manually and repeatedly. When a Dockerfile is finished executing, you end up having formed an image, which then you use to create a new container.

    Any aditional question about how dockerfiles works, you can find here: Dockerfiles usage

    Installing Docker

    Before proceeding, you must have installed the docker. Follow the docs on installation guide

    So now you’ve got docker!

    Once you have docker installed. You have to know the docker CLI consists of passing it a chain of params, you can get a help typing the following command:

    docker

    Or check docker information and version:

    # Check docker system-wide information sudo docker info
    # Check docker version sudo docker version

    In this guide the used client version is 1.2.0 and used client API version is 1.14.

    OS X cases

    If you use OS X, read this first: For OS X cases, you need a helper application called Boot2Docker that installs a virtual machine (using VirtualBox) that’s all set up to run the Docker daemon. You initialize Boot2Docker from the following commands:

    $ boot2docker init $ boot2docker start $ export DOCKER_HOST=tcp://192.168.59.103:2375

    Starting with Docker

    In this guide I’ve publish a project in github. In this project have a Dockerfile to build docker image, using nodejs.

    Hint: You can check other Dockerfiles examples here.

    We’ll use this project as base to this guide. Clone or fork the project to continue. So open the project folder and run to build an image:

    $ docker build -t="docknode" .

    Okay, so now you’ve got a docker image called “docknode” and you can run that image, using this:

    $ docker run -i -t docknode

    Or to run it in the background

    $ docker run -d docknode

    After this, you’ll should receive this:

    Server running at http://127.0.0.1:8080/

    …And you’ve built your first application running with docker, fast and easy :)

    Understanding how Code works

    Dockerfile: This scripts builds a Docker image. It’s use ubuntu OS, install nodejs via apt-get and run app.js file using node.

    FROM ubuntu:13.10 MAINTAINER Raphael Amorim
    RUN apt-get update RUN apt-get install -y nodejs RUN mkdir /var/www
    ADD app.js /var/www/app.js
    CMD ["nodejs", "/var/www/app.js"]

    App.js: Build a simple nodejs server.

    var http = require('http'),     port = process.env.PORT || 8080;
    var server = http.createServer(function (request, response) {     response.writeHead(200, {"Content-Type": "text/plain"});     text = "Running Node.js:" + process.versions.node
        response.end(text); });
    server.listen(port, function(){     console.log("Server running at http://127.0.0.1:" + port + "/"); });

    This is Docker!

    Do you like or have any suggestion? I would love to receive your feedback about this post or Docker.