1、操作系统
ubuntu22
2、安装Docker
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get updatecurl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
3、构建镜像(nginx+php)
Dockerfile 如下
FROM ubuntu
MAINTAINER itdd
ENV MYPATH /tmp/php
WORKDIR $MYPATH
RUN apt-get -y update#安装vim编辑器
RUN apt-get -y install vim
RUN apt-get -y install lrzsz
#安装ifconfig命令查看网络IP
RUN apt-get -y install net-tools# nginx 依赖
RUN apt-get install -y gcc
RUN apt-get install -y libpcre3 libpcre3-dev
RUN apt-get install -y zlib1g zlib1g-dev
RUN apt-get install -y openssl
RUN apt-get install -y libssl-dev# 安装nginx
RUN apt-get install -y make
RUN mkdir -p /usr/local/nginx
ADD nginx-1.18.0.tar.gz /usr/local/nginx
RUN cd /usr/local/nginx/nginx-1.18.0/ && ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
&& make \
&& make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/bin/# 安装php
# 不选时区 可以通过配置环境变量,来跳过这个步骤
ARG DEBIAN_FRONTEND=noninteractive
RUN echo "6" | apt-get install -y pkg-config
RUN apt-get -y install gcc make openssl curl libssl-dev \
libxml2-dev libzip-dev libcurl4-openssl-dev libpng-dev libjpeg-dev \
libwebp-dev libonig-dev libsqlite3-dev libsodium-dev libargon2-dev
RUN mkdir -p /tmp/php/
ADD php-7.4.30.tar.gz /tmp/php/
RUN cd /tmp/php/php-7.4.30/ && ./configure --prefix=/usr/local/php-7.4.30 \
--with-config-file-path=/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl --enable-fpm \
&& make \
&& make install \
&& cp php.ini-development /usr/local/php-7.4.30/etc/php.ini
ADD php-fpm.conf /usr/local/php-7.4.30/etc/php-fpm.conf
ADD www.conf /usr/local/php-7.4.30/etc/php-fpm.d/www.conf
ADD nginx.conf /etc/nginx/nginx.conf# 复制代码到环境
ADD index.php /usr/local/nginx/html/index.php
RUN groupadd www
RUN useradd -g www www# 启动文件打入镜像
ADD startServer.sh /tmp/php/startServer.sh
RUN chmod +x /tmp/php/startServer.sh
EXPOSE 80 443
ENTRYPOINT ["./startServer.sh"]
里面设计到的文件都在文章开头的打包文件里,自取免费下载!!!
注意:php-fpm.conf最下面有一行路径,改成自己的路径
具体可参考 Docker微服务-Dockerfile之Nginx+PHP_php dockerfile_IT东东歌的博客-CSDN博客
4、创建容器
docker build -t ubuntu-nginx-php:1.0 .
docker run -p 8001:80 -d ubuntu-nginx-php:1.0
如果创建容器失败的话,可以看下日志< docker logs 容器ID > 根据日志提示解决问题。
5、访问
成功应该是这样的。
----------------------------还在持续更新中----------------------------