docker 官网:Install Docker Engine on Ubuntu
sudo apt update
y
)sudo apt install docker.io docker-compose
sudo usermod -aG docker ${USER}
sudo docker run hello-world
运行成功
#1.在宿主机创建挂在容器的目录映射,创建目录用于存储数据信息
cd /usr/local/develop
sudo mkdir docker-app
cd docker-app
sudo mkdir nginx
cd nginx
#2.搜索nginx镜像
sudo docker search nginx
#3.拉取nginx镜像
sudo docker pull nginx
#4.创建简单容器,设置端口映射 (本机:容器)
sudo docker run -id --name nginx -p 80:80 nginx
#5.执行复制命令(复制容器里的配置文件到本地目录)
sudo mkdir -p conf logs html
sudo docker cp nginx:/etc/nginx/nginx.conf /usr/local/develop/docker-app/nginx/conf/nginx.conf
sudo docker cp nginx:/var/log/nginx /usr/local/develop/docker-app/nginx/logs
sudo docker cp nginx:/usr/share/nginx/html /usr/local/develop/docker-app/nginx/html
#6.删除容器
sudo docker stop <容器名或者容器id>
sudo docker rm <容器名或者容器id>
#7.修改配置文件
sudo docker gedit conf/nginx.conf
#或者
sudo vim conf/nginx.conf
#7.创建最终容器并挂载
sudo docker run -id --name nginx -p 80:80 -v /usr/local/develop/docker-app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/develop/docker-app/nginx/logs:/var/log/nginx -v /usr/local/develop/docker-app/nginx/html:/usr/share/nginx/html nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
server {
listen 80; # 配置监听的端口
listen [::]:80;
server_name localhost; # 配置的域名
# location段配置信息
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
# 前端加载404问题
try_files $uri $uri/ /index.html;
}
location ^~/prod-api/{
proxy_pass http://xxx.xxx.xx.xx:xxxx/;
}
}
}