docker 官网:Install Docker Engine on Ubuntu
一、安装docker
1.将apt升级到最新
sudo apt update
2.使用apt安装 docker 和 docker-compose (遇到提示输入y
)
sudo apt install docker.io docker-compose
3.将当前用户添加到docker用户组
sudo usermod -aG docker ${USER}
4.运行hello-world
sudo docker run hello-world
运行成功
二、安装nginx
#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
三、nginx.conf 文件配置
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/;}}
}
四、配置多个项目、映射多个端口
1.修改配置文件
修改conf/nginx.conf
2.重新挂载
sudo docker run -id --name nginx -p 80:80 -p 81:81 -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