|
| 1 | +# How to build your own image |
| 2 | + |
| 3 | +You can build a customized image based on Fluentd's image. |
| 4 | +Customized image can include plugins and `fluent.conf` file. |
| 5 | + |
| 6 | +### 1. Create a working directory |
| 7 | + |
| 8 | +We will use this directory to build a Docker image. |
| 9 | +Type following commands on a terminal to prepare a minimal project first: |
| 10 | + |
| 11 | +```bash |
| 12 | +# Create project directory. |
| 13 | +mkdir custom-fluentd |
| 14 | +cd custom-fluentd |
| 15 | + |
| 16 | +# Download default fluent.conf and entrypoint.sh. This file will be copied to the new image. |
| 17 | +# VERSION is v1.7 like fluentd version and OS is alpine or debian. |
| 18 | +# Full example is https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/v1.10/debian/fluent.conf |
| 19 | + |
| 20 | +curl https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/VERSION/OS/fluent.conf > fluent.conf |
| 21 | + |
| 22 | +curl https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/VERSION/OS/entrypoint.sh > entrypoint.sh |
| 23 | +chmod +x entrypoint.sh |
| 24 | + |
| 25 | +# Create plugins directory. plugins scripts put here will be copied to the new image. |
| 26 | +mkdir plugins |
| 27 | + |
| 28 | +curl https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/Dockerfile.sample > Dockerfile |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Customize `fluent.conf` |
| 32 | + |
| 33 | +Documentation of `fluent.conf` is available at [docs.fluentd.org][3]. |
| 34 | + |
| 35 | +### 3. Customize Dockerfile to install plugins (optional) |
| 36 | + |
| 37 | +You can install [Fluentd plugins][4] using Dockerfile. |
| 38 | +Sample Dockerfile installs `fluent-plugin-elasticsearch`. |
| 39 | +To add plugins, edit `Dockerfile` as following: |
| 40 | + |
| 41 | +About deprecated old images, see [DEPRECATED](DEPRECATED.md). |
| 42 | + |
| 43 | +#### Alpine version |
| 44 | + |
| 45 | +```Dockerfile |
| 46 | +FROM fluent/fluentd:v1.17-1 |
| 47 | + |
| 48 | +# Use root account to use apk |
| 49 | +USER root |
| 50 | + |
| 51 | +# below RUN includes plugin as examples elasticsearch is not required |
| 52 | +# you may customize including plugins as you wish |
| 53 | +RUN apk add --no-cache --update --virtual .build-deps \ |
| 54 | + sudo build-base ruby-dev \ |
| 55 | + && sudo gem install fluent-plugin-elasticsearch \ |
| 56 | + && sudo gem sources --clear-all \ |
| 57 | + && apk del .build-deps \ |
| 58 | + && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem |
| 59 | + |
| 60 | +COPY fluent.conf /fluentd/etc/ |
| 61 | +COPY entrypoint.sh /bin/ |
| 62 | + |
| 63 | +USER fluent |
| 64 | +``` |
| 65 | + |
| 66 | +#### Debian version |
| 67 | + |
| 68 | +```Dockerfile |
| 69 | +FROM fluent/fluentd:v1.17-debian-1 |
| 70 | + |
| 71 | +# Use root account to use apt |
| 72 | +USER root |
| 73 | + |
| 74 | +# below RUN includes plugin as examples elasticsearch is not required |
| 75 | +# you may customize including plugins as you wish |
| 76 | +RUN buildDeps="sudo make gcc g++ libc-dev" \ |
| 77 | + && apt-get update \ |
| 78 | + && apt-get install -y --no-install-recommends $buildDeps \ |
| 79 | + && sudo gem install fluent-plugin-elasticsearch \ |
| 80 | + && sudo gem sources --clear-all \ |
| 81 | + && SUDO_FORCE_REMOVE=yes \ |
| 82 | + apt-get purge -y --auto-remove \ |
| 83 | + -o APT::AutoRemove::RecommendsImportant=false \ |
| 84 | + $buildDeps \ |
| 85 | + && rm -rf /var/lib/apt/lists/* \ |
| 86 | + && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem |
| 87 | + |
| 88 | +COPY fluent.conf /fluentd/etc/ |
| 89 | +COPY entrypoint.sh /bin/ |
| 90 | + |
| 91 | +USER fluent |
| 92 | +``` |
| 93 | + |
| 94 | +#### Note |
| 95 | + |
| 96 | +These example run `apk add`/`apt-get install` to be able to install |
| 97 | +Fluentd plugins which require native extensions (they are removed immediately |
| 98 | +after plugin installation). |
| 99 | +If you're sure that plugins don't include native extensions, you can omit it |
| 100 | +to make image build faster. |
| 101 | + |
| 102 | +### 4. Build image |
| 103 | + |
| 104 | +Use `docker build` command to build the image. |
| 105 | +This example names the image as `custom-fluentd:latest`: |
| 106 | + |
| 107 | +```bash |
| 108 | +docker build -t custom-fluentd:latest ./ |
| 109 | +``` |
| 110 | + |
| 111 | +### 5. Test it |
| 112 | + |
| 113 | +Once the image is built, it's ready to run. |
| 114 | +Following commands run Fluentd sharing `./log` directory with the host machine: |
| 115 | + |
| 116 | +```bash |
| 117 | +mkdir -p log |
| 118 | +docker run -it --rm --name custom-docker-fluent-logger -v $(pwd)/log:/fluentd/log custom-fluentd:latest |
| 119 | +``` |
| 120 | + |
| 121 | +Open another terminal and type following command to inspect IP address. |
| 122 | +Fluentd is running on this IP address: |
| 123 | + |
| 124 | +```bash |
| 125 | +docker inspect -f '{{.NetworkSettings.IPAddress}}' custom-docker-fluent-logger |
| 126 | +``` |
| 127 | + |
| 128 | +Let's try to use another docker container to send its logs to Fluentd. |
| 129 | + |
| 130 | +```bash |
| 131 | +docker run --log-driver=fluentd --log-opt tag="docker.{{.ID}}" --log-opt fluentd-address=FLUENTD.ADD.RE.SS:24224 python:alpine echo Hello |
| 132 | +# and force flush buffered logs |
| 133 | +docker kill -s USR1 custom-docker-fluent-logger |
| 134 | +``` |
| 135 | +(replace `FLUENTD.ADD.RE.SS` with actual IP address you inspected at |
| 136 | +the previous step) |
| 137 | + |
| 138 | +You will see some logs sent to Fluentd. |
0 commit comments