nginx 是一個web服務器,須要持久運行的服務器,並且咱們通常選擇在後臺運行.nginx
拉取nginx鏡像web
[root@xinsz10 ~]# docker pull hub.c.163.com/library/nginx:latestdocker
latest: Pulling from library/nginxshell
5de4b4d551f8: Pull complete bash
d4b36a5e9443: Pull complete 服務器
0af1f0713557: Pull complete tcp
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6ide
Status: Downloaded newer image for hub.c.163.com/library/nginx:latestui
2.查看下載下來的nginx鏡像3d
[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