Deploy Docker Using an Ansible Playbook and Systemd Service
This guide explains how to batch‑install Docker on Linux servers by creating a deployment directory, writing an Ansible playbook that unpacks the Docker binary, copies the service file, configures systemd, and verifies the installation with Docker commands.
This article describes a step‑by‑step process for deploying Docker on multiple Linux hosts using Ansible and systemd.
1. Preparation – Create a working directory on the control node:
[root@k8s-master2 ~]# mkdir docker-deploy
[root@k8s-master2 ~]# cd docker-deploy/
[root@k8s-master2 docker-deploy]# ls
[root@k8s-master2 docker-deploy]# mkdir files
[root@k8s-master2 docker-deploy]# cd files/Place the Docker binary archive ( docker-18.09.6.tgz) and the docker.service file inside the files directory.
2. Ansible Playbook – Create deploy.yaml with the following content:
---
- hosts: webservers
vars:
remoter_user: root
gather_facts: false
tasks:
- name: 分发解压包
unarchive: src=/files/docker-18.09.6.tgz dest=/tmp
- name: 移动二进制文件
shell: mv /tmp/docker/* /usr/bin
- name: 分发 service 文件
copy: src=files/docker.service dest=/usr/lib/systemd/system
- name: 启动设置开机启动
systemd: name=docker state=restarted enabled=yes
- name: docker 启动状态
shell: docker info
register: docker
- debug: var=docker.stdout_linesThis playbook unpacks the Docker archive, moves binaries to /usr/bin, copies the systemd service file, enables and restarts the Docker service, and finally checks the Docker status.
3. Systemd Service File – The docker.service file should contain:
[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
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target4. Execution and Verification – Run the playbook from the control node:
[root@k8s-master2 docker-deploy]# ansible-playbook deploy.yamlAfter the playbook completes, verify Docker installation on each node:
[root@k8s-node2 ~]# docker --version
Docker version 18.09.6, build 481bc77The article also provides links to related Ansible tutorials for conditional statements, loops, and batch configuration distribution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
