使用Dockerfile创建自己的单一LNMP镜像

突然想到回顾docker创建自己常用镜像,做个记录。
环境为windows11、docker desktop,复制以下内容保存成对应文件

1.Dockerfile.ubuntu

# 指定基础镜像
FROM ubuntu
# 镜像维护者作者
MAINTAINER Netpc.com.cn-ken

# 环境变量
ENV MYPATH /usr/local
# 工作路径,容器创建后,终端默认进入的工作目录,或者理解为落脚点
WORKDIR $MYPATH

# 构成镜像时,执行的命令
# 从Dockerfile到images的过程中会执行的命令

# 备份原镜像软件源
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
# 更改镜像为阿里镜像源
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list

RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y net-tools
RUN apt-get install -y iputils-ping
RUN apt-get install -y php7.4 php7.4-fpm php7.4-gd php7.4-mbstring php7.4-pdo php7.4-curl php7.4-mysql php7.4-mysqli php7.4-json php7.4-mbstring php7.4-xml
RUN apt-get install -y nginx
RUN apt-get install -y mysql-server mysql-client
RUN usermod -d /var/lib/mysql/ mysql

# 暴露端口
EXPOSE 80
EXPOSE 443
EXPOSE 3306

# 将宿主机的文件复制到镜像中
ADD mysql.sh /mysql.sh
RUN chmod 777 /mysql.sh && /mysql.sh
RUN rm -rf /mysql.sh

# 将脚本复制到容器中
ADD a.php /var/www/html/
ADD start.sh /usr/local/bin/
ADD default /etc/nginx/sites-available/default

# 设置脚本可执行权限
RUN chmod +x /usr/local/bin/start.sh

# 在容器启动时执行脚本
CMD ["/usr/local/bin/start.sh"]

2.mysql.sh #启动mysql,修改默认账号信息

#!/bin/sh
service mysql start
mysql -uroot -e "use mysql;update user set Host='%' where User='root';flush privileges;ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';flush privileges;"
echo 'bind-address = 0.0.0.0' >> /etc/mysql/mysql.conf.d/mysqld.cnf

3.default #为了替换原有的启动php

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
	}

}

4.start.sh

#!/bin/sh
service php7.4-fpm start
service mysql start
nginx -g "daemon off;" #不这样可能容器运行后会自动关闭

5.生成镜像命令:docker build -f Dockerfile.ubuntu -t ‘ubuntu-netpc’ .

6.docker run 映射到对应/var/www/html目录,就能把php放进去运行,映射端口3306就能直接访问容器内的mysql了。

上图

创建自己的docker镜像lnmp 创建自己的docker镜像lnmp2 创建自己的docker镜像lnmp1

此条目发表在服务器分类目录,贴了, 标签。将固定链接加入收藏夹。

发表回复