Docker for DevOps Engineers: Mastering Docker Compose and Containerization
In this blog, I'll be diving into some essential Docker concepts for DevOps engineers. Today’s focus is on Docker Compose and running containers as non-root users.
What is Docker Compose?
Docker Compose is a powerful tool used to define and share multi-container applications. With Docker Compose, we can create a docker-compose.yml
file to define the services required by our application and link them together. With just a single command, we can spin up or tear down the entire environment.
Key Benefits of Docker Compose:
Multi-container management: You can define multiple services that work together in a single YAML file.
Ease of use: With a single command, all the necessary services for the application are up and running.
Configuration management: You can easily manage the configuration of all the services involved in your project.
Learn more about Docker Compose from this tutorial.
What is YAML?
YAML stands for "YAML Ain’t Markup Language" and is commonly used for writing configuration files. It's human-readable and makes managing configurations simpler. YAML files use .yml
or .yaml
extensions.
If you want to get deeper into YAML, you can read more about it here.
Task 1: Setting Up the Environment with Docker Compose
In this task, we’ll set up a Spring Boot application using MySQL as the database. We'll define the services, configure the environment, and link the containers together using Docker Compose.
Steps:
Clone the Git Repository:
Clone the repository that contains the application code:
git clone https://github.com/LondheShubham153/Springboot-BankApp.git
Create the Dockerfile:
Create a
Dockerfile
for building and deploying the Spring Boot application:# Stage 1: Build the application FROM maven:3.8.3-openjdk-17 as builder WORKDIR /app COPY . /app RUN mvn clean install -DskipTests=true # Stage 2: Deploy the application FROM openjdk:17-alpine as deployer COPY --from=builder /app/target/*.jar /app/target/bankapp.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]
Create the docker-compose.yml:
Create a
docker-compose.yml
file to configure the MySQL and Spring Boot services:version: "3.8" services: mysql: image: mysql:latest container_name: Mysql environment: - MYSQL_DATABASE=BankDB - MYSQL_ROOT_PASSWORD=test@123 volumes: - ./mysql-data:/var/lib/mysql networks: - bankapp healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] interval: 10s timeout: 5s retries: 3 start_period: 30s bankapp: build: . container_name: Bankapp environment: - SPRING_DATASOURCE_USERNAME=root - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - SPRING_DATASOURCE_PASSWORD=test@123 ports: - "8080:8080" depends_on: mysql: condition: service_healthy networks: - bankapp restart: always healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"] interval: 10s timeout: 5s retries: 5 start_period: 30s networks: bankapp: volumes: mysql-data:
Running the Application:
Use the following command to bring up the environment:
docker-compose up -d
Accessing the Application:
Open the inbound rule of your instance to allow traffic on port 8080.
Task 2: Running Containers as a Non-Root User
In this task, we’ll learn how to run Docker containers as a non-root user, ensuring that Docker commands can be executed without sudo
.
Steps:
Create a User and Add to Docker Group:
Create a new user
amitabh
and add them to the Docker group:sudo usermod -aG docker amitabh newgrp docker
This ensures that
amitabh
can execute Docker commands without needingsudo
.Pull and Run a Custom Docker Image:
Pull a custom image:
docker pull amitabhdevops/notes-app
Run the container:
docker run -d -p 8000:8000 --name notes-app amitabhdevops/notes-app
Inspecting the Container:
After running the container, you can inspect it using:
docker inspect notes-app
Viewing Container Logs:
To view the container logs:
docker logs notes-app
Stopping and Starting the Container:
To stop the container:
docker stop notes-app
To start it again:
docker start notes-app
Removing the Container:
Once done, remove the container:
docker stop notes-app && docker rm notes-app
Conclusion:
Docker Compose simplifies the management of multi-container applications by defining all services in a single YAML file. Running containers as non-root users adds an extra layer of security and allows developers to work without sudo
for Docker commands. These tasks are essential for DevOps engineers working with containerized applications.
Feel free to check out the video tutorial for further guidance.
For more updates, connect with me on LinkedIn.