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