docker

docker相关

# 1. docker 运行 ubuntu 20.04

docker run -it ubuntu:20.04 bash

# 2. 设置 docker 镜像源

# 2.1. 方法一

# 编辑docker配置文件
sudo vim /etc/docker/daemon.json

# 添加以下内容
{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://hub-mirror.c.163.com",
    "https://mirror.ccs.tencentyun.com"
  ]
}

# 重启docker
sudo systemctl restart docker

# 2.2. 方法二

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}
EOF
# 重启docker
sudo systemctl restart docker

# 2.3. 使用镜像安装 Nginx

  1. 拉取镜像
# 镜像地址
"https://docker.1ms.run",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"

docker pull docker.1ms.run/library/nginx
  1. 创建 nginx 静态资源目录
sudo mkdir -p /usr/share/nginx/html
  1. 创建 index.html
sudo nano /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>我的网站</title>
    <style>
        body{
            text-align:center;
            margin-top:100px;
            font-family:Arial;
        }
        h1{
            color:#2d8cf0;
        }
    </style>
</head>
<body>
    <h1>Hello Nginx!</h1>
    <p>Docker + Nginx 部署成功啦</p>
</body>
</html>

Ctrl+O 回车保存 Ctrl+X 退出

  1. 停止 nginx
docker stop nginx
docker rm nginx
  1. 创建 nginx 容器并运行
docker run -d --name nginx -p 8080:80 -v /usr/share/nginx/html:/usr/share/nginx/html  --restart=always nginx
  1. 浏览器访问 ,针对 Windows 的 WSL 虚拟主机 使用 http://localhost 或 http://127.0.0.1 也无法访问,使用命令 hostname -I 查看 WSL IP 地址,然后在浏览器中访问该 IP 地址
上次更新: