Docker for DevOps Engineers

Docker for DevOps Engineers

Day 18 : 90Days of DevOps Challenge

ยท

5 min read

What is Docker-Compose ?

Docker Compose is a tool that allows you to define and run multi-container Docker applications. It provides a way to manage multiple Docker containers as a single application, making it easier to orchestrate and configure complex applications that require multiple services or components.

With Docker Compose, you can define your application's services, networks, and volumes in a YAML file called docker-compose.yml. Each service represents a container that runs a specific component of your application, such as a web server, a database, or a message queue. You can specify various configuration options for each service, including the Docker image to use, environment variables, networking, and volumes.

By using Docker Compose, you can define the relationships between different services in your application, specify dependencies, and easily scale or replicate containers as needed. It simplifies the process of setting up and managing multi-container environments, especially for development, testing, and staging environments.

Docker Compose provides a command-line interface (CLI) that allows you to start, stop, and manage your application using simple commands. You can spin up all the containers defined in your docker-compose.yml file with a single command, and Docker Compose takes care of creating the necessary networks and volumes, linking the containers, and ensuring they can communicate with each other.

Overall, Docker Compose is a powerful tool for defining, running, and managing multi-container applications, making it easier to develop, deploy, and scale complex containerized environments.

What is YAML ?

YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format. It stands out for its simplicity and readability, making it popular for configuration files, data exchange, and other structured data needs. YAML is often used in combination with programming languages, including Python, Ruby, and JavaScript.

Here are some key characteristics of YAML:

  1. Structure: YAML uses indentation and whitespace to define the structure of data. It relies on proper indentation levels to determine the hierarchy of elements, making it visually intuitive.

  2. Data Types: YAML supports various data types, including scalars (strings, numbers, booleans), sequences (arrays/lists), and mappings (key-value pairs). It also allows for complex data structures through nesting and combining different types.

  3. Readability: YAML emphasizes human-readability by using a clean and straightforward syntax. It avoids unnecessary punctuation and allows for comments, making it easy to understand and maintain.

  4. Relationships: YAML enables the definition of relationships between data elements using references and anchors. References allow you to refer to other elements within the YAML file, while anchors help you define reusable data fragments.

  5. Integration: YAML is widely supported in different programming languages and tools. Many frameworks and libraries provide built-in support for parsing and generating YAML files, making it easy to integrate with existing systems.

YAML is commonly used for configuration files, especially in systems like Docker Compose, Kubernetes, Ansible, and many other applications and tools within the DevOps and infrastructure automation ecosystem. Its readability and simplicity make it a popular choice for expressing configuration options and data structures in a human-friendly way.

Task :

-> Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

  • Install docker-compose

  • Create a docker-compose.yml file inside the project folder

    • version: "3.3" denotes that we are using version 3.3 of Docker Compose.

    • The services section defines all the different containers we will create.

    • The build keyword specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.

    • The image keyword is used to specify the image from the docker hub for MySQL containers

    • For the database and web, we are using the ports keyword to mention the ports that need to be exposed.

    • And then, we also specify the environment variables for MySQL which are required to run MySQL.

  • Run docker-compose.yml file

    • docker-compose up: This command does the work of the docker-compose build and docker-compose run commands.

    • -d: open in detached mode.

    • - docker ps or docker-compose ps command list all the containers in the current docker-compose file.

  • Verify that the application is working by accessing it in a web browser.

  • docker-compose down This command stops all the services and cleans up the containers, networks, and images.

-> 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. Run the container as a non-root user (Hint- Use usermod command to give the user permission to docker). Make sure you reboot instance after giving permission to user.

- For running container as a non-root user, use command usermod to give user permission to docker

sudo usermod -a -G docker $USER

Then reboot instance after giving permission to user.

sudo reboot

Copy docker image command from dockerhub

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

docker inspect <container_name or ID>

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

docker logs <container_name or ID>

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

docker stop: To stop one or more running Docker containers.

docker stop <container-name or ID>

docker start: Start one or more stopped containers

docker start <container-name or ID>

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

docker rm: Remove one or more containers.

--force, -f: Force the removal of a running container.

docker rm <container_name or ID>

Thank You for Reading My Blog.. ๐Ÿ‘

Connect with me : linkedin.com/in/shivraj-salunkhe-5881141a4

Follow my Blog channel : shivrajofficial.hashnode.dev

ย