Docker学习(二)——实例
2019-12-27 本文已影响0人
Haleng
2.1 Docker 安装 Nginx
- 搜索并拉取官方的Nginx镜像,查看是否存在:
[root@localhost /]# docker search nginx
...
[root@localhost /]# docker pull nginx
...
[root@localhost /]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b 9 days ago 126MB
- 使用
NGINX
默认的配置来启动一个Nginx
容器实例,其中geekleng-nginx-test
是容器名称,-p
端口进行映射,将本地 8081 端口映射到容器内部的 80 端口,-d
表示容器后台一直运行,可在浏览器中输入http://192.168.74.128:8081/
进行访问,该ip
是通过ip addr
进行查询出来的:
[root@localhost /]# docker run --name geekleng-nginx-test -p 8081:80 -d nginx
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a6e07c6f1b1 nginx "nginx -g 'daemon of…" 51 minutes ago Up 51 minutes 0.0.0.0:8081->80/tcp geekleng-nginx-test
- 进行
nginx
部署,创建目录nginx
, 用于存放后面的相关东西,拷贝容器内Nginx
默认配置文件到本地当前目录下的conf
目录,容器ID
可以查看docker ps
命令输入中的第一列(注意其中的~
表示当前系统的家目录,使用root
账号登录的家目录是/root/
,使用非root
用户登录的家目录是/home/
目录);- www: 目录将映射为 nginx 容器配置的虚拟目录;
- logs: 目录将映射为 nginx 容器的日志目录,
- conf: 目录里的配置文件将映射为 nginx 容器的配置文件
[root@localhost /]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
[root@localhost /]# docker cp 0a6e07c6f1b1:/etc/nginx/nginx.conf ~/nginx/conf
- Nginx部署命令,执行下面的命令以及命令说明:
-
-p 8082:80
: 将容器的 80 端口映射到主机的 8082 端口; -
--name runoob-nginx-test-web
:将容器命名为 runoob-nginx-test-web; -
-v ~/nginx/www:/usr/share/nginx/html
:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html; -
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf; -
-v ~/nginx/logs:/var/log/nginx
:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
-
$ docker run -d -p 8082:80 --name geekleng-nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
- 启动以上命令后进入
~/nginx/www
目录,新建index.html
文件,内容如下,然后再浏览器中输入http://192.168.74.128:8082/
并打开,该ip
是通过ip addr
进行查询出来的:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>