# 14. Containerization: Docker Setup and Walkthrough

### **Introduction**

Containers hold immense power, but it all begins with getting Docker up and running. In this walkthrough, l'll guide you through the installation process and introduce the core concepts behind building and running containerized applications with Docker.

**Installing Docker**

The Docker website provides tailored installation instructions for various operating systems. Visit [https://docs.docker.com/install](https://docs.docker.com/install) to find the right steps for your setup.

* **CentOS Example:** If you're using CentOS, here's the typical installation process:
    
    ```plaintext
    yum install docker
    systemctl start docker
    systemctl enable docker
    ```
    
* **Verify:** Confirm your installation is successful by running:
    
    ```plaintext
    docker info
    ```
    

### **Docker Fundamentals**

You may use this resource link for more info: [http://docs.docker.com/get-started](http://docs.docker.com/get-started)

* **Image Hierarchy:** Think of images like layered blueprints. Each image often starts with a base parent image (like a Linux operating system) and then adds layers of customization, such as your application, its dependencies, and settings.
    
* **It's Build Time:** To create your custom image, you first define its ingredients in a file aptly named a 'Dockerfile.' Let's use the `docker build` command to transform this recipe into reality:**Bash**
    
    ```plaintext
    docker build -t <docker username>/<image-name> .
    ```
    
    Note: Include the '.' at the end of the command. It specifies the location of your Dockerfile.
    
* **Run Your Container:** Ready to see it in action? Use the `docker run` command:
    
    ```plaintext
    docker run -d <docker username>/<image-name>
    ```
    
    * The '-d' flag lets it run in the background (detached mode).
        
* **Management Commands:**
    
    * `docker ps` - Gives you a list of running containers.
        
    * `docker stop <container id>` - Gracefully stops your container.
        

### **Docker Registry**

Registries act as libraries for your Docker images. After building an image, you can "push" it to a registry, making it accessible from other machines.

* **Popular Options:** Docker Hub ([https://hub.docker.com](https://hub.docker.com/)) is the official publicly accessible registry. However, for certain situations, you might utilize a private registry for more control.
    
* **Docker Hub Login (if using):**
    
    ```plaintext
    docker login --username=<hub username> --email=<hub email>
    ```
    

### **Dockerfile**

A Dockerfile contains a series of instructions that tell Docker how to assemble your image. Let's briefly explain common ones:

* `FROM` &lt;image name&gt; – Sets the base image to build upon.
    
* `WORKDIR` &lt;directory path&gt; – Sets the working directory for subsequent commands.
    
* `COPY` &lt;source&gt; &lt;destination&gt; – Copies files from your computer into the image.
    
* `RUN` &lt;command&gt; – Executes commands to install software or manipulate files.
    
* `EXPOSE` &lt;port&gt; – Informs Docker about network ports your application uses.
    
* `CMD` &lt;array of command arguments&gt; – Specifies the default command the container will run.
    

**Further Exploration:** Get in-depth command breakdowns at the Dockerfile reference: [https://docs.docker.com/engine/reference/builder/](https://docs.docker.com/engine/reference/builder/)

**Additional Info**

* **Error Handling:** Keep in mind that sometimes installation or Docker commands might result in errors. Don't panic! The Docker documentation can typically help you navigate troubleshooting these issues.
    
* **Image Sizes:** As you get more advanced, be aware that the size of your images can affect download and deployment speeds. This often depends on the choice of base image and your Dockerfile instructions.
    
* **Real World Tip:** In production environments, you'll typically use existing, well-established base images instead of writing every detail in your Dockerfile from scratch. This promotes greater efficiency and reliability.
    

**Wrapping Up**

We've installed Docker and dipped our toes into image building and container management. Ready to dive deeper? Next, we'll explore crafting advanced Dockerfiles and the magic of orchestration for large-scale containerized applications with tools like Kubernetes!
