nginx 是一个web服务器,需要持久运行的服务器,而且我们一般选择在后台运行.
拉取nginx镜像
[root@xinsz10 ~]# docker pull hub.c.163.com/library/nginx:latest
latest: Pulling from library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
2.查看下载下来的nginx镜像
[root@xinsz10 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 05a3bd381fc2 4 weeks ago 1.84kB
hub.c.163.com/library/nginx latest 46102226f2fd 5 months ago 109MB
3.运行nginx
[root@xinsz10 ~]# docker run hub.c.163.com/library/nginx
4.查看是否运行
重新打开一个窗口执行命令
root@xinsz10 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
039303f1129e hub.c.163.com/library/nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp stoic_almeida
可以看到nginx已经启动了.
这种方式是在前台运行,停止直接用ctrl+c就可以了
在后台运行nginx的方法
使用参数-d即可
[root@xinsz10 ~]# docker run -d hub.c.163.com/library/nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
d1c2d8e307e75170428a433dbdc97fe6f817cc01097c0b32bbd72cf69b96b6c5
重新打开一个窗口查看是否运行
[root@xinsz10 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1c2d8e307e7 hub.c.163.com/library/nginx "nginx -g 'daemon ..." 55 seconds ago Up 53 seconds 80/tcp xenodochial_snyder
可以看到标红的ID号是一样的
说明容器已经运行起来了.
我们要管理nginx,进入nginx内部就可以直接使用 docker exec
查看下docker exec的使用帮助
[root@xinsz10 ~]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a
container
-e, --env list Set environment variables
--help Print usage
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
我们比较常用的参数是 -i 和 -t
[root@xinsz10 ~]# docker exec -it d1 bash #注: d1是刚刚nginx的ID号,我取了前两位.
root@d1c2d8e307e7:/#
这样就进入了一个docker nginx终端,相当于一个shell终端
在这里就和shell一样了,Linux命令都有.
root@d1c2d8e307e7:/# pwd
/
root@d1c2d8e307e7:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@d1c2d8e307e7:/# which nginx
/usr/sbin/nginx
退出容器:
root@d1c2d8e307e7:/# exit
exit