linux服务器搭建docker、docker-compose

今天在服务器搭建docker和docker-compose,使用docker搭建一些服务。之前没想到使用离线方式这么简单,记录下安装过程。

一、docker压缩包下载
docker压缩包下载地址

选择对应服务器的和版本的压缩包下载下来,上传到服务器的自己用的目录。

我使用的是24.0.7版本的。

二、解压缩安装:

bash 复制代码
tar -zxvf docker-24.0.7.tgz

解压后生成docker文件夹

执行命令将相关文件复制到服务器/usr/bin/目录

bash 复制代码
cp docker/* /usr/bin/

执行命令验证版本

bash 复制代码
docker -v
Docker version 24.0.7, build afdd53b

三、配置docker系统服务

新建docker.service文件

bash 复制代码
touch /usr/lib/systemd/system/docker.service
vi /usr/lib/systemd/system/docker.service

将下面的内容放到docker.service文件中

bash 复制代码
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

wq保存退出。

配置docker自启动。

bash 复制代码
# 重新加载系统配置 使docker.service配置文件生效
systemctl daemon-reload

# 设置自启动
systemctl enable docker

# 启动docker
systemctl start docker

# 查看docker运行状态
systemctl status docker

如下图表示运行成功

四、安装docker-compose
docker-compose github下载地址

可以选择对应版本和系统类型,我使用的是2.32.0的linux-x86版本的。

把文件下载下来放到服务器目录上,修改名字为docker-compose。

把docker-compose 文件放到/usr/local/bin目录

bash 复制代码
mv docker-compose /usr/local/bin

给文件复制可运行权限

bash 复制代码
chmod +x /usr/local/bin/docker-compose

创建软连接

bash 复制代码
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

版本验证

bash 复制代码
docker-compose version
# 输出 docker-compose 版本信息
Docker Compose version v2.32.0

五、docker镜像存放位置更改

有的时候,由于docker镜像比较大。docker镜像默认存放地址可能目录容量太小,所以我们需要自定义目录存放docker镜像。

创建docker的配置文件:

bash 复制代码
touch /etc/docker/daemon.json
vi /etc/docker/daemon.json

把下面的内容放到配置文件中:

bash 复制代码
{
  "registry-mirrors": ["https://registry.docker-cn.com", "https://mirror.aliyuncs.com"],
  "data-root": "/data/docker"  # 指定新的存储路径
}

docker镜像拉取的地址也是在这里配置的。

配置好之后wq保存退出。

使用系统服务方式重启:

bash 复制代码
# 停止docker
systemctl stop docker

# 生效配置文件
systemctl daemon-reload

# 启动docker
systemctl start docker

到这里就可以开始开开心心的使用docker啦。

相关推荐
java_logo1 小时前
Apache Doris Docker 部署指南:实时分析数据库实战
数据库·docker·apache·doris·apache doris·轩辕镜像·docker部署doris
爱写代码的森8 小时前
鸿蒙三方库 | harmony-utils之ImageUtil图片保存到本地详解
服务器·华为·harmonyos·鸿蒙·huawei
HLC++12 小时前
Linux的进程间通信
android·linux·服务器
华清远见IT开放实验室13 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
白露与泡影13 小时前
Arthas 实战指南:从方法耗时定位到 JVM 变量热修改
服务器·jvm·c#
神奇霸王龙14 小时前
Claude Code屠榜:MiMo与Grok紧追Codex
服务器·网络·人工智能·gpt·ai·ai编程
Web极客码14 小时前
WordPress SEO优化:提升网站排名的13个关键步骤
服务器·seo·wordpress
生活爱好者!14 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器
java_logo15 小时前
Docker Compose 部署 ClickHouse:快速搭建高性能列式数据库
数据库·clickhouse·docker·列式数据库·轩辕镜像·高性能数据库·clickhouse部署教程
MC皮蛋侠客15 小时前
uv 系列(七):CI/CD、Docker 与私有索引——生产级交付
python·ci/cd·docker·uv