Day 19:Docker for DevOps Engineers

Day 19:Docker for DevOps Engineers

Docker-Volume

  • Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having the same data.

Docker Network

  • Docker Network is a feature that enables different containers to communicate with each other and with other networks. Docker provides several built-in network drivers that allow containers to connect to each other or to the host network. You can also create custom network drivers to meet specific network requirements.

  • some of the most commonly used Docker network drivers:

    1. Bridge network driver: The default network driver used by Docker when creating new containers. It connects containers to a shared network and assigns them IP addresses from a subnet. Containers connected to the bridge network can communicate with each other using their IP addresses, and they can also communicate with the host machine using the host's IP address.

    2. Host network driver: This driver connects a container directly to the host network, which means the container shares the host's IP address and network stack. Containers connected to the host network can access all ports on the host machine, and they can also communicate with each other using their IP addresses.

    3. Overlay network driver: This driver is used to create multi-host networks that span multiple Docker hosts. It allows containers running on different hosts to communicate with each other using encrypted tunnels.

    4. Macvlan network driver: This driver is used to connect containers directly to a physical network interface on the host machine. Containers connected to the Macvlan network can be assigned IP addresses from the same subnet as the host machine, which makes them appear as if they are directly connected to the physical network.

    5. None network driver: This driver disables networking for the container, which means it has no network interfaces and cannot communicate with other containers or the host machine.

Here are some commands for managing Docker networks:

  • docker network ls: Lists all the available networks.

  • docker network create <network-name>: Creates a new network with the specified name.

  • docker network inspect <network-name>: Displays detailed information about the network, including its driver, IP range, and connected containers.

  • docker network connect <network-name> <container-name>: Connects a container to a network.

  • docker network disconnect <network-name> <container-name>: Disconnects a container from a network.

By default, when you create a new container, it is connected to the default bridge network. You can also specify a different network when creating a container using the --network option.

Task-1

Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot

vi docker-compose.yml
  •   version : '3.9'
      services:
        flask-api:
          image: trainwithshubham/flask-micro-api:latest
          container_name: flask-micro-api
          ports:
            - "5000:5000"
    
        mongo-db:
          image: mongo
          container_name: mongo
          ports:
            - "27017:27017"
    
  • Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.

      docker-compose up -d
    

  • Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in deployment file for auto-scaling.

      docker-compose scale flask-api=3
    

    The above commands will start the services defined in the YAML file in detached mode and then scale the flask-api service to three instances. Docker Compose will automatically load balance incoming requests between the running instances.

  • Use the docker-compose ps command to view the status of all containers, and docker-compose logs to view the logs of a specific service.

      docker ps
      docker-compose logs
    

  • Use the docker-compose down command to stop and remove all containers, networks, and volumes associated with the application

      docker-compose down
    

Task-2

  • Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

    Docker Volumes are a way to store data in a container that can be shared with other containers. You can create a Docker Volume using the docker volume create command.

    example:

      docker volume create flaskapp
    

    This will create a new volume that can be mounted to a container using the --mount or -v option.

  • Named Volumes:

    Named Volumes are similar to Docker Volumes, but they have a name that can be used to reference them. You can create a Named Volume using the docker volume create command with the --name option.

    Example:

       docker volume create --name my_name_volume
    
  • Create two or more containers that read and write data to the same volume using the docker run --mount command.

      docker run -d --name container1 --mount
      source=my_name_volume,target=/data alpine sleep 3600 
    
      docker run -d --name container2 --mount source=my_name_volume,target=/data alpine sleep 3600
    

  • Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

  •   # Write a file in container1
      docker exec container1 sh -c 'echo "Hello World" > /data/file.txt'
    
      # Read the file in container2
      docker exec container2 cat /data/file.txt
    

  • Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.

      docker volume ls
    

      docker volume rm my_name_volume #this will remove volume
    

    Thankyou for reading........