Docker的相关使用及实践

A.搭建Docker服务。

  1. 安装Docker的依赖库。

yum install -y yum-utils device-mapper-persistent-data lvm2

  1. 添加Docker CE的软件源信息。

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

  1. 安装Docker CE。

yum makecache fast &&
yum -y install docker-ce

  1. 启动Docker服务。

systemctl start docker

  1. 配置DockerHub镜像加速器。

tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF

  1. 重启Docker服务。

systemctl restart docker

B.准备应用代码和Dockerfile
在工作空间下创建一个基于Golang的HelloWorld代码文件和一个用来构建运行Hello代码所需环境镜像的Dockerfile文件。

  1. 创建工作空间。

mkdir -p /tmp/demo && cd /tmp/demo

  1. 在工作空间下创建HelloWorld代码文件,用来在容器环境中监听HTTP服务,输出HelloWorld字符串。

cat > /tmp/demo/main.go << EOF
package main

import (

    "fmt"
    "net/http"

)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello! World\n")
    })

    fmt.Println("start to serve...")
    http.ListenAndServe(":80", nil)

}
EOF

  1. 在工作空间下创建Dockerfile文件。

cat > /tmp/demo/Dockerfile << EOF
FROM golang:1.12-alpine

change current working dir

WORKDIR /go/src/app

copy main.go into /go/src/app

COPY . .

go build and install the app

RUN go install -v ./...

run the app by default

CMD ["app"]
EOF

C. 本地构建镜像并运行镜像

  1. 使用docker build命令构建镜像。

docker build . -t demo:v1

Sending build context to Docker daemon 3.072kB

Step 1/5 : FROM golang:1.12-alpine

1.12-alpine: Pulling from library/golang

c9b1b535fdd9: Pull complete

cbb0d8da1b30: Pull complete

d909eff28200: Pull complete

665fbbf998e4: Pull complete

4985b1919860: Pull complete

Digest: sha256:3f8e3ad3e7c128d29ac3004ac8314967c5ddbfa5bfa7caa59b0de493fc01686a

Status: Downloaded newer image for golang:1.12-alpine

---> 76bddfb5e55e

Step 2/5 : WORKDIR /go/src/app

---> Running in 267b6eaa6339

Removing intermediate container 267b6eaa6339

---> b945a18744ab

Step 3/5 : COPY . .

---> 38bf768aed84

Step 4/5 : RUN go install -v ./...

---> Running in ca02c0e66ee8

app

Removing intermediate container ca02c0e66ee8

---> 6105ed5cc6f7

Step 5/5 : CMD ["app"]

---> Running in 5bc835f7b9de

Removing intermediate container 5bc835f7b9de

---> dfeafa2824e6

Successfully built dfeafa2824e6

Successfully tagged demo:v1

  1. 使用docker run命令运行镜像。

docker run -d -p 8000:80 demo:v1

[root@iZuf670yt5j8prmz4bkmpgZ demo]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

e897cc02550c demo:v1 "app" 11 seconds ago Up 11 seconds 0.0.0.0:8000->80/tcp brave_wescoff

  1. 使用curl工具访问容器中的HelloWorld服务。

curl localhost:8000

D. 推送镜像

  1. 登录到阿里云Docker Registry。

[root@iZuf670yt5j8prmz4bkmpgZ ~]# docker login --username="XXX@XXX.com" registry.cn-shanghai.aliyuncs.com

Password:

WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

  1. 标记本地镜像,将其归入远程仓库。

[root@iZuf670yt5j8prmz4bkmpgZ ~]# docker image ls

REPOSITORY TAG IMAGE ID CREATED SIZE

demo v1 dfeafa2824e6 13 minutes ago 354MB

golang 1.12-alpine 76bddfb5e55e 2 years ago 346MB

docker tag 76bddfb5e55e registry.cn-shanghai.aliyuncs.com/space_test001/demo:v1

[root@iZuf670yt5j8prmz4bkmpgZ ~]# docker image ls

REPOSITORY TAG IMAGE ID CREATED SIZE

demo v1 dfeafa2824e6 14 minutes ago 354MB

golang 1.12-alpine 76bddfb5e55e 2 years ago 346MB

registry.cn-shanghai.aliyuncs.com/space_test001/demo v1 76bddfb5e55e 2 years ago 346MB

  1. 将本地镜像推送到远程仓库。

docker push registry.cn-shanghai.aliyuncs.com/space_test001/demo:v1

The push refers to repository [registry.cn-shanghai.aliyuncs.com/space_test001/demo]

7306dca01e79: Pushed

3957f7032fc4: Pushed

12c4e92b2d48: Pushed

45182158f5da: Pushed

5216338b40a7: Pushed

v1: digest: sha256:1782cafde43390b032f960c0fad3def745fac18994ced169003cb56e9a93c028 size: 1365

  1. 拉取指定版本的远程镜像。

docker pull registry.cn-shanghai.aliyuncs.com/space_test001/demo:v1

上一篇:yolov3检测报错Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED


下一篇:盒马联手13家地产商搞事情,要把新零售加速铺向全中国