【Docker】004.1-Docker部署Nginx、MySQL、Redis
目录
为了更好找,这里将一个常用的主题独立放在一篇文章
Docker部署Nginx:
# 第一步:搜索镜像,建议去网站搜索https://hub.docker.com/
docker search nginx
# 控制台
[root@zibo /]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14630 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1995 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 811 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 164
linuxserver/nginx An Nginx container, brought to you by LinuxS… 142
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 119 [OK]
# 第二步:下载镜像
docker pull nginx
# 控制台
[root@zibo /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 第三步:运行
docker run -d --name nginx01 -p 3344:80 nginx
# 控制台
[root@zibo /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 weeks ago 13.3kB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
[root@zibo /]# docker run -d --name nginx01 -p 3344:80 nginx
# -d:后台运行
# --name : 容器名字
# -p 3344:80:端口 主机端口:容器端口 外网访问3344相当于访问容器的80
6501c7f2c10ecc1bf1218157486e2f8d75112c29c5dd69cb5f043312fb86ed2c
[root@zibo /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6501c7f2c10e nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:3344->80/tcp nginx01
# 发起请求测试
[root@zibo /]# curl localhost:3344
<!DOCTYPE html> # 成功访问!
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
浏览器访问:
别忘了在服务器安全组规则内放行3344端口
Docker部署Nginx含挂载
# 注意目录(根据需要修改本机挂载目录)
# 先运行一个demo容器,复制里面的内容
docker run -d --name ng nginx
# 复制目录
docker cp ng:/etc/nginx/nginx.conf /home/docker/nginx/conf/
docker cp ng:/etc/nginx/conf.d/ /home/docker/nginx/conf/
docker cp ng:/var/log/nginx/ /home/docker/nginx/log/
docker cp ng:/usr/share/nginx/html/ /home/docker/nginx/html/
# 直接删除旧容器
docker rm -f ng
# 启动新容器并挂载外部配置和数据
docker run -d \
--name zb-nginx \
--restart always \
-p 80:80 -p 443:443 \
-v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/docker/nginx/log:/var/log/nginx \
-v /home/docker/nginx/html:/usr/share/nginx/html \
nginx
# 本机验证
curl localhost
一键启动并挂载MySQL
# 本机挂载的目录可根据需要调整,且不需要预先创建
docker run -d -p 3306:3306 -v /home/docker/mysql/conf:/etc/mysql/conf.d -v /home/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=密码 --name zb-mysql mysql
部署redis
第一步:拉取镜像
docker pull redis
第二步:直接启动
docker run -p 6379:6379 -d --name zb-redis redis redis-server
第三步:本地连接redis
docker exec -it zb-redis redis-cli
# 使得中文正常显示
docker exec -it zb-redis redis-cli --raw