有时候启动镜像不能在服务器上手动编写启动命令,拿redis举例,如果需要启动redis镜像并要加上密码等参数,就需要重新打包镜像来修改启动命令了。
一、创建一个 Dockerfile
的文件
二、创建一个名为 redis.conf
的配置文件,并根据需求进行自定义配置
三、在 Dockerfile 中添加以下内容:
FROM redis:latest
# 添加自定义的配置文件
COPY redis.conf /usr/local/etc/redis/redis.conf
# 修改启动命令,加上密码参数
CMD ["redis-server", "--requirepass", "yourpassword", "/usr/local/etc/redis/redis.conf"]
这里的 yourpassword
是你想要设置的 Redis 连接密码,可以根据需要自行更改。
四、 构建新的 Redis 镜像
docker build -t my-new-redis .
这里的 my-new-redis
是你给镜像取的名称,可以根据需要自行更改。
这样就可以把启动命令打包到镜像中去了,运行启动新镜像容器时,启动命令就变成了:
docker run --name my-new-redis-container -d my-new-redis
不需要加上密码参数直接启动即可。