dockerfile安装httpd+mariadb+sshd

123456
512
2025-06-18

docker加速器:https://docker.xuanyuan.me

最好还是用虚拟机做,虚拟机安装docker教程:

https://www.zjznb.cn/?p=793353cc-a9d0-484e-b9fe-5d1443175992

docker pull centos:7

docker ps查看有没有端口被占用(80,3306,22)

mkdir dockerfile

cd dockerfile

vi dockerfile

按i进入编辑模式

FROM centos:7
ENV container docker
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all
COPY init.sh /opt
RUN bash /opt/init.sh
EXPOSE 22 80 3306
VOLUME ["/sys/fs/cgroup"]
STOPSIGNAL SIGRTMIN+3
CMD ["/usr/sbin/init"]

按esc输入:wq保存退出

vi init.sh

按i进入编辑模式

#!/bin/bash

yum -y swap -- remove fakesystemd -- install systemd systemd-libs

cd /lib/systemd/system/sysinit.target.wants
for i in *; do
  [ "$i" == "systemd-tmpfiles-setup.service" ] || rm -f "$i"
done
rm -f /lib/systemd/system/multi-user.target.wants/*
rm -f /etc/systemd/system/*.wants/*
rm -f /lib/systemd/system/local-fs.target.wants/*
rm -f /lib/systemd/system/sockets.target.wants/*udev*
rm -f /lib/systemd/system/sockets.target.wants/*initctl*
rm -f /lib/systemd/system/basic.target.wants/*
rm -f /lib/systemd/system/anaconda.target.wants/*

yum -y install httpd mariadb-server openssh-server

systemctl enable httpd
systemctl enable mariadb
systemctl enable sshd

按esc输入:wq保存退出

docker build -t 任意名字 .

👆不要漏了最后的点.......

docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro --tmpfs /run --tmpfs /run/lock -p 2222:22 -p 80:80 -p 3306:3306 -it 任意名字

会直接卡住 我们再开一个终端

docker ps 查看容器id

docker exec -it 容器id /bin/bash

此时已经做完了,cha'kan

最后查看一下服务有没有运行

systemctl status httpd

systemctl status mariadb

systemctl status sshd

exit 退出 查看dockerfille文件

cat dockerfile

cat init.sh

后续想要登录 docker ps 查看容器id

docker exec -it 容器id /bin/bash

动物装饰