搜索鏡像
[root@xiaoyequ ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13301 [OK]
拉取鏡像
[root@xiaoyequ ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
afb6ec6fdc1c: Pull complete
dd3ac8106a0b: Pull complete
8de28bdda69b: Pull complete
a2c431ac2669: Pull complete
e070d03fd1b5: Pull complete
Digest: sha256:c870bf53de0357813af37b9500cb1c2ff9fb4c00120d5fe1d75c21591293c34d
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
啓動容器
-d 後臺運行
--name 給容器命名
-p 宿主機端口:容器內部端口
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4392e5dad77d 5 days ago 132MB
centos latest 470671670cac 4 months ago 237MB
[root@xiaoyequ ~]# docker run -d --name mynginx -p 3344:80 nginx
41b957ae07b4490df8a7146ff969c80d517df5bb1d26d7459cfcb41efdded3df
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41b957ae07b4 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:3344->80/tcp mynginx
測試訪問
內部訪問
[root@xiaoyequ ~]# 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>
外網訪問
進入容器
[root@xiaoyequ ~]# docker exec -it mynginx /bin/bash
root@41b957ae07b4:/# whereis nginx # 尋找nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@41b957ae07b4:/# cd /etc/nginx
root@41b957ae07b4:/etc/nginx# cd /usr/share/nginx # nginx 的路徑
root@41b957ae07b4:/usr/share/nginx# ls
html
root@41b957ae07b4:/usr/share/nginx# cd html # 首頁的位置
root@41b957ae07b4:/usr/share/nginx/html# ls
50x.html index.html
root@41b957ae07b4:/usr/share/nginx/html# cat index.html
<!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>