Showing posts with label YAML. Show all posts
Showing posts with label YAML. Show all posts

Nov 26, 2022

Docker Compose 102

Let's follow the steps to create more complex docker images:

  • frontend - wordpress image
  • backend - mysql image


First, we just start a docker images with Multipass.

PS> multipass launch docker -n kiko

Login to docker (kiko) and start creating docker-compose.yaml.

PS> multipass shell kiko

ubuntu@kiko:~$ mkdir blog && cd blog

ubuntu@kiko:~/blog$ vi docker.compose.yaml

---------------------------------------------------

version: "3"
services:
  frontend:
    image: wordpress
    ports:
      - "8089:80"
    depends_on:
      - backend
    environment:
      WORDPRESS_DB_HOST: backend
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "coffee"
      WORDPRESS_DB_NAME: wordpress
    networks:
      dmz:
        ipv4_address: "192.168.33.89"
  backend:
    image: "mysql:5.7"
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: "coffee"
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      dmz:
        ipv4_address: "192.168.33.90"
networks:
  dmz:
    ipam:
      driver: default
      config:
        - subnet: "192.168.33.0/24"

----------------------------------------------------

ubuntu@kiko:~/blog$ docker-compose up -d 

ubuntu@kiko:~/blog$ docker-compose ps

ubuntu@kiko:~/blog$ docker network ls

ubuntu@kiko:~/blog$ docker inspect blog_dmz

 

http://kiko.mshome.net:8089/


Links:

  • Docker Compose 101

Nov 24, 2022

Docker Compose 101

Let's follow the steps to create first docker images.


First, we just start a docker images with Multipass.

PS> multipass launch docker -n kiko

Login to docker (kiko) and start creating docker-compose.yaml.

PS> multipass shell kiko

ubuntu@kiko:~$ mkdir coffee && cd coffee

ubuntu@kiko:~/coffee$ vi docker.compose.yaml

---------------------------------------------------

version: "3"
services:
  website:
    image: nginx
    ports:
      - "8081:80"
    restart: always 

----------------------------------------------------

ubuntu@kiko:~/coffee$ docker-compose up -d 

ubuntu@kiko:~/coffee$ docker-compose ps

Add second image with different network (coffee).

ubuntu@kiko:~/coffee$ vi docker.compose.yaml

---------------------------------------------------

version: "3"
services:
  website:
    image: nginx
    ports:
      - "8081:80"
    restart: always
  website2:
    image: nginx
    ports:
      - "8082:80"
    restart: always
    networks:
      coffee:
        ipv4_address: 192.168.92.22
networks:
  coffee:
    ipam:
      driver: default
      config:
        - subnet: "192.168.92.0/24"

----------------------------------------------------

ubuntu@kiko:~/coffee$ docker-compose up -d 

ubuntu@kiko:~/coffee$ docker network ls 

ubuntu@kiko:~/coffee$ docker inspect coffee_default 

ubuntu@kiko:~/coffee$ docker inspect coffee_coffee


Links:

Sep 30, 2022

Customized a Node.js App in Multipass

This is a short tutorial to:

  • Create an Ubuntu VM with cloud-init file
  • Install a running Node.js app with a Nginx as the front-end proxy


Cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. It can be used to install packages and write files, or to configure users and security hardening. 

Cloud-init also works across distributions which means it automatically uses the native package management tool for the distro you select.

#cloud-config
package_upgrade: true
packages:
  - nginx
  - nodejs
  - npm
write_files:
  - owner: www-data:www-data
    path: /etc/nginx/sites-available/default
    content: |
      server {
        listen 80;
        location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection keep-alive;
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
  - owner: webadm:webadm
    path: /home/webadm/myapp/index.js
    content: |
      var express = require('express')
      var app = express()
      var os = require('os');
      app.get('/', function (req, res) {
        res.send('Hello World from host ' + os.hostname() + '!')
      })
      app.listen(3000, function () {
        console.log('Hello world app listening on port 3000!')
      })
runcmd:
  - service nginx restart
  - cd "/home/webadm/myapp"
  - npm init
  - npm install express -y
  - nodejs index.js


Save the YAML as webadmin.yaml and then start creating the VM with Multipass as below:

multipass -n nodejs3k --cloud-init webadmin.yaml

multipass info nodejs3k

Last, open the URL to http://nodejs3k_ip_addr with your browser.


Links:

Sep 28, 2022

My Third Try on Multipass

After a few rounds of testing, you will find yourself repeating a lot of similar actions whenever creating an instance. For example:

  • creating new user
  • adding ssh public key
  • install Python, htop, Nodejs.....

YES, we can pre-configure all the actions every time a new instance is created. This is done via cloud-init.


First, create a YAML file called "cloud_init.yaml" with the following content.

users:
  - default
  - name: xx
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh_authorized_keys:
      - ssh-rsa <rsa keys in one line>
package_update: true
package_upgrade: true
packages:
  - nodejs
  - python3

Second, create a new instance.

multipass launch -c 2 -m 2G -d 10G -n jimny --cloud-init cloud_init.yaml

Last, login to new instance and you will find that apt is both updated and upgraded. Nodejs and Python3 are there waiting for you.

multipass info jimny

multipass shell jimny

ubuntu@jimny:~$ apt list python3 nodejs 

May 18, 2022

Docker HelloWorld_2

Continue from the hello_world_1, here comes another notes on running multiple containers with docker compose.

With Docker compose, we can configure and start multiple containers with a single yaml file. This is helpful for a technology stack with multiple technologies.

As an example, say that you are working on a project that uses a MySQL database, Python for AI/ML, NodeJS for real-time processing, and .NET for serving API’s. Docker makes this easier with the help of compose.

docker compose is a yaml file in which we can configure different types of services. Then with a single command all containers will be built and fired up.

There are 3 main steps involved in using compose:    

  • Generate a Dockerfile for each project.    
  • Setup services in the docker-compose.yml file.    
  • Fire up the containers.

Here is the full copy of docker-compose.yml.

docker-compose.yml:

version: '3.4'
services:
  super-app-db:
    image: mysql:8.0.28
    environment:
      MYSQL_DATABASE: 'super-app'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - '3306:3306'
    expose:
      - '3306'
  super-app-node:
    build: ./node
    ports:
      - "3000:3000"
  super-app-dotnet:
    build: ./dotnet
    ports:
    - "8080:80"
  super-app-python:
    build: ./python
  super-app-php:
    build: ./php
    ports:
    - "8000:80"

Then, we need to:

  • Configure MySQL
  • Configure NodeJS
  • Configure .NET 
  • Configure Python
  • Configure PHP

 

Links:

May 3, 2022

YAML Tutorial

YAML stands for “YAML Ain’t markup language” is a data serialization language which is designed to be human -friendly and works well with other programming languages for everyday tasks. 

Features:

  • YAML data is portable between programming languages
  • Includes data consistent data model
  • Easily readable by humans
  • Supports one-direction processing
  • YAML is case sensitive
  • The files should have .yaml as the extension
  • YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead


Links: