docker构建镜像

2019-08-03  本文已影响0人  GUIN蚂蚁

这里假设你已经成功安装了docker,以下是贴子要做的事情:

1. 工程目录

.
├── docker
│   ├── Dockerfile
│   └── make_image.sh
└── main.go

工程目录简单一点,只是举个栗子

docker目录:docker相关文件
Dockerfile:帮助我们构建镜像的文件,里面会写一些执行命令
make_image.sh: 编译main.go文件,构建镜像,push镜像到docker仓库
main.go: 我们的app代码,最后运行在容器里面的程序

Dockerfile

FROM ubuntu

COPY . app/

CMD ["app/docker-test"]

第一行:制定基础镜像,即程序运行的环境
第二行:拷贝docker目录下的所有文件到镜像根目录的app目录下
第三行:容器启动成功后,执行的命令/程序

make_image.sh

#!/usr/bin/env bash

# enter root directory
cd ..

# init environment, to find command: go
source /etc/profile

# build app
go build -o docker-test

if [[ ! -f docker-test ]]; then
    exit 1
fi

chmod +x docker-test
mv docker-test docker/

cd docker

version="0.0.1"
my_registry="my.registry:5000/docker_test:"${version}

# build the image
docker build --tag=docket_test:${version} .

# tag for image
docker tag docket_test:${version} ${my_registry}

# push the image to docker repository
docker push ${my_registry}

echo ${my_registry}
rm docker-test

main.go

只打印一些简单的信息,要安装好go环境哈,没有的话写个shell脚本在上面跑也行

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("running on the docker")
    for  {
        time.Sleep(time.Second * 2)
        fmt.Println("sleep 2s on the docker")
    }
}

2. 配置/etc/host

$ cat /etc/hosts
127.0.0.1   localhost
## your docker repository address
127.0.0.1    my.registry

3. 在私有仓库的服务器上安装docker registry

$ docker pull registry
docker run -d -p 5000:5000 -v /option/docker-registry:/var/lib/registry registry

4. 配置/etc/docker/daemon.json

$ cat /etc/docker/daemon.json 

{
        "registry-mirrors": ["http://harbor.test.com"], 
        "insecure-registries": ["my.registry:5000"]
}

5. 构建并push镜像

好了,到最后一步了,在docker目录执行make_image.sh脚本

$ cd docker
$ sudo ./make_image.sh 

Sending build context to Docker daemon  2.006MB
Step 1/3 : FROM ubuntu
 ---> 3556258649b2
Step 2/3 : COPY . app/
 ---> Using cache
 ---> b0bce5a5a773
Step 3/3 : CMD ["app/docker-test"]
 ---> Using cache
 ---> 0f1bd3b8aea9
Successfully built 0f1bd3b8aea9
Successfully tagged docket_test:0.0.1
The push refers to repository [my.registry:5000/docker_test]
062151ee13df: Layer already exists 
b079b3fa8d1b: Layer already exists 
a31dbd3063d7: Layer already exists 
c56e09e1bd18: Layer already exists 
543791078bdb: Layer already exists 
0.0.1: digest: sha256:55987343bb1c2a102d4ceab288f2f39a446fc67c1c1ae92e64ee16cafd1b6ddc size: 1363
my.registry:5000/docker_test:0.0.1
本地运行一下镜像,看效果
$ sudo docker images

REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docket_test                    0.0.1               0f1bd3b8aea9        About an hour ago   66.2MB
my.registry:5000/docker_test   0.0.1               0f1bd3b8aea9        About an hour ago   66.2MB
ubuntu                         latest              3556258649b2        10 days ago         64.2MB
hello-world                    latest              fce289e99eb9        7 months ago        1.84kB

docket_test和my.registry:5000/docker_test, 都是我们生成的镜像,前者是本地的镜像,后者是push到docker仓库的镜像,但通过看IMAGE ID可以知道,它们是同一个东西。

$ sudo docker run docket_test:0.0.1

running on the docker
sleep 2s on the docker
sleep 2s on the docker
sleep 2s on the docker
sleep 2s on the docker
sleep 2s on the docker
sleep 2s on the docker

6. 查看docker私有仓库的镜像

$ curl -XGET http://my.registry:5000/v2/_catalog

{"repositories":["docker_test"]}
$ curl -XGET http://my.registry:5000/v2/docker_test/tags/list

{"name":"docker_test","tags":["0.0.1"]}
$ ls /option/docker-registry

该目录是启动docker registry时通过-v参数配置的

一键清除none镜像

$ sudo docker rmi -f `sudo docker images | grep "<none>" | awk '{print $3}'`
上一篇下一篇

猜你喜欢

热点阅读