Introduction to container based development - Part 1/4
Tags: docker
Published: 2020-03-26 19:19:53

This post is based on the container development [article]( https://github.com/drFabio/containerDevelopment/blob/master/article.pdf) wrote by me. We are going to review some basics about containers and hopefully you are going to start loving and using them as much as me.


## Requirements
Have [docker](https://docs.docker.com/install/) and [docker-compose]({https://docs.docker.com/compose/install/) installed.

## Introduction 
Containers borrow the name from shipping containers a mean of transportation that revolutionized the way we ship goods. Be it 100 tons of tuna, a whole house or thousands of unrelated Amazon packages they can be easily transported from anywhere to anywhere. Software containers are the same, the handlers of the containers don't care about what is inside they know how to handle containers and that is what make them flexible.

On the software world developing with containers allow us to eliminate the "it works on my machine", run multiple versions of the same software, create complex architectures on a local machine, make micros services with hundreds of different languages and many more nice things.

## The concepts

### Image

Think of an image as a read only data, like a music “CD”. You could play the PHP CD, perhaps you are feeling a little javazzy and play the java CD or you want to do your own thing so you remix a  CD and put the data that you need. The CD *does not have a state* it is what it is and that particular CD will always have the same contents.
A image is built with layers on top of a filesystem. Usually you create an image using another image and create it with a series of instructions, if docker already has these instructions on cache it will use that cache instead of building from scratch. Still on our CD metaphor the CD is done with each track separated, you have the drums, the guitar, the bass and the vocals each on a different layer, if you had a karaoke version of the CD the studio one would be the karaoke with the vocals track on top of it.

Images are listed on a repository like [docker hub](https://hub.docker.com/) and have a name and a tag.

![An image with its layers](https://dev-to-uploads.s3.amazonaws.com/i/ne7bk9n3pcs1lif95omj.png)

### Container

A container is a running image and *it has state but does not have persistence*. Think about a CD and somebody manipulating it like a DJ, it is doing changes to the current state of the CD but by the time the execution is done the CD is back on the case and nothing changed on it. The execution of the CD was the container and it can change as much as we need but it's changes will not change the image itself.

### Volumes

A volume is where we have *persistent state*, this is where our changes are not lost, this is how containers read and write persistent data. For example think on an application that counts sales, it would be a pretty lousy application if we lost all sale data as soon as the application was shutdown, for that we persist data on volumes. Most database images will have a volume. Still on the musical analogy our volume is our live recording, we are working on it and it will change, even if we have some fixed tracks we are still changing the data on this one.


|  |State|Persistence|
|---|---|---|
|Image| No | No  |
|Container| Yes | No  |
|Volume| No | Yes |

## Getting started

### Running the first image - Playing the CD
First we need our image on our system so we pull it from the repository. Execute the code below on you terminal.

```shell
docker pull docker/whalesay:latest
```
The output should say something like:

"Status: Downloaded newer image for docker/whalesay:latest"

The image is on our system now we need to run it (Play the cd):

```shell
docker run docker/whalesay cowsay "Docker is awesome"
```

And you should see the following:

```text
< Docker is awesome >
 ------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/  
```

So what happened? For a brief moment we run our image, that generated a container that printed a whale on the screen and then the container stopped.

Congratulations you just used your first image and created your first container!

### Acknowledgement

[Photo by Kaique Rocha from Pexels](https://www.pexels.com/photo/business-commerce-container-export-379964/)