用commit创建镜像
只做了解,一般都是用Dockerfile创建镜像
命令格式:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS说明:
- -a :提交的镜像作者;
- -c :使用Dockerfile指令来创建镜像;
- -m :提交时的说明文字;
- -p :在commit时,将容器暂停
运行命令:
我们运行一个ubuntu镜像,安装一个redis,然后exit退出,再commit生成一个带有redis的ubuntu镜像起仓库名为myrepo/redis,跟原先的ubuntu镜像对比发现大小增加了,之后在运行myrepo/redis这个镜像里面就会有安装过的redis
[root@izur9gz docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7698f282e524 8 days ago 69.9MB
[root@izur9gz docker]# docker run -i -t ubuntu /bin/bash
root@b030f46323ce:/# apt update
···
root@b030f46323ce:/# apt -y install redis
···
root@b030f46323ce:/# exit
exit
[root@izur9gz docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izur9gz docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b030f46323ce ubuntu "/bin/bash" 2 minutes ago Exited (0) 23 seconds ago recursing_shannon
[root@izur9gz docker]# docker commit b030f46323ce Charlie/redis
invalid reference format: repository name must be lowercase
[root@izur9gz docker]# docker commit b030f46323ce myrepo/redis
sha256:f2686eb560e51da9fe4a39048e14f548a4ad55791a794ffcc79a3bdd953d6604
[root@izur9gz docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myrepo/redis latest f2686eb560e5 25 seconds ago 99.2MB
ubuntu latest 7698f282e524 8 days ago 69.9MB
Dockerfile构建镜像
用例子来说明Dockerfile构建镜像的步骤,这个例子是在一个ubuntu中安装nginx,构建出一个新的镜像,用这个新的镜像启动容器,然后访问这个容器nginx欢迎页面。
创建文件夹,Dockerfile文件就放着这个文件夹中
mkdir /usr/local/docker
进入/usr/local/docker文件夹
cd /usr/local/docker
创建并编辑Dockerfile文件
vim Dockerfile
给Dockerfile文件中添加下方内容,使用ubuntu镜像,安装nginx,开放80端口
FROM ubuntu
RUN apt update
RUN apt -y install nginx
EXPOSE 80
用Dockerfile创建镜像,镜像名字是ubuntu/nginx,版本是v1,. 表示在当前文件夹
docker build -t ubuntu/nginx:v1 .
查看镜像,直接运行这个镜像就可以生成容器了
[root@Charlie docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu/nginx v1 019971f67182 3 minutes ago 155MB
运行镜像
[root@Charlie docker]# docker run -d -p 8787:80 ubuntu/nginx:v1 nginx -g "daemon off;"
9ef5f6f800e00db1ccc2aa57e9ea707d307e7a60d5af77ca5fbd21e8651e4622
访问8787端口页面,是nginx欢迎页,当然在浏览器上通过ip:端口方式更加直观
[root@Charlie docker]# curl localhost:8787
<!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>