0%

Docker 基本命令2(hello world)

拉取nginx image

1
2
3
4
5
6
7
xiaomao@jp-vps:~$docker pull nginx

xiaomao@jp-vps:~$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 13 days ago 64.2MB
busybox latest 020584afccce 2 weeks ago 1.22MB
nginx latest 540a289bab6c 3 weeks ago 126MB

运行起来

1
2
3
4
5
6
7
xiaomao@jp-vps:~$docker run --rm --name test-nginx -d -p 8080:80 nginx
f639a32e0aa2ddf85aff57364134758bd1d0055db40fbf64006d1db4def3bde2
xiaomao@jp-vps:~$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f639a32e0aa2 nginx "nginx -g 'daemon of…" 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp test-nginx
xiaomao@jp-vps:~$docker port test-nginx
80/tcp -> 0.0.0.0:8080

–rm 容器退出时自动删除
–name test-nginx 容器命名为test-nginx
-d 后台运行并打印容器id
-p 8080:80 把主机的8080端口映射到容器的80端口

docker port xxx 罗列容器的端口映射关系

测试

1
2
3
4
5
6
7
8
9
10
xiaomao@jp-vps:~$curl -I http://127.0.0.1:8080
HTTP/1.1 200 OK
Server: nginx/1.17.5
Date: Thu, 14 Nov 2019 13:54:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Oct 2019 14:30:00 GMT
Connection: keep-alive
ETag: "5daf1268-264"
Accept-Ranges: bytes

通过name停止/运行容器

1
2
3
4
xiaomao@jp-vps:~$docker stop test-nginx
test-nginx
xiaomao@jp-vps:~$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

定制内容

1
2
3
4
5
6
7
8
xiaomao@jp-vps:~$cat test/index.html
hello mm

xiaomao@jp-vps:~$docker run --rm -d --name test-nginx -p 8080:80 -v ~/test/:/usr/share/nginx/html:ro nginx
3e81756bf521f409b4cf489e01ea82d011a4c80186cd4e15b2d92a4f4161fabb

xiaomao@jp-vps:~$curl http://127.0.0.1:8080
hello mm

-v /test/:/usr/share/nginx/html:ro 将主机/test目录挂载到容器的/usr/share/nginx/html目录

查看容器的目录挂载情况

1
2
3
4
5
6
7
8
9
10
11
12
xiaomao@jp-vps:~$docker inspect test-nginx | grep Mounts -A 10
"Mounts": [
{
"Type": "bind",
"Source": "/home/xiaomao/test",
"Destination": "/usr/share/nginx/html",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
}
],
"Config": {

进入容器

1
2
3
xiaomao@jp-vps:~$docker exec -it test-nginx bash
root@3e81756bf521:/# hostname
3e81756bf521