Containerization using Docker

Containerization using Docker

·

4 min read

Why Docker?

  • Traditional Problems with VMs: VMs are resource-intensive, slow to start, and expensive to manage due to the overhead of running full operating systems for each instance.

  • Docker as a Modern Solution: Docker containers are lightweight, portable, and start quickly. Containers share the host system's kernel, significantly reducing resource usage compared to VMs. They package everything needed for an application (code, runtime, libraries, dependencies) into a single unit, ensuring consistency across different environments.

Key Benefits of Docker:

  1. Lightweight: Containers share the host system’s kernel, reducing memory and storage usage.

  2. Portability: Docker containers run the same way on any system with Docker installed, making the "works on my machine" problem irrelevant.

  3. Fast Startup: Containers start in seconds, unlike VMs which can take minutes.

  4. Cost-Effective: Containers optimize resource usage, allowing multiple containers to run on the same host without the overhead of separate operating systems.

  5. Simplified CI/CD: Docker integrates seamlessly into CI/CD pipelines, enabling automated builds, tests, and deployments.

  6. Ecosystem and Community: Docker Hub provides pre-built images for common applications, speeding up development.

Docker Lifecycle:

  • Build: Create a Docker image using a Dockerfile.

  • Run: Start a container from the image.

  • Stop: Pause or stop a running container.

  • Restart: Restart a stopped container.

  • Remove: Delete containers and images when no longer needed.

Common Docker Operations:

  • docker --version: Check the installed Docker version.

  • docker pull ubuntu: Download an image from Docker Hub.

  • docker run -it -d ubuntu: Run a container from the ubuntu image in detached mode with an interactive terminal.

  • docker ps: List all running containers.

  • docker exec -it <container-id> bash: Access a running container.

  • docker stop <container-id>: Gracefully stop a container.

  • docker rm <container-id>: Remove a stopped container.

  • docker rmi <image-id>: Remove a Docker image.

Creating and Managing Docker Hub Account:

  • Sign Up: Create a Docker Hub account at Docker Hub.

  • Push an Image: Use docker login and docker push <image-name> to upload images to Docker Hub.

Saving Changes to a Docker Container:

  • To save changes made inside a container, use the docker commit command to create a new image from the modified container.

Dockerfile:

A Dockerfile is a text document containing all commands to build a Docker image. Here's a breakdown of Dockerfile instructions:

  • FROM: Specifies the base image (e.g., FROM ubuntu:20.04).

  • RUN: Executes commands inside the container (e.g., installing software).

  • COPY/ADD: Copy files from the host to the container.

  • WORKDIR: Sets the working directory for subsequent commands.

  • CMD/ENTRYPOINT: Defines the command to run when the container starts.

  • EXPOSE: Specifies which ports the container will listen on.

VOLUME: Creates a mount point for data sharing between the host and the container.

Example Dockerfile:

FROM ubuntu:20.04
LABEL maintainer="tksahu@example.com"
ARG VERSION=1.0
ENV APP_ENV=production
WORKDIR /app
COPY ./myapp /app
RUN apt-get update && apt-get install -y apache2
EXPOSE 80
VOLUME ["/data"]
USER www-data
CMD ["apache2ctl", "-D", "FOREGROUND"]
ENTRYPOINT ["python3"]

Hands-on Dockerfile Example:

  1. Create a directory for the Dockerfile:

     mkdir dockerfile
     cd dockerfile
    
  2. Create the Dockerfile using nano editor:

     nano Dockerfile
    
  3. Basic Dockerfile Example:

     FROM ubuntu
     RUN apt-get update
     RUN apt-get install -y apache2
     EXPOSE 80
     CMD ["apache2ctl", "-D", "FOREGROUND"]
    

    Real-Time Scenario:

    • Install Apache in a Docker container:

      1. Run the container: docker run -it -d ubuntu

      2. Install Apache: apt-get install apache2

      3. Commit the container to a new image: docker commit <container-id> tksahu/apache2

      4. Run a new container from the image: docker run -it -d -p 80:82 tksahu/apache2

    • ```bash • EXAMPLE :-Hands on lab for Dockerfile

      • Let create a DIR $mkdir dockerfile $cd dockerfile • Creat a file using nano editer

$nano dockerfile

FROM ubunru RUN apt-get update RUN apt-get –y install apache2 ADD ./var/www/html ENTRYPOINT apachectl –d FOREGROUND ENV name Devops-Alchemy

$nano 1.html

Hello World

hello from devops alchemy

Note :- how to run docker cmd without add sudo ? Type the cmd – sudo usermod –aG docker $user -----enter . then restart the computer it will work without sudo prvilage .

• Build the dockerfile

$docker build . –t new_dockerfile • Verify the new image: $docker images Output:-new_dockerfile Run a new container from the commited image Docker run –it –p 84:80 –d new_dockerfile Output:- 2abcdef12345 • Verify the running container: Docker ps Output:-2abcd • Enter the new container: $ sudo docker exec -it 2abcd bash Successfully enter the container • After enter it . Let me compare with my dockerfile $cd var/ww/html/ Output:- 1.html , Dockerfile , index.html {this is default page of apache } • Let me check the environment variable $cd /var/www/html root@2abcd: /var/www/html$ echo $name output:- Devops alchemy • Exit from the computer $exit

```