Docker 运行镜像 创建容器

***运行镜像***

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

猜你喜欢

转载自blog.csdn.net/test121210/article/details/106367754