---
# System prepended metadata

title: 建立 Docker CentOS Named Service 筆記
tags: [Docker]

---

> [TOC]
# 建立 Docker CentOS Named Service 筆記
###### tags: `Docker`

## 建立 Docker File
```
FROM centos:7

LABEL maintainer="Avery Sue"

ENV TZ Asia/Taipei

RUN yum install -y wget bind bind-chroot net-tools bash-completion vim openssl bind-utils openssh-clients supervisor\
    && yum install -y epel-release\
    && yum install -y supervisor telnet \
    && sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config\
    && ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone

COPY named.conf /var/named/chroot/etc/named.conf
COPY dcity.com.tw /var/named/chroot/etc/named/dcity.com.tw
COPY named.rfc1912.zones /var/named/chroot/etc/named.rfc1912.zones
COPY rndc.key /var/named/chroot/etc/rndc.key
COPY named.root.key /var/named/chroot/etc/named.root.key
COPY bind.ini /etc/supervisord.d/bind.ini
COPY start-container.sh /usr/local/bin/start-container.sh

RUN mkdir /var/named/chroot/var/log/named\
    && chown -R named.named /var/named/chroot/var/log/named\
    && chown -R named.named /var/named/chroot/etc\
    && cp -r /var/named/named.ca /var/named/chroot/var/named\
    && chown -R root.named /var/named/chroot/var/named/named.ca\
    && mkdir /var/named/chroot/var/named/dynamic\
    && chown -R named.named /var/named/chroot/var/named/dynamic\
    && cp -r /var/named/named.localhost /var/named/chroot/var/named\
    && chown -R root.named /var/named/chroot/var/named/named.localhost\
    && cp -r /var/named/named.loopback /var/named/chroot/var/named\
    && chown -R root.named /var/named/chroot/var/named/named.empty\
    && chmod +x /usr/local/bin/start-container.sh
    
ENTRYPOINT ["start-container.sh"]

EXPOSE 53/udp 53/tcp
```
## Build Docker images
-t : tag <name>
--no-cache : Do not use cache when building the image
```
docker build -t mybind . --no-cache
```
## 用 docker images 查看建立好的 images 
```
[root@backup named]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
mybind                   latest              ca3d14def0a2        5 seconds ago       516 MB
docker.io/ubuntu         latest              27941809078c        8 weeks ago         77.8 MB
docker.io/ubuntu/bind9   latest              a9dbaa4abc4f        3 months ago        134 MB
docker.io/php            7.4-apache          daa6a70e590e        3 months ago        452 MB
docker.io/centos         7                   eeb6ee3f44bd        10 months ago       204 MB
```

## 執行剛才建立好的 Images
-d : run in background
-p <local port>:<container port> - 把主機的53通訊埠所有流量轉發到mybind這個Container的udp 53通訊埠。
-v <local Directory>:<container Directory> - Bind mount a volume
```
docker run -d --name bind -p 53:53/udp -v /home/docker/named/log:/var/log/supervisor  mybind
```

## docker ps 查看建立好的容器
-a : 查看全部
```
[root@backup named]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                        NAMES
7a08aeb668a9        mybind              "start-container.sh"   8 minutes ago       Up 8 minutes        53/udp, 0.0.0.0:53->53/tcp   bind
```
    
## start-container.sh
```
#!/usr/bin/env bash
set -e
set -u
SUPERVISOR_PARAMS='-c /etc/supervisord.conf'

chown -R named.named /var/named/chroot/var/log/named
/usr/bin/supervisord $SUPERVISOR_PARAMS
export PS1='[\u@\h : \w]\$ '
/bin/bash
```
    
## 停止容器
```
[root@backup named]# docker stop bind1
bind1
```
    
## 啟動容器
```
[root@backup named]# docker start bind1
bind1
[root@backup named]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                        NAMES
13c2b273f700        mybind1             "start-container.sh"   13 minutes ago      Up 4 seconds        53/tcp, 0.0.0.0:53->53/udp   bind1
```
    
## 刪除容器
```
[root@backup named]# docker rm bind1
bind1
```
    
## 刪除 Images
```
[root@backup named]# docker image rm mybind
Untagged: mybind:latest
Deleted: sha256:ca3d14def0a29e420535bad281002fb569ce470cd85c33ec7c8598bbc17a9ee3
Deleted: sha256:917276c6c0e28f73941f4cf790d93d1619f1a31ecf239cb7df9fb07164fe2c62
Deleted: sha256:3228bf1833576815362ac5ccb9525deacae4bfca021c03934d59c5e7dba46d34
Deleted: sha256:c58c3bc5ff1b35f4f61b2d99a3de4a1d6b730f7878f3c20a842429cd37aa4b07
Deleted: sha256:0022a34d4ab9db9bbfb7e66d7dbf7c868d799c4aad3246d5d450e42779b75466
Deleted: sha256:5d68d17934760d0fdbc0f331c2a3765a0827cef3d2354b380548ca68ed4b84f4
Deleted: sha256:74eb1d28b894515e01d71c0e42525541c8be1001a6e8690d606ddcb8baa02e78
Deleted: sha256:f1970ff82b9b768f53d8b1a86f85ce3aa8d52fe4add37914fda695b57d60c802
Deleted: sha256:ff1efec15abf5dcda5712b6ffbaeea6e9ead6b37fa31c71664b3358a2f70fcf0
Deleted: sha256:4a005f95ff184f783e10a42fb9bea5ac5938023304c04f616fc1c49694aa4f94
Deleted: sha256:26e52a510f83260ab78e3dbe23feef8ae4ddeb3d6a2dd113ce9dfee61aa1ecef
Deleted: sha256:65e8524a5df4d89b53730f92596b0bd9df26b1166ed1950336c673a97b187625
Deleted: sha256:cbae9d3e056fa491575dfecceb647950caee14baec92c410892b45d0729b98b1
Deleted: sha256:5233a611fc76dc8f9c11d3cc0993a615b9cf50197c0884b54b96850ee71dbeea
Deleted: sha256:c1d2f628c7f606dbd577d166a786e8e5b8e766011b0b42c82fd3618ab8fce7ba
Deleted: sha256:80b538a1ffb41578f36c235b102a856943a4885ea749f1dd3bf063134b432010
Deleted: sha256:7174522ab1ccf7239e39ba091493d29f44cfa753e123964dacfd8784d36a4cbd
Deleted: sha256:ec86eee2d5e5d10348f95f7a4270fa2a1ee392f977b0efdeeb8de74fea93070a
Deleted: sha256:e28f1089a3421021f649379fc055e3d7c6af1a19aa7ccc8179dfd0ed193ce676
Deleted: sha256:e25c060b13e7850c481212d037d15722190014c66eb683fc9fd11706b5890ed9
Deleted: sha256:6943706f6614156f44e46fc403121b9984b77b8a6e7b7255478bca82b30aec91
```

## 進入容器
-i : Keep STDIN open even if not attached
-t : Allocate a pseudo-TTY
```
docker exec -it bind bash
```
    


