首先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工具连接该实例:

 

验证成功 

Logo

更多推荐