Docker新手练习

一、练习网址

无需安装Docker直接通过网址进行Docker的学习,访问以下网址直接点击以docker登录
https://labs.play-with-docker.com/

image.png

会有四小时的学习时间,过了四小时就要重新登录才行。

二、Docker镜像下载和运行

拉取最新的nginx

docker pull nginx:latest

拉取指定版本的nginx

$ docker pull nginx:1.13
1.13: Pulling from library/nginx
f2aa67a397c4: Pull complete 
3c091c23e29d: Pull complete 
4a99993b8636: Pull complete 
Digest: sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
Status: Downloaded newer image for nginx:1.13
docker.io/library/nginx:1.13

查看下载的镜像

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    4f380adfc10f   3 hours ago   133MB
mysql        latest    5c62e459e087   3 hours ago   556MB
nginx        1.13      ae513a47849c   3 years ago   109MB

运行Nginx镜像

docker run -d -p 81:80 nginx
-d 后台运行
-p 外部端口:内部端口

在网页上方会出来一个81端口,点击81进行访问


image.png

查看运行的容器

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
8243a621a060   nginx     "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    0.0.0.0:81->80/tcp   laughing_saha
1343c31176d2   nginx     "/docker-entrypoint.…"   24 minutes ago   Up 24 minutes   0.0.0.0:80->80/tcp   jovial_blackburn

进入运行的容器内部

$ docker exec -it 1343c31176d2 bash
root@1343c31176d2:/# cd /usr/share/nginx/html/
root@1343c31176d2:/usr/share/nginx/html# ls
50x.html  index.html

修改index
root@1343c31176d2:/usr/share/nginx/html# echo hlelo > index.html 
再次访问81端口会发现页面显示的就是 hello

只跟容器ID的部分也可以(可以区分开其他的容器)

$  docker exec -it 1343 bash
root@1343c31176d2:/#

删除运行的81端口Nginx容器

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
8243a621a060   nginx     "/docker-entrypoint.…"   9 minutes ago    Up 9 minutes    0.0.0.0:81->80/tcp   laughing_saha
1343c31176d2   nginx     "/docker-entrypoint.…"   31 minutes ago   Up 31 minutes   0.0.0.0:80->80/tcp   jovial_blackburn
[node1] (local) [email protected] ~
$ docker rm -f 8243
8243
[node1] (local) [email protected] ~
$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
1343c31176d2   nginx     "/docker-entrypoint.…"   32 minutes ago   Up 32 minutes   0.0.0.0:80->80/tcp   jovial_blackburn
[node1] (local) [email protected] ~
$ 

三、Docker的commit

docker commit 运行的容器id 指定一个名字

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
eac5302a1da6   nginx     "/docker-entrypoint.…"   5 seconds ago   Up 3 seconds   0.0.0.0:80->80/tcp   cool_carson
[node1] (local) [email protected] ~
$ docker commit eac zcnginx
sha256:bd079e1317aee884542e51e1e443a006cdea7b81dce8c1381cd263145229461b

commit后会生个一个zcnginx的镜像

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
zcnginx      latest    bd079e1317ae   About a minute ago   133MB
nginx        latest    4f380adfc10f   4 hours ago          133MB
mysql        latest    5c62e459e087   4 hours ago          556MB
nginx        1.13      ae513a47849c   3 years ago          109MB

而且可以运行

$ docker run -d -p 90:80 zcnginx
464746b9322542488e6d6bf19cef7da26b6b304385dae6edc31a8bc72ad8a44b
[node1] (local) [email protected] ~
$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
464746b93225   zcnginx   "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   0.0.0.0:90->80/tcp   distracted_bardeen
eac5302a1da6   nginx     "/docker-entrypoint.…"   5 minutes ago   Up 5 minutes   0.0.0.0:80->80/tcp   cool_carson
[node1] (local) [email protected] 

四、DockerFile

vim dockerfile 和 vim index.html
内容如下

$ cat dockerfile 
FROM nginx
ADD ./ /usr/share/nginx/html
[node1] (local) [email protected] ~
$ cat index.html 
this custom dockerfile

构建dockerfile

$ docker build -t zcdf .
Sending build context to Docker daemon     47MB
Step 1/2 : FROM nginx
 ---> 4f380adfc10f
Step 2/2 : ADD ./ /usr/share/nginx/html
 ---> dde14dd5cb1b
Successfully built dde14dd5cb1b
Successfully tagged zcdf:latest

测试运行自己通过DockerFile构建成功的镜像

$ docker run -d -p 100:80 zcdf
30a93f367db70b4cdcd1ff8ec5fd374f5fc07d40a296d665f1f02515717ce19d
[node1] (local) [email protected] ~
$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES
30a93f367db7   zcdf      "/docker-entrypoint.…"   5 seconds ago    Up 4 seconds    0.0.0.0:100->80/tcp   eloquent_williamson
464746b93225   zcnginx   "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   0.0.0.0:90->80/tcp    distracted_bardeen
eac5302a1da6   nginx     "/docker-entrypoint.…"   15 minutes ago   Up 15 minutes   0.0.0.0:80->80/tcp    cool_carson

五、Docker save

docker save 镜像名 > 保存的文件名.tar

$ docker save zcdf > 1.tar
[node1] (local) [email protected] ~
$ ls
1.tar       dockerfile  index.html

删除zcdf镜像

删除以zcdf镜像运行的容器
[node1] (local) [email protected] ~
$ docker rm -f 30a
30a

查看现有镜像
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
zcdf         latest    dde14dd5cb1b   11 minutes ago   180MB
zcnginx      latest    bd079e1317ae   24 minutes ago   133MB
nginx        latest    4f380adfc10f   5 hours ago      133MB
mysql        latest    5c62e459e087   5 hours ago      556MB
nginx        1.13      ae513a47849c   3 years ago      109MB
[node1] (local) [email protected] ~
删除zcdf镜像
$ docker rmi zcdf
Untagged: zcdf:latest
Deleted: sha256:dde14dd5cb1b101d6ce3a0205a1cd9dc91c322e730e7f1e89cd2ed4dd7838e85
Deleted: sha256:0b3e4764eed78399d2c0ba22ef6fbbe88276965a81055d91a3c728d130e12cf7
[node1] (local) [email protected] ~
验证是否删除成功
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
zcnginx      latest    bd079e1317ae   24 minutes ago   133MB
nginx        latest    4f380adfc10f   5 hours ago      133MB
mysql        latest    5c62e459e087   5 hours ago      556MB
nginx        1.13      ae513a47849c   3 years ago      109MB

然后加载之前save的文件,会发现zcdf有恢复了

$ ls
1.tar       dockerfile  index.html
[node1] (local) [email protected] ~
$ docker load < 1.tar
17ec54357efc: Loading layer  47.01MB/47.01MB
Loaded image: zcdf:latest
[node1] (local) [email protected] ~
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
zcdf         latest    dde14dd5cb1b   16 minutes ago   180MB
zcnginx      latest    bd079e1317ae   28 minutes ago   133MB
nginx        latest    4f380adfc10f   5 hours ago      133MB
mysql        latest    5c62e459e087   5 hours ago      556MB
nginx        1.13      ae513a47849c   3 years ago      109MB

六、指定运行容器的名字和文件映射

--name:指定运行容器
-v:外部路径:内部路径,可以进行文件的映射,可以进行数据的保存,将数据保存在外部存储盘中

[node1] (local) [email protected] ~
$ docker run -d -p 92:80 --name zc_run_nginx -v `pwd`:/usr/share/nginx/html nginx:1.13
05acc1139c1b47572bd93d58c24b0a19f61f8bb57d00575dc401f4774be2d9f7
$ docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                NAMES
05acc1139c1b   nginx:1.13   "nginx -g 'daemon of…"   17 seconds ago   Up 16 seconds   0.0.0.0:92->80/tcp   zc_run_nginx
464746b93225   zcnginx      "/docker-entrypoint.…"   33 minutes ago   Up 33 minutes   0.0.0.0:90->80/tcp   distracted_bar
deen
eac5302a1da6   nginx        "/docker-entrypoint.…"   38 minutes ago   Up 37 minutes   0.0.0.0:80->80/tcp   cool_carson
[node1] (local) [email protected] ~

你可能感兴趣的:(Docker新手练习)