Day 18 Task: Docker for DevOps Engineers

Day 18 Task: Docker for DevOps Engineers

#Day18 of 90DaysofDevOps Challenge

Docker Compose?

  • Docker Compose is a tool that was developed to help define and share multi-container applications.

  • With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

  • There is a three-step process to work with Docker Compose.

    1. Define the application environment with Dockerfile for all services.
    2. Create a docker-compose.yml file defining all services under the application.
    3. Run docker-compose up to run all services under applications

What is YAML?

  • YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • YAML is a popular programming language because it is designed to be easy to read and understand.

  • It can also be used in conjunction with other programming languages. Because of its flexibility and accessibility, YAML is used by the Ansible® automation tool to create automation processes, in the form of Ansible Playbooks.

  • YAML files use a .yml or .yaml extension.

  • This is a very basic example of a YAML file:

      #Comment: This is a supermarket list using YAML
      #Note that - character represents the list
      ---
      food: 
        - vegetables: tomatoes #first list item
        - fruits: #second list item
            citrics: oranges 
            tropical: bananas
            nuts: peanuts
            sweets: raisins
    

Task-1

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file called docker-compose.yml to define the services, networks, and volumes required for your application. In this file, you can specify the environment variables, configure the services, and define the links between different containers. Let's go through the process step by step:

1. Install Docker Compose: First, make sure you have Docker Compose installed on your system. You can check this by running the following command:

docker-compose --version

If Docker Compose is not installed, you can follow the official documentation to install it for your operating system.

2. Create a docker-compose.yml file: Create a file named docker-compose.yml in your project directory. This is where you will define your services and their configurations.

3. Define services: Within the docker-compose.yml file, start by defining the services you want to run. Each service represents a container in your application. Here's an example of a basic docker-compose.yml file:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - 80:80
    environment:
      - ENV_VAR=value
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=secret

In this example, we have two services: web and db. The web service uses the nginx image and exposes port 80. It also sets an environment variable ENV_VAR with the value value. The db service uses the mysql image and sets the MYSQL_ROOT_PASSWORD environment variable.

4. Configure services: You can configure various aspects of your services, such as ports, volumes, environment variables, etc. For example, in the above docker-compose.yml file, the ports key maps the container's port 80 to the host's port 80. The environment key sets the environment variables for each service.

5. Define links and networks: Docker Compose automatically creates a default network for your application. Containers within the same network can communicate with each other using their service names as hostnames. You can also define additional networks and specify which services should be connected to them.

6. Use environment variables: To use environment variables within your docker-compose.yml file, you can specify them using the ${VARIABLE_NAME} syntax. You can define environment variables directly in the file or load them from an external .env file. For example:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - 80:80
    environment:
      - ENV_VAR=${MY_ENV_VAR}

Here, we are referencing the environment variable MY_ENV_VAR in the ENV_VAR field.

7. Start and manage your application: Once you have your docker-compose.yml file configured, you can start your application using the following command in your project directory:

docker-compose up

This will start all the defined services and create the necessary containers.

These are the basic steps to use a docker-compose.yml file to set up your environment, configure services, and use environment variables. Docker Compose provides many more features and options to manage your containers, volumes, networks, and more. I recommend referring to the Docker Compose documentation for detailed information on all the available options and configurations: https://docs.docker.com/compose/.

Task-2

  • Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use usermod command to give the user permission to docker). Make sure you reboot the instance after giving permission to user.

  • Inspect the container's running processes and exposed ports using the docker inspect command.

  • Use the docker logs command to view the container's log output.

  • Use the docker stop and docker start commands to stop and start the container.

  • Use the docker rm command to remove the container when you're done.

  • To pull a pre-existing Docker image from a public repository (such as Docker Hub), run it on your local machine as a non-root user, and perform the requested actions, follow the steps below:

    1. Install Docker: If Docker is not already installed on your machine, follow the official documentation to install Docker for your operating system.

    2. Give user permission to use Docker: By default, Docker requires root (superuser) access. To allow a non-root user to use Docker, you need to add the user to the docker group. Follow these steps: a. Add the user to the docker group:

      sudo usermod -aG docker <username>
    

    Replace <username> with the actual username of the non-root user you want to add.

    b. Reboot the machine to apply the group changes:

      sudo reboot
    

    After the reboot, the user will have permission to use Docker without root access.

    3. Pull and run a Docker image: a. Pull the Docker image from a public repository:

      docker pull <image_name>
    

    Replace <image_name> with the name of the Docker image you want to pull, such as nginx or ubuntu:latest.

    b. Run the container using the pulled image:

      docker run --name mycontainer -d <image_name>
    

    The --name flag assigns a name to the container for easy reference. The -d flag runs the container in detached mode.

    4. Inspect running processes and exposed ports: To inspect the container's running processes and exposed ports, use the docker inspect command:

      docker inspect mycontainer
    

    Replace mycontainer with the name or container ID of your container. The command will provide detailed information, including the processes running inside the container and the exposed ports.

    5. View container's log output: To view the container's log output, use the docker logs command:

      docker logs mycontainer
    

    Replace mycontainer with the name or container ID of your container. This command will display the log output generated by the container.

    6. Stop and start the container: To stop the running container, use the docker stop command:

      docker stop mycontainer
    

    To start the container again, use the docker start command:

      docker start mycontainer
    

    Replace mycontainer with the name or container ID of your container.

    7. Remove the container: When you are done with the container, you can remove it using the docker rm command:

      docker rm mycontainer
    

    Replace mycontainer with the name or container ID of your container.

    Remember to replace <image_name> and mycontainer with the appropriate values for your setup.

    How to run Docker commands without sudo?

    • Make sure docker is installed and the system is updated

    • sudo usermod -a -G docker $USER

    • Reboot the machine.