Skip to main content

Hands-on with Docker and Docker-Compose

 Hands-on with Docker & Docker-Compose


(1) Create a "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose working-with-multiple-services/app", with base image "python:3.7" to dockerize the given Flask application "app.py"


(2) Create another "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/db", with base image "postgres" and configure the environment variables postgres user "abc", postgres password "abc@123", postgres db "postgres".


(3) Create one more "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/cache", with base image as "redis".


(4) Create a "docker-compose.yml" file inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services" with the below specifications,

    (4.1) Create three services named "app", "db" and "cache"

    (4.2) The "app" service, should build the "Dockerfile" present in the "app" directory with image named "python_img" and container named "web_container" with the port as "5000". Configure the environment variables db host "postgres" db password "abc@123" and redis host "cache".

    (4.3) The "db" service, should build the "Dockerfile" present in the "db" directory with image named "db_img" and container named "db" container with the port as "5444". Mount it to a volume named "db_data" with default docker volume path as source path and "/var/lib/postgresql/data" as destination path.

    (4.4) The "cache" service should build the "Dockerfile" present in the "cache" directory with image named "cache_img" and container named "cache_container" with the port as "6379". Mount it to a volume named "cache data" with default docker volume path as source path and "/data" as destination path.

    (4.5) Create a bridge network named "new-network" to connect all the services.

(5) Run the "docker-compose.yml" file and hit "http://localhost:5000" to check whether the Flask application is running successfully.


Lets get started...

We will need 3 folder inside "-/Desktop/Project/docker-docker-compose-working-with-multiple-services" create them using mkdir command

cd ~/Desktop/Project/docker-docker-compose-working-with-multiple-services
mkdir app
mkdir db
mkdir cache

# to check created folders
ls

Next we need to create three Dockerfiles in respective folders, naming should be Dockerfile for each under different folders

/app/Dockerfile

# Use python 3.7 as the base image
FROM python:3.7

# Set the working directory
WORKDIR /app

# Copy the Flask application code to the container
COPY app.py .

# Install dependencies
RUN pip install Flask

# Expose the port on which the application will run
EXPOSE 5000

# Start the Flask application
CMD ["python", "app.py"]

/db/Dockerfile

# Use postgres as the base image
FROM postgres

# Set the environment variables for PostgreSQL
ENV POSTGRES_USER abc
ENV POSTGRES_PASSWORD abc@123
ENV POSTGRES_DB postgres

/cache/Dockerfile

# Use redis as the base image
FROM redis

Next, here's an example of a docker-compose.yml file that you can create inside the path "/Desktop/Project/docker-docker-compose-working-with-multiple-services" to define the services, build Docker images, and configure the network:

docker-compose.yml

version: '3'
services:
  app:
    build: ./app
    image: python_img
    container_name: web_container
    ports:
      - "5000:5000"
    environment:
      DB_HOST: db
      DB_PASSWORD: abc@123
      REDIS_HOST: cache
	networks:
	  - new-network
  db:
    build: ./db
    image: db_img
    container_name: db_container
    ports:
      - "5444:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
	networks:
	  - new-network
  cache:
    build: ./cache
    image: cache_img
    container_name: cache_container
    ports:
      - "6379:6379"
    volumes:
      - cache_data:/data
	networks:
	  - new-network
networks:
  new-network:
    driver: bridge
volumes:
  db_data:
  cache_data:


To run and build docker-compose file

docker-compose up --build

or 

docker-compose build

docker-compose up

If you want to run the containers in the background (detached mode), you can add the -d flag:

docker-compose up -d


--- THANK YOU ---

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command: ...

Kubernetes1

  Challenge 1 Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully. Environment Setup Check whether docker & minikube are properly installed and configured. Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env) Step-1 Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp. Check if the pod creation is successful by running the command: kubectl get pod firstapp Step-2 Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp Step-3 Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be ...

Docker + Kubernetes + Ansible

  Docker + Kubernetes + Ansible ----------------------------------------------------------------------------------------------------------------------------- Web Application Deployment using Kubernetes and Ansible A Flask application named "application.py" is given in the path Run the given setup.sh file given in the path to install the required dependencies be the challenge. 1. Start Minikube and sync host Docker with Minikube Docker. Note: If you get any errors while starting Minikube, please do try again by running the command to start Minikube. 2. Create a Dockerfile in the path and dockerize the given 'Flask' application as 'webapp-img' using 'python:alpine3.7' as its base image. 3. Create a 'deployment.yml' file in the path to create a Kubernetes deployment object named "web-deployment" with 3 replicas which uses the 'webapp-img' and runs a container named "webapp-container". Add Label "app=webapp" an...