docker(6):获取和创建image

一、实现功能

获取image,或者实现自己制作Images。

二、获取image

1.通过dockerfile获取
(1)Dockerfile

more Dockerfile

FROM ubuntu:14.04
LABEL maintainer=”Fresh New”
RUN apt-get update && apt-get install -y redis-server
EXPOSE 6379
ENTRYPOINT ["/usr/bin/stress"]
CMD []

(2)通过当前Docker制作image

docker build -t xiaopeng163/redis:latest ./

2.直接通过Registry(中心)获取
网址:https://hub.docker.com/

Docker pull ubuntu:14.04

三、制作base image

1.去除sudo,运行docker不需要sudo

(1)添加组

sudo groupadd docker

(2)添加密码

sudo gpasswd -a vagrant docker

(3)docker version

[vagrant@localhost ~]$ docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/version: dial unix /var/run/docker.sock: connect: permission denied

(4)重启docker

sudo systemctl restart docker

(5)重新登录shell

exit
vagrant ssh

PS D:\Program Files\dockerimage\centos7> vagrant ssh
Last login: Wed Jun 12 22:48:44 2019 from 10.0.2.2
[vagrant@localhost ~]$ docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:02:43 2019
  OS/Arch:          linux/amd64
  Experimental:     false

成功!

2.创建目录

[vagrant@localhost ~]$ mkdir hello-world
[vagrant@localhost ~]$ cd hello-world/

3.创建c语言helloworld程序

[vagrant@localhost hello-world]$ vi hello.c

写入

#include<stdio.h>
int main()
{
printf("hello docker");
}

4.安装依赖

History | grep yum

sudo yum install -y git
sudo yum install -y vim
sudo yum install -y gcc
sudo yum install -y glibc
sudo yum install -y locate
sudo yum install -y glibc-static

5.编译成可执行文件hello

gcc -static hello.c -o hello

[vagrant@localhost hello-world]$ ls
hello  hello.c

测试执行文件

[vagrant@localhost hello-world]$ ./hello
hello docker

6.当前目录创建dockerfile

创建base image

FROM scratch   #创建Baseimage
ADD hello /           #将可执行文件hello添加到image的根目录下
CMD ["/hello"]        #执行根目录下的hello

7.制作docker image:spark/hello

#.表示当前目录
[vagrant@localhost hello-world]$ docker build -f dockerfile.df -t spark/hello-world .

Sending build context to Docker daemon  860.7kB
Step 1/3 : FROM scratch
 --->
Step 2/3 : ADD hello /
 ---> aeb23d00be47
Step 3/3 : CMD ["/hello"]
 ---> Running in a4bc6efdc635
Removing intermediate container a4bc6efdc635
 ---> d801c21d3e64
Successfully built d801c21d3e64
Successfully tagged spark/hello-world:latest

8.查看已有image

[vagrant@localhost hello-world]$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
spark/hello-world   latest              d801c21d3e64        6 minutes ago       857kB
hello-world         latest              fce289e99eb9        5 months ago        1.84kB

9.查看image分层

Docker history +id

docker history d801c21d3e64
[vagrant@localhost hello-world]$ docker run spark/hello-world
hello docker[vagrant@localhost hello-world]$ docker history d801c21d3e64
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d801c21d3e64        10 hours ago        /bin/sh -c #(nop)  CMD ["/hello"]               0B              
aeb23d00be47        10 hours ago        /bin/sh -c #(nop) ADD file:045c96be48d245ab8…   857kB

10.创建docker容器

[vagrant@localhost hello-world]$ docker run spark/hello-world
hello docker


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值