- 创建一个GO容器
//拉取镜像
docker pull alpine
//因为gin要连接mysql,必须知道mysql docker容器的IP,然后更改gin代码mysql连接的IP
docker inspect <容器名> | grep IP
//运行
docker run --name goserver -d -v /home/gin/web:/server -w /server/build/gin alpine ./main
- 创建一个nginx容器
docker pull nginx:alpine
接着准备个nginx.conf配置文件
mkdir ngx
vi ngx/nginx.conf
//写入
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
upstream gin {
//这个IP要用“docker inspect <容器名> | grep IP”查gin docker容器的IP
server 172.17.0.3:8080;
}
server{
listen 80;
location / {
proxy_pass http://gin;
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
运行
docker run -d --name ngx -v /home/gin/ngx/nginx.conf:/etc/nginx/nginx.conf -p 80:80 nginx:alpine
我这里踩了个坑,报错了
[gin@gin ~]$ sudo docker start 62123fb04c90
Error response from daemon: driver failed programming external connectivity on endpoint ngx (75290bd4d15ff6befb64ee857ddf311e84eb523e3db5c501e758f00674ed05e1): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.4:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1))
Error: failed to start containers: 62123fb04c90
解决办法
//开放80端口(或者直接关闭iptables都可以)
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
//重启docker
sudo systemctl daemon-reload //守护进程重启
sudo systemctl restart docker //重启docker服务
- 测试成功