Docker 中使用Nginx网站的搭建

Docker 中使用Nginx网站的搭建

使用Nginx 来搭建完整的前置站点,实现后向的代理,这篇文章中简单介绍一个搭建的步骤,至于Nginx 的知识,Docker 的使用可以参考对应的文档。

前提条件

  • Ubuntu (20.04)
  • Docker (23.0.1 Community)
  • Nginx

安装步骤

  • Ubuntu
    • 云服务器,国内的国外的都行,花点钱买一台
  • Docker 安装步骤,参考链接,做个简单的翻译
    • 首先更新系统 sudo apt update
    • 安装一些依赖的软件 sudo apt install apt-transport-https ca-certificates curl software-properties-common
    • 将Docker 官网仓储添加到系统的GPGkey中 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    • 将Docker 仓储添加到APT源中echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    • 更新系统 sudo apt update
    • 再次确认一下,你是从docker仓储中安装而不是ubuntu 中安装apt-cache policy docker-ce
    • 安装Dockersudo apt install docker-ce
    • 检查一下Docker的守护进程是否运行sudo systemctl status docker
  • 介绍几个Docker 中常用的命令
Command Comments
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
  • Nginx 安装步骤 参考链接

    • 拉取docker 源 docker pull nginx
    • 启动nginx docker run --name docker-nginx -p 80:80 nginx
      • 这样我们就启动了一个默认配置下的nginx 服务器,监听端口在80.
  • 下面我们要启动两个不同的网站,他们在同一台服务器下,同一个端口,只是通过不同的域名访问。假设域名为DomainA.com, DomainB.com端口都是80.

    • 首先基于上一步,我们先从docker 中copy 出一个默认的配置文件
      docker cp docker-nginx:/etc/nginx/conf.d/default.conf ~/default.conf
    • 然后修改这个文件,先复制里面的server directive, 然后将里面的server 配置成DomainA.com, 第二个server 配置成DomainB.com
    • 修改里面的root, 加入改成/var/www/SiteA, /var/www/SiteB
    • 在我们的ubuntu 上也新建一个SiteA,SiteB 的目录放入html.
    • 我们通过磁盘挂载的方式来启动nginx,具体参考这个链接
    • 首先我们把上一步的nginx 删除 docker rm docker-nginx
    • 重新启动 docker run --name docker-nginx -p 80:80 -v /local/SiteA:/var/www/SiteA -v /local/default.conf:/etc/nginx/conf.d/default.conf -v /local/SiteB:/var/www/SiteB

    Happy Coding

posted @ 2023-02-19 17:24  kongshu  阅读(312)  评论(0编辑  收藏  举报