1.通过编写Dockerfile自己创建镜像
vagrant@ubuntu-xenial:/data/www$ sudo mkdir d1 && cd d1
vagrant@ubuntu-xenial:/data/www/d1$ sudo vi Dockerfile
写入
FROM alpine:latest #使用一个基础镜像Alipine
MAINTAINER youer #作者
CMD echo "hello docker" #运行一个命令
使用docker build 构建一个镜像,其中 -t (镜像的名字及标签,通常 name:tag 或者 name 格式), . 表示当前目录下的内容都送给docker daemon 产生一个镜像(image)
vagrant@ubuntu-xenial:/data/www/d1$ docker build -t hello_docker . #构建一个镜像(image) Sending build context to Docker daemon 2.048kB Step 1/3 : FROM alpine:latest latest: Pulling from library/alpine df20fa9351a1: Pull complete Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321 Status: Downloaded newer image for alpine:latest ---> a24bb4013296 Step 2/3 : MAINTAINER youer ---> Running in a3e2bba91734 Removing intermediate container a3e2bba91734 ---> 6e866d4ff44b Step 3/3 : CMD echo "hello docker" ---> Running in 57300c2509cd Removing intermediate container 57300c2509cd ---> e8798fbd35bc Successfully built e8798fbd35bc Successfully tagged hello_docker:latest vagrant@ubuntu-xenial:/data/www/d1$ docker images #查看创建的镜像(images) REPOSITORY TAG IMAGE ID CREATED SIZE hello_docker latest e8798fbd35bc About a minute ago 5.57MB #即为我们刚刚创建的hello_docker的镜像 intelligent_knuth latest f6bea6b5d8c6 About an hour ago 133MB ubuntu latest 4e2eef94cd6b 2 weeks ago 73.9MB nginx latest 4bb46517cac3 3 weeks ago 133MB alpine latest a24bb4013296 3 months ago 5.57MB vagrant@ubuntu-xenial:/data/www/d1$ docker run hello_docker # 运行这个镜像输出hello docker hello docker vagrant@ubuntu-xenial:/data/www/d1$
下面我们创建一个较复杂的镜像作为演示
vagrant@ubuntu-xenial:/data/www$ sudo mkdir d2 && cd d2 vagrant@ubuntu-xenial:/data/www/d2$ sudo vi Dockerfile
写入
FROM ubuntu #使用一个基础镜像ubuntu MAINTAINER nz #作者名称 RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list #加速使用国内镜像 RUN apt-get update #更新一下ubuntu程序库 RUN apt-get install -y nginx #安装一个nginx COPY index.html /var/www/html #修改nginx默认显示 ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"] #运行nginx不以守护进程的方式启动 EXPOSE 80 #暴漏一个端口
注意: nginx默认是以后台模式启动的,Docker未执行自定义的CMD之前,nginx的pid是1,执行到CMD之后,nginx就在后台运行,bash或sh脚本的pid变成了1。
所以一旦执行完自定义CMD,nginx容器也就退出了。为了保持nginx的容器不退出,应该关闭nginx后台运行vagrant@ubuntu-xenial:/data/www/d2$ sudo vi index.html
写入
dockerfile-nginx
执行构建镜像命令
vagrant@ubuntu-xenial:/data/www/d2$ docker build -t dockerfiel/nginx . #构建一个镜像(image) Sending build context to Docker daemon 3.072kB Step 1/8 : FROM ubuntu ---> 4e2eef94cd6b Step 2/8 : MAINTAINER youer ---> Running in 2d3d3aa2856a Removing intermediate container 2d3d3aa2856a ---> cdbb8ec79689 Step 3/8 : RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list ---> Running in 82c99efdfd80 Removing intermediate container 82c99efdfd80 ---> 6fc9b3f9f95e Step 4/8 : RUN apt-get update ---> Running in e2ca44c3e909 Setting up nginx (1.18.0-0ubuntu1) ... Processing triggers for libc-bin (2.31-0ubuntu9) ... Removing intermediate container 97070963bb2c ---> d7bd2e56614c Step 6/8 : COPY index.html /var/www/html ---> bd5d2ca18a19 Step 7/8 : ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"] ---> Running in 7de655d9034d Removing intermediate container 7de655d9034d ---> 83a294795bc7 Step 8/8 : EXPOSE 80 ---> Running in 5b4549e15f9b Removing intermediate container 5b4549e15f9b ---> ccc10b2d21b6 Successfully built ccc10b2d21b6 Successfully tagged dockerfiel/nginx:latest vagrant@ubuntu-xenial:/data/www/d2$ docker images #可以看到我们刚创建的dockerfiel/nginx镜像 REPOSITORY TAG IMAGE ID CREATED SIZE dockerfiel/nginx latest ccc10b2d21b6 About a minute ago 156MB hello_docker latest e8798fbd35bc 48 minutes ago 5.57MB intelligent_knuth latest f6bea6b5d8c6 2 hours ago 133MB ubuntu latest 4e2eef94cd6b 2 weeks ago 73.9MB nginx latest 4bb46517cac3 3 weeks ago 133MB alpine latest a24bb4013296 3 months ago 5.57MB
运行这个镜像
vagrant@ubuntu-xenial:/data/www/d2$ docker run -p 80:80 -d dockerfiel/nginx #运行这个镜像 44eac38c0fe284c519d64711c2d152391ac8849e67b832fda330c9c7ee1b90c2 vagrant@ubuntu-xenial:/data/www/d2$ docker ps #创建了一个容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44eac38c0fe2 dockerfiel/nginx "/usr/sbin/nginx -g …" 8 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp romantic_ishizaka vagrant@ubuntu-xenial:/data/www/d2$
我们在本机访问一下: http://192.168.33.12/
Comment here is closed