第一步:新建一个docker工作目录,我这里是/var/www/docker/
新建Dockerfile\nginx.conf\php.ini\www.conf三个文件
我这里后面拉取阿里云本地的一个ubuntu镜像,方便后续我在机房的本地服务部署测试如果不需要的话后面这个拉取命令可以不执行
docker pull dragonwell-registry.cn-hangzhou.cr.aliyuncs.com/dragonwell/dragonwell:8-ubuntu
可以使用
docker tag dragonwell-registry.cn-hangzhou.cr.aliyuncs.com/dragonwell/dragonwell:8-ubuntu dragon_ubuntu
对下载的镜像进行重命名
第二步:将代码写入Dockerfile
自定义 Dockerfile 确保 Nginx 作为主进程运行 如果您需要基于自定义的基础镜像(例如 dragonwell-registry.cn-hangzhou.cr.aliyuncs.com/dragonwell/dragonwell:8-ubuntu),可以通过编写 Dockerfile 来确保 Nginx 作为主进程运行。 步骤: 1. 创建 Dockerfile 创建一个名为 Dockerfile 的文件,并添加以下内容:
使用指定的基础镜像
FROM dragonwell-registry.cn-hangzhou.cr.aliyuncs.com/dragonwell/dragonwell:8-ubuntu
更新包列表并安装 Nginx
RUN apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/*
暴露端口 80
EXPOSE 80
复制自定义的 Nginx 配置文件(如果有)
COPY my_nginx.conf /etc/nginx/nginx.conf
设置 Nginx 作为容器启动时的主进程
CMD ["nginx", "-g", "daemon off;"]
说明: • CMD ["nginx", "-g", "daemon off;"]:这行命令确保 Nginx 以前台模式运行,使其成为容器的 PID 1 进程。这对于 Docker 容器来说非常重要,因为 Docker 会监控 PID 1 进程的状态来决定容器的运行状态。
如果没有进行在容器内运行那么容器start以后会立即退出,所以在创建容器同时也要马上安装nginx
第三步:
在包含 Dockerfile 的目录下运行以下命令来构建镜像,注意,这里创建的是镜像不是容器:
docker build -t myc .
使用构建好的自定义镜像启动容器:
sudo docker run -d \
--name bendi \
-p 80:80 \
myc:latest