***運行鏡像***
docker run -d -p 88:80 --name mynginx -v `pwd`:/usr/share/nginx/html nginx:1.13
設置運行參數:
-d 後臺運行,不阻塞shell指令窗口;
-p 指定內外端口映射,如:先寫外部端88,後寫內部端口80,二者做映射
--name 指定名字,如果沒指定,系統會默認給一個
-v 映射文件:可以把裏面的文件映射到外面,這樣方便修改
MySQL data文件映射到外面,方便保存,以免丟失
鏡像,如:tomcat
版本,默認是最新,也可以指定版本
[root@bogon ~]# docker run -d -p 80:80 tomcat 指定80端口運行tomcat鏡像
cd083ab32666842632b03ec6bbdb2cd0ec9028d41897d3abddf0a67746ef762e
---- 運行的容器的id
[root@bogon ~]# docker run -d -p 80:80 tomcat
5473be19d6e8e14df5cb6173e985a02d4cb20a11fabea5cc555ab8b9a058ca11
docker: Error response from daemon: driver failed programming external connectivity on endpoint upbeat_ardinghelli (e56eacf21e85038ab8fbdcddbb0b9aaf4593e5524c1e5ab5b14efc8ef54deb5d): Bind for 0.0.0.0:80 failed: port is already allocated.
報錯原因:因爲端口80已經被佔用
[root@bogon ~]# docker run -d -p 81:80 tomcat 指定81端口運行tomcat鏡像
d9669b176dc5d01ef9e561df4c666e8823cfe9f30d494d69a880c64fec41d49c
查看正在運行的容器
[root@bogon ~]# ps -ef | grep docker
進入到容器裏操作
[root@bogon ~]# docker exec -it 6b bash
root@6bb27bc8e53e:/# cd /usr/share/nginx/html
root@6bb27bc8e53e:/usr/share/nginx/html# ls
50x.html index.html
root@6bb27bc8e53e:/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>
root@6bb27bc8e53e:/usr/share/nginx/html# echo hell0 > index.html
root@6bb27bc8e53e:/usr/share/nginx/html# cat index.html
hell0
root@6bb27bc8e53e:/usr/share/nginx/html# exit 退出容器
exit
強制刪除鏡像
[root@ bogon ~]# docker rm -f d9
d9
tar文件commit創建容器
[root@ bogon ~]# docker commit cd m1 提交生成鏡像【cd是鏡像id簡寫】
sha256:f1552ede833ac6892c562ded7426030e0fbdd075fbc1170327923e61e6d73371
[root@ bogon ~]# docker images
[root@localhost ~]# docker run -d -p90:80 m1 指定90端口運行鏡像m1
ccca22e70fa708f3309dc19b1dd7fd23aac32663c6261d1e0e98b2ba2287e1b7
dockerfile build容器
[root@localhost ~]# vi dockerfile 創建dockerfile文件
編輯文件,內容:
FROM nginx
ADD ./ /usr/share/nginx/html/
退出編輯
[root@localhost ~]# ls
anaconda-ks.cfg dockerfile
[root@localhost ~]# vi index.html 創建index.html,寫外部文件
[root@localhost ~]# docker build -t m2 . 創建鏡像m2 (.是當前目錄)
save tar文件
[root@localhost ~]# docker save m2 >1.tar 把m2鏡像保存爲1.tar文件
[root@localhost ~]# ls
tar anaconda-ks.cfg dockerfile index.html
[root@localhost ~]# docker ps
[root@localhost ~]# docker rm -f 86 應該刪除container ID
86
[root@localhost ~]# docker rmi m2
Untagged: m2:latest
Deleted: sha256:d176c473ec2cb5480917ec9b222bc5d14f7c976c96a8b214813b833684abd5e3
Deleted: sha256:06dbfe7912356c86b1cea9c1cbf06c50964a1d9430d68037b9da029e057ce6e4
[root@localhost ~]# docker images
load tar文件
[root@localhost ~]# docker load < 1.tar
99b70335cf8e: Loading layer [==================================================>] 17.41kB/17.41kB
Loaded image: m2:latest
[root@localhost ~]# docker iamges