Docker Compose Networking

Docker Compose Networking

Artikel: Manage Docker Network


Membuat custom network pada docker compose

  • Buat folder wp-net
root@sys-ops:~# mkdir wp-net && cd wp-net
  • Buat file docker-compose.yml
root@sys-ops:~/wp-net# nano docker-compose.yml
  • contianer: s_mysql dengan image mysql:8.0 menggunakan network: sysnet dengan static ip: 192.168.88.10
  • container: s_wordpress dengan image wordpress:latest menggunakan network: sysnet dengan ip dhcp
  • network: sysnet menggunakan driver bridge dengan subnet: 192.168.88.0/24 dan gateway: 192.168.88.1
version: '3.0'
services:
   s_mysql:
     image: mysql:8.0
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: pass_root
       MYSQL_DATABASE: db_wp
       MYSQL_USER: user_wp
       MYSQL_PASSWORD: pass_wp
     networks:
       sysnet:
         # static ip address
         ipv4_address: 192.168.88.10

   s_wordpress:
     depends_on:
       - s_mysql
     image: wordpress:latest
     volumes:
       - wordpress_data:/var/www/html
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: s_mysql:3306
       WORDPRESS_DB_USER: user_wp
       WORDPRESS_DB_PASSWORD: pass_wp
       WORDPRESS_DB_NAME: db_wp
     networks:
       # dhcp ip address
       - sysnet

volumes:
    db_data: {}
    wordpress_data: {}

networks:
    sysnet:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 192.168.88.0/24
            gateway: 192.168.88.1
  • Jalankan docker-compose
  • Network dan container berhasil di buat dan dijalankan tanpa error
root@sys-ops:~/wp-net# docker-compose up -d

[+] Running 5/5
 ⠿ Network wp-net_sysnet           Created                                                                                                                                                           0.2s
 ⠿ Volume "wp-net_wordpress_data"  Created                                                                                                                                                           0.0s
 ⠿ Volume "wp-net_db_data"         Created                                                                                                                                                           0.0s
 ⠿ Container wp-net-s_mysql-1      Started                                                                                                                                                           3.0s
 ⠿ Container wp-net-s_wordpress-1  Started
  • Cek status container
root@sys-ops:~/wp-net# docker-compose ps

NAME                   COMMAND                  SERVICE             STATUS              PORTS
wp-net-s_mysql-1       "docker-entrypoint.s…"   s_mysql             running             33060/tcp
wp-net-s_wordpress-1   "docker-entrypoint.s…"   s_wordpress         running             0.0.0.0:8080->80/tcp, :::8080->80/tcp
  • Cek IP address container wp-net-s_mysql-1 dan wp-net-s_wordpress-1
  • wp-net-s_mysql-1 menggunakan IP 192.168.88.10 (static IP sesuai dengan config pada docker-compose)
  • wp-net-s_wordpress-1 menggunakan IP 192.168.88.2 (dhcp IP/random sesuai dengan config pada docker compose)
root@sys-ops:~/wp-net# docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp-net-s_mysql-1
192.168.88.10

root@sys-ops:~/wp-net# docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp-net-s_wordpress-1
192.168.88.2
  • Akses container wordpress dengan url: http://ip_address_server:8080


Menggunakan Existing network pada docker compose

  • Cek list docker network, network yang akan digunakan adalah sys-ops_network
root@sys-ops:~/wp-net# docker network ls
NETWORK ID     NAME                            DRIVER    SCOPE
18794b121cf8   bridge                          bridge    local
3df78f9a3f97   host                            host      local
1c4eccfb7e53   nextcloud_default               bridge    local
1d0f1664bc56   none                            null      local
3e12e0f54c58   sys-ops_network                 bridge    local
66e63c30ac25   sys-opsbikininwebsite_default   bridge    local
7cabc1d0720b   wp-net_sysnet                   bridge    local
  • Cek info detail network: sys-ops_network, menggunakan subnet 192.168.100.0/24
root@sys-ops:~/wp-net# docker network inspect sys-ops_network
[
    {
        "Name": "sys-ops_network",
        "Id": "3e12e0f54c58d7653398c8d6c917e7db8a2f40472d6cac8ae5cf8ea57878b7b6",
        "Created": "2022-05-08T08:50:57.218497245Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.100.2/24",
                    "Gateway": "192.168.100.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
  • Buat folder wp-net1
root@sys-ops:~# mkdir wp-net1 && cd wp-net1
  • Buat file dokcer-compose.yml
root@sys-ops:~/wp-net1# nano docker-compose.yml
  • contianer: s_mysql dengan image mysql:8.0 menggunakan network: default dengan static ip: 192.168.88.10
  • container: s_wordpress dengan image wordpress:latest menggunakan network: default dengan ip dhcp
  • network: default terkoneksi ke external network sys-ops_network, jadi tidak akan membuat network baru dengan nama default tapi hanya akan menggunakan network yang sudah ada yaitu sys-ops_network
version: '3.0'
services:
   s_mysql:
     image: mysql:8.0
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: pass_root
       MYSQL_DATABASE: db_wp
       MYSQL_USER: user_wp
       MYSQL_PASSWORD: pass_wp
     networks:
       default:
         # static ip address
         ipv4_address: 192.168.100.10

   s_wordpress:
     depends_on:
       - s_mysql
     image: wordpress:latest
     volumes:
       - wordpress_data:/var/www/html
     ports:
       - "8586:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: s_mysql:3306
       WORDPRESS_DB_USER: user_wp
       WORDPRESS_DB_PASSWORD: pass_wp
       WORDPRESS_DB_NAME: db_wp
     networks:
       # dhcp ip address
       - default

volumes:
    db_data: {}
    wordpress_data: {}

networks:
    default:
      external: true
      name: sys-ops_network
  • Jalankan docker-compose
  • container berhasil di buat dan dijalankan tanpa error, tidak ada network yang dibuat, karena sudah menggunakan network yang sudah ada sebelumnya
root@sys-ops:~/wp-net1# docker-compose up -d

[+] Running 4/4
 ⠿ Volume "wp-net1_db_data"         Created                                                                                                                                                          0.5s
 ⠿ Volume "wp-net1_wordpress_data"  Created                                                                                                                                                          0.3s
 ⠿ Container wp-net1-s_mysql-1      Started                                                                                                                                                         12.5s
 ⠿ Container wp-net1-s_wordpress-1  Started
  • Cek status container
root@sys-ops:~/wp-net1# docker-compose ps
NAME                    COMMAND                  SERVICE             STATUS              PORTS
wp-net1-s_mysql-1       "docker-entrypoint.s…"   s_mysql             running             33060/tcp
wp-net1-s_wordpress-1   "docker-entrypoint.s…"   s_wordpress         running             0.0.0.0:8586->80/tcp, :::8586->80/tcp
  • Cek IP address container wp-net-s_mysql-1 dan wp-net-s_wordpress-1
  • wp-net-s_mysql-1 menggunakan IP 192.168.100.10 (static IP sesuai dengan config pada docker-compose)
  • wp-net-s_wordpress-1 menggunakan IP 192.168.100.2 (dhcp IP/random sesuai dengan config pada docker compose)
root@sys-ops:~/wp-net1# docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp-net1-s_mysql-1
192.168.100.10

root@sys-ops:~/wp-net1# docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp-net1-s_wordpress-1
192.168.100.2
  • Cek info detail network: sys-ops_network
root@sys-ops:~/wp-net# docker network inspect sys-ops_network
[
    {
        "Name": "sys-ops_network",
        "Id": "3e12e0f54c58d7653398c8d6c917e7db8a2f40472d6cac8ae5cf8ea57878b7b6",
        "Created": "2022-05-08T08:50:57.218497245Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.100.2/24",
                    "Gateway": "192.168.100.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "246b9dbaff3a55526b4d614b522e91898973cbdf1d90ab8b796e29c5edb55500": {
                "Name": "wp-net1-s_wordpress-1",
                "EndpointID": "7a60625bdd0e7e3de0f3b5ee2540e5a8a1becef8b0b54c53425bc42772d7e44a",
                "MacAddress": "02:42:c0:a8:64:02",
                "IPv4Address": "192.168.100.2/24",
                "IPv6Address": ""
            },
            "f8e44d49d13526a1c202d41467d1bc3f77bc68783a7ca1407fbe34fe72a2a691": {
                "Name": "wp-net1-s_mysql-1",
                "EndpointID": "84f6dc55cef130f29c8bd66caa232b0d95fafe4e65c0ea109f99438460db38a0",
                "MacAddress": "02:42:c0:a8:64:0a",
                "IPv4Address": "192.168.100.10/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
  • Akses container wordpress dengan url: http://ip_address_server:8586

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.