1、搜索nginx镜像
$ docker search nginx
2、下载nginx镜像
$ docker pull nginx
# 控制台输出
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3、查看本地镜像
$ docker images
# 控制台输出
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 23 months ago 141MB
4、从nginx镜像启动一个容器
# -d, 设置容器在在后台一直运行
# --name, 设置容器名称
# -p 8001:80, 端口映射,将本地8080端口映射到容器内部的80端口
$ docker run -d --name nginx01 -p 8001:80 nginx
# 控制台输出:容器运行成功返回的container id
f4cb91f7571a58adff0a89143e58d5d4dfa3b5959d6660b200a6349a4da3810e
# 测试nginx服务是否启动成功
$ curl localhost:8001
5、查看容器列表
$ docker ps #or: docker container list
# 控制台输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4cb91f7571a nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8001->80/tcp nginx01
5、进入容器
# exec命令进入容器内部
$ docker exec -it f4cb91f7571a /bin/bash
# 控制台输出
root@f4cb91f7571a:/# which nginx #查看nginx目录
/usr/sbin/nginx
root@f4cb91f7571a:/# nginx -t #查看nginx配置文件
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@f4cb91f7571a:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
6、拷贝容器内的nginx配置文件到本机
lipengjie@MacBook-Pro ~ % docker cp f4cb91f7571a:/etc/nginx/nginx.conf ~/my-docker/nginx01/
lipengjie@MacBook-Pro ~ % cd ~/my-docker/nginx01/
lipengjie@MacBook-Pro conf % ls
nginx.conf
7、使用本机的nginx配置文件
创建~/my-docker/nginx02目录,并准备nginx.conf配置文件。
# -v,/宿主机绝对路径目录:/容器内目录
$ docker run -d --name nginx02 --restart=always -p 8002:80 -v ~/my-docker/nginx02/nginx.conf:/etc/nginx/nginx.conf nginx
# 控制台输出
39cc4d9b329f956e5402870d9bb9ebdb7baa2d68f17457714f44899379847f8d
# 查看所有运行中的容器
$ docker ps -a
# 控制台输出
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cc4d9b329f nginx "/docker-entrypoint.…" 26 seconds ago Up 25 seconds 0.0.0.0:8002->80/tcp nginx02
f4cb91f7571a nginx "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 0.0.0.0:8001->80/tcp nginx01
# 测试nginx服务是否启动成功
$ curl localhost:8002
8、挂载数据卷
$ docker run -d --name nginx01 -p 8001:80 -v ~/html/:/usr/share/nginx/html/ nginx