nginx在docker上安装配置

参考:

首先确保 my-nginx-network 网络提前创建好
docker network create -d bridge mynginx_network

准备nginx docker项目

创建项目并拷贝文件

mkdir mynginx
cd mynginx 
docker run --rm -d  --name temp-nginx -p 80:80 nginx
docker container ls --all
# 拷贝文件
docker cp f7968bc4ff4f:/usr/share/nginx ./www
docker cp f7968bc4ff4f:/etc/nginx/conf.d ./conf/conf.d
docker cp f7968bc4ff4f:/etc/nginx/nginx.conf ./conf/nginx.conf
docker cp f7968bc4ff4f:/var/log/nginx ./log

创建README.md,添加.dockerignore文件

*.dockerapp
*.tar.gz
_build/
bin
README.md

运行

docker run --rm -d -p 80:80 \
    --name mynginx \
    --network mynginx_network \
    -v $PWD/www:/usr/share/nginx \
    -v $PWD/conf/conf.d:/etc/nginx/conf.d \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    nginx
    

使用Dockerfile

创建Dockerfile

FROM nginx:latest
# RUN echo '<h1>Hello, This is Mynginx in Docker!</h1>' > /usr/share/nginx/html/index.html

COPY ./conf/nginx.conf /etc/nginx/nginx.conf
COPY ./conf/conf.d /etc/nginx/conf.d
COPY ./log /var/log/nginx
# COPY ./nginx /usr/share/nginx
# WORKDIR /root
EXPOSE 80

构建镜像
docker build -t mynginx:v1.0 .

运行

docker run --rm -d -p 80:80 \
    --name mynginx \
    --network mynginx_network \
    -v $PWD/www:/usr/share/nginx \
    -v $PWD/conf/conf.d:/etc/nginx/conf.d \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/log:/var/log/nginx \
    mynginx:v1.0

使用docker-compose

创建docker-compose.yml

version: '3.5'
services:
  nginx:
    build: .
    # command: nginx -g 'daemon off;'
    # image: nginx:lastest
    image: mynginx:v1.0  # 指定(生成的)镜像名
    container_name: mynginx # 指定(生成的)容器名
    volumes:
      - ./log:/var/log/nginx
      - ./conf.d:/etc/nginx/conf.d
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./www:/usr/share/nginx
    ports:
      - 80:80
    networks:
      - nginx_network

# 引用已存在的网络  
networks:
  nginx_network:
    external:
        name: mynginx_network

# mynginx_network 在命令行里提前建好
# docker network create -d bridge mynginx_network

# # 创建新的网络
# networks:
#   nginx_network:
#     name: mynginx_network
#     driver: bridge

docker-compose service的启动、运行及删除

docker-compose up -d //创建并后台启动
docker-compose stop	//停止
docker-compose start	//启动
docker-compose ps	//查看
docker-compose down --rmi all --remove-orphans  //删除服务、并删除生成的镜像和容器

应用配置

ghost

docker run --rm --name my-ghost \
    --network mynginx_network \
    -e url=http://localhost \
    --expose 2368 \
    -p 3002:2368 ghost

jekyll

export JEKYLL_VERSION=3.8
docker run --rm --name my-jekyll \
    --network mynginx_network \
    --volume="$PWD:/srv/jekyll" \
    --volume="$PWD/vendor/bundle:/usr/local/bundle" \
    -p 4000:4000 \
    --expose 4000 \
    -it jekyll/jekyll:$JEKYLL_VERSION \
    jekyll serve --watch --drafts --host my-jekyll

docker run --rm --name my-jekyll \
    --network mynginx_network \
    --volume="$PWD:/srv/jekyll" \
    --volume="$PWD/vendor/bundle:/usr/local/bundle" \
    -p 4000:4000 \
    -it jekyll/jekyll:$JEKYLL_VERSION \
    jekyll serve --watch --drafts

nginx

docker run -p 80:80 \
    --name mynginx \
    --restart=always \
    --network mynginx_network \
    -v $PWD/conf/conf.d:/etc/nginx/conf.d \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/log:/var/log/nginx \
    -v $PWD/www:/usr/share/nginx \
    -v $PWD/../myjekyll/_site:/usr/share/nginx/blog \
    nginx

布署nginx

docker run --rm -p 80:80 \
    --name mynginx \
    --network mynginx_network \
    --hostname=wodedata.com \
    --add-host=app.wodedata.com:127.0.0.1 \
    -v $PWD/conf/conf.d:/etc/nginx/conf.d \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/log:/var/log/nginx \
    -v $PWD/www:/usr/share/nginx \
    -v $PWD/../jekyll/_site:/usr/share/nginx/jekyll \
    nginx

注:--hostname=HOSTNAME 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts

版权所有,转载请注明出处 luowei.github.io.