この記事は約26分8秒で読むことができます。

dockerのフォルダ構成見直してみた話

フォルダ構成

tmplにはサービスごとにフォルダきってtmplateファイル作成しておく。必要なら。各コンテナに配備する。今回のサービスはssh。httpが必要なら都度フォルダきる。

コード表示

[oracle@centos tadan]$ tree
.
├── Dockerfile
├── Makefile
├── docker-compose.yml
├── scripts
│   ├── env
│   │   ├── cmn
│   │   │   ├── path.sh
│   │   │   └── tz.sh
│   │   └── ssh
│   ├── inst
│   │   ├── cmn
│   │   │   └── yum_install.sh
│   │   └── ssh
│   │       └── yum_install.sh
│   ├── main
│   │   ├── cmn
│   │   └── ssh
│   │       ├── create_dir.sh
│   │       ├── create_grp.sh
│   │       ├── create_pwd.sh
│   │       ├── create_usr.sh
│   │       └── define_seq.sh
│   ├── post
│   │   ├── cmn
│   │   └── ssh
│   └── pre
│       ├── cmn
│       └── ssh
├── share
│   ├── saba1
│   │   └── ssh
│   │       └── tmpl
│   ├── saba2
│   │   └── ssh
│   │       └── tmpl
│   ├── saba3
│   │   └── ssh
│   │       └── tmpl
│   ├── saba4
│   │   └── ssh
│   │       └── tmpl
│   ├── saba5
│   │   └── ssh
│   │       └── tmpl
│   └── saba6
│       └── ssh
│           └── tmpl
└── tmpl
    └── ssh
        ├── config
        └── genkey.sh

37 directories, 14 files

フォルダ作成

コード表示

[oracle@centos ~]$ mkdir tadan
[oracle@centos ~]$ cd tadan
[oracle@centos tadan]$ mkdir -p ./scripts/{env,inst,main,post,pre}/{cmn,ssh}
[oracle@centos tadan]$ mkdir -p tmpl/{ssh}

Dockerfile

コード表示

[oracle@centos tadan]$ cat D*
FROM centos:latest

ENV MNT_DIR=/mnt
ENV ENV_DIR=${MNT_DIR}/env
ENV ENV_CMN_DIR=${ENV_DIR}/cmn
ENV ENV_SSH_DIR=${ENV_DIR}/ssh
ENV INST_DIR=${MNT_DIR}/inst
ENV INST_CMN_DIR=${INST_DIR}/cmn
ENV INST_SSH_DIR=${INST_DIR}/ssh
ENV PRE_DIR=${MNT_DIR}/pre
ENV PRE_CMN_DIR=${PRE_DIR}/cmn
ENV PRE_SSH_DIR=${PRE_DIR}/ssh
ENV MAIN_DIR=${MNT_DIR}/main
ENV MAIN_CMN_DIR=${MAIN_DIR}/cmn
ENV MAIN_SSH_DIR=${MAIN_DIR}/ssh
ENV POST_DIR=${MNT_DIR}/post
ENV POST_CMN_DIR=${POST_DIR}/cmn
ENV POST_SSH_DIR=${POST_DIR}/ssh

COPY ./scripts ${MNT_DIR}

RUN find ${INST_CMN_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN find ${INST_SSH_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN find ${PRE_CMN_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN find ${PRE_SSH_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN find ${MAIN_CMN_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN [ -e ${MAIN_SSH_DIR}/define_seq.sh ] && chmod u+x ${MAIN_SSH_DIR}/define_seq.sh && ${MAIN_SSH_DIR}/define_seq.sh
RUN find ${POST_CMN_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done
RUN find ${POST_SSH_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && chmod u+x ${line} && bash -c ${line};done

USER oracle
WORKDIR /home/oracle

EXPOSE 20
CMD ["/sbin/init"]

Makefile

コード表示

[oracle@centos tadan]$ cat M*
CMD=docker-compose
up:
	@$(CMD) up -d
down:
	@$(CMD) down

docker-compose.yml

コード表示

[oracle@centos tadan]$ cat d*
version: '3.7'
services:
  saba1:
    image: centos_aine
    container_name: saba1
    hostname: saba1
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba1/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_1:
        ipv4_address: 192.168.100.101
    ports:
      - '1:22'
  saba2:
    image: centos_aine
    container_name: saba2
    hostname: saba2
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba2/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_1:
        ipv4_address: 192.168.100.102
    ports:
      - '2:22'
  saba3:
    image: centos_aine
    container_name: saba3
    hostname: saba3
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba3/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_1:
        ipv4_address: 192.168.100.103
    ports:
      - '3:22'
  saba4:
    image: centos_aine
    container_name: saba4
    hostname: saba4
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba4/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_2:
        ipv4_address: 192.168.200.101
    ports:
      - '4:22'
  saba5:
    image: centos_aine
    container_name: saba5
    hostname: saba5
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba5/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_2:
        ipv4_address: 192.168.200.102
    ports:
      - '5:22'
  saba6:
    image: centos_aine
    container_name: saba6
    hostname: saba6
    privileged: true
    volumes:
      -  /home/oracle/tadan/share/saba6/ssh:/home/oracle/.ssh
      -  /home/oracle/tadan/tmpl/ssh:/home/oracle/.ssh/tmpl
    networks:
      saba_net_2:
        ipv4_address: 192.168.200.103
    ports:
      - '6:22'
networks:
  saba_net_1:
    name: saba_net_1
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.100.0/24
  saba_net_2:
    name: saba_net_2
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.200.0/24

/tmpl/ssh/genkey.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*gen*")
#!/bin/bash
USR="$1"
shift
NM="$@"
PWD=""
expect -c "
spawn ssh-keygen -f /home/${USR}/.ssh/${NM} -t rsa
expect \"Enter\"
send \"${PWD}\n\"
expect \"Enter\"
send \"${PWD}\n\"
expect \"Enter\"
send \"${PWD}\n\"
expect \"\\\$\"
exit 0
"

/scripts/main/ssh/create_usr.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*usr*")
#!/bin/bash
useradd -m -g docker -u 1000 oracle

/scripts/main/ssh/create_dir.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*dir*")
#!/bin/bash
mkdir -p /home/oracle/.ssh

/scripts/main/ssh/create_grp.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*grp*")
#!/bin/bash
groupadd -g 1001 docker

/scripts/main/ssh/create_pwd.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*pwd*")
#!/bin/bash
echo 'ORACLE_PWD' | passwd --stdin oracle
echo 'ORACLE_PWD' | passwd --stdin root

/scripts/main/ssh/define_seq.sh

sourceコマンドで順番を制御できる。実行順序をここに集約できるからいいとおもう。

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*seq*")
#!/bin/bash
find ${ENV_CMN_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && source ${line};done
find ${ENV_SSH_DIR} -name "*.sh" | while read line;do [ -e ${line} ] && source ${line};done
[ -e ${MAIN_SSH_DIR}/create_grp.sh ] && source ${MAIN_SSH_DIR}/create_grp.sh
[ -e ${MAIN_SSH_DIR}/create_usr.sh ] && source ${MAIN_SSH_DIR}/create_usr.sh
[ -e ${MAIN_SSH_DIR}/create_pwd.sh ] && source ${MAIN_SSH_DIR}/create_pwd.sh
[ -e ${MAIN_SSH_DIR}/create_dir.sh ] && source ${MAIN_SSH_DIR}/create_dir.sh

/scripts/env/cmn/path.sh

これはあってもなくてもいいかも。dockerfileでもENVしているし。

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*path*")
#!/bin/bash
MNT_DIR=/mnt
ENV_DIR=${MNT_DIR}/env
INST_DIR=${MNT_DIR}/inst
PRE_DIR=${MNT_DIR}/pre
MAIN_DIR=${MNT_DIR}/main
MAIN_CMN_DIR=${MAIN_DIR}/cmn
MAIN_SSH_DIR=${MAIN_DIR}/ssh
POST_DIR=${MNT_DIR}/post

/scripts/env/cmn/tz.sh

コード表示

[oracle@centos tadan]$ cat $(find $(pwd) -name "*tz*")
#!/bin/bash
TZ=Asia/Tokyo

/scripts/inst/cmn/yum_install.sh

コード表示

[oracle@centos tadan]$ cat /home/oracle/tadan/scripts/inst/cmn/yum_install.sh
#!/bin/bash
yum install -y iputils \
yum install -y net-tools \
yum install -y iproute \
yum install -y vim \
yum install -y tree \
yum install -y lsof \
yum install -y expect

/scripts/inst/ssh/yum_install.sh

コード表示

[oracle@centos tadan]$ cat /home/oracle/tadan/scripts/inst/ssh/yum_install.sh
#!/bin/bash
yum install -y openssh-server \
yum install -y openssh-clients

Leave a Reply

Your email address will not be published. Required fields are marked *