How to Dockerize an App ?

sbalasa

Santhosh Balasa

Posted on May 31, 2021

How to Dockerize an App ?

-> Create a Dockerfile if there is an argument need to be passed to your app

FROM python:3.7
ADD . /srv
WORKDIR /srv
ARG file
RUN pip install -r requirements.txt
CMD ["python", "main.py"]
Enter fullscreen mode Exit fullscreen mode

-> Build it

docker build -t app_name .
Enter fullscreen mode Exit fullscreen mode

-> Run it by passing its arguments

sudo docker run -e file=nl-ams02a-rw1_20181105_084407.json obs3-meta
Enter fullscreen mode Exit fullscreen mode

-> Sample docker-compose file to create a mysql db service

version: '3.1'
networks:
  backend:
    driver: bridge
services:
  db:
    command: mysqld --user=root --verbose
    container_name: mysql_db
    environment:
      MYSQL_DATABASE: santee
      MYSQL_PASSWORD: santee
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: santee
    image: mysql:8.0.15
    networks:
      backend: null
    ports:
    - 3306:3306/tcp
    volumes:
    - ./mysql_dump/santee.sql:/docker-entrypoint-initdb.d/santee.sql:rw
volumes:
  container-volume: {}

Enter fullscreen mode Exit fullscreen mode

-> Assigning a Static ip to docker container

#docker-compose file

version: '3.1'
networks:
  obs_network:
    ipam:
      config:
        - subnet: 172.20.0.0/16
services:
  redis:
    image: redis
    ports:
      - 6379:6379
    networks:
      obs_network:
        ipv4_address: 172.20.128.2
  db:
    command: mysqld --user=root --verbose
    environment:
      MYSQL_DATABASE: obs3_meta
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: obs3-meta
    image: mysql:5.7
    networks:
      obs_network:
        ipv4_address: 172.20.128.4
    ports:
    - 3306:3306/tcp
    volumes:
    - ./obs3-meta/mysql_dump/obs3_meta.sql:/docker-entrypoint-initdb.d/obs3_meta.sql:rw
  kafka:
    depends_on:
    - zookeeper
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      ZOOKEEPER_IP: zookeeper
    image: ches/kafka:latest
    networks:
      obs_network:
        ipv4_address: 172.20.128.5
    ports:
    - 7203:7203/tcp
    - 9092:9092/tcp
  zookeeper:
    image: zookeeper:3.4
    networks:
      obs_network:
        ipv4_address: 172.20.128.6
    ports:
    - 2181:2181/tcp
volumes:
  container-volume: {}
Enter fullscreen mode Exit fullscreen mode

-> Display all the ip address of the containers from its docker-compose file

$for s in `docker-compose ps -q`; do echo ip of `docker inspect -f "{{.Name}}" $s` is `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $s`; done
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
sbalasa
Santhosh Balasa

Posted on May 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related