docker-compose使用自定義配置文件啓動redis

首先linux機上安裝了docker後需要再安裝docker compose,安裝比較簡單,直接拷貝官網教程

For alpine, the following dependency packages are needed: py-pip, python-dev, libffi-dev, openssl-dev, gcc, libc-dev, and make.

sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

Note: If the command docker-compose fails after installation, check your path. You can also create a symbolic link to /usr/bin or any other directory in your path.

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Test the installation:

docker-compose --version

首先創建一個目錄用來放配置文件:/usr/local/redis-compose

新建一個redis.conf配置文件,這裏端口使用16379

daemonize no
bind 0.0.0.0
appendonly no
port 16379

創建docker-compose.yml文件

version: '3.5'
services:
  redis:
    image: "redis:latest"
    container_name: my_redis
    command: redis-server /etc/redis/redis.conf
    ports:
      - "16379:16379"
    volumes:
      - ./data:/data
      - ./redis.conf:/etc/redis/redis.conf

參數說明:

version:docker-compose版本,這裏使用3.5版本

services:一個service就是一個容器實例

image:表示使用哪個鏡像,兒裏使用遠程image

container_name:設定實例名,不指定的話會根據當前路徑和文件配置生成一個名字,如這裏不指定名稱,會是"redis_compose_redis_1"

command:指定運行的命令,這裏會使用本地文件redis.conf掛載到容器的/etc/redis/路徑下然後用該文件啓動

volumes:這裏掛載/data路徑和redis.conf文件

 

啓動容器:

docker-compose up

控制檯輸出:

Creating network "redis-compose_default" with the default driver
Creating my_redis ... done
Attaching to my_redis
my_redis | 1:C 22 Jun 2020 09:05:35.741 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
my_redis | 1:C 22 Jun 2020 09:05:35.741 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
my_redis | 1:C 22 Jun 2020 09:05:35.741 # Configuration loaded
my_redis | 1:M 22 Jun 2020 09:05:35.742 * Running mode=standalone, port=16379.
my_redis | 1:M 22 Jun 2020 09:05:35.743 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
my_redis | 1:M 22 Jun 2020 09:05:35.743 # Server initialized
my_redis | 1:M 22 Jun 2020 09:05:35.743 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
my_redis | 1:M 22 Jun 2020 09:05:35.743 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
my_redis | 1:M 22 Jun 2020 09:05:35.743 * Ready to accept connections

啓動成功

也可以後臺運行

docker-compose up -d

 使用docker-compose logs -f查看日誌

使用docker ps命令可以看到該實例

通過redis-cli工具連接該實例:

 

驗證成功 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章