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: ...

Build Flask image using docker & docker-compose

 Build Flask Image using docker & docker-compose 1. Create a Dockerfile to build Flask image with Flask application, 'requirements.txt' and expose it in port 5000. 2. Create a 'docker-compose.json' file to create two services named 'python_web_app' and 'db_service' 3. The 'python_web_app' service  should be built from Dockerfile and specify the port as 5000 4. the 'db_service' should be built from image 'redis:alpine' 5. Add a volume to the 'python_web_app' service that mounts the current directory to the working directory inside the container and set the flask envireonment in debug mode. 6. Visit 'http:localhost:5000" to check weather the Flask application is running fine. The container name of 'python_web_app' service should be 'web_container' and container name of 'db_service' should be 'db_container' Here are the instructions for each of the tasks mentioned: Create a file na...

Building Docker Image with Maven

 Building Docker Image with Maven You are a DevOps Engineer working for ABC Infotech. A client has requested you to build a Docker image using Maven plugin, so that when you package your Maven project. Docker image will be built, and the generated artifacts will be deployed in local git repository  1. Create a Maven java project using CLI with the following configurations - groupId=javaApp  - artifactId maven-docker-image  2. Replace the 'Hello World!' message in the 'App.java' file with 'Hurray! You have successfully built a docker image using Maven' 3. Configure your 'pom.xml' to deploy the generated artifacts in the local git repository to build a docker image 4. The project should be deployed in the local repositary with the following details: - id = maven.repo - name = Maven Artifacts - URL = file:///home/labuser/Desktop/Remote/mavenArtifacts 5. The name of the jar file, repository name af the image should be same as the project artifactId...