Part 1: Dockerize
对于许多编程语言(包括 Go ),有几个很好的官方和社区支持的容器。我们在容器化Go apps的时候,可以选择基于 Golang 官方镜像构建,如:golang:onbuild,golang:latest。但是这有一个很大的缺点:这些容器可能很大,所以基于它们的镜像创建的镜像文件将会非常大。
这是因为我们的应用程序是在容器内编译的。这意味着该容器需要安装 Go ,以及 Go 的依赖关系,同时这也意味着我们需要一个程序包管理器和整个操作系统。实际上,如果您查看 Golang 的 Dockerfile,它将以 Debian Jessie 开头,安装 GCC 编译器和一些构建工具,压缩 Go 并安装它。因此,我们几乎有一个完整的 Debian 服务器和 Go 工具包来运行我们的小型应用程序。
镜像 scratch(空镜像), 大小 0B
镜像 busybox(空镜像 + busybox), 大小 1.4MB
镜像 alpine (空镜像 + busybox + apk), 大小 3.98MB
So what’s scratch? Scratch is a special docker image that’s empty. It’s truly 0B:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
scratch latest 511136ea3c5a 22 months ago 0 B
目前 Docker 官方已开始推荐使用 Alpine 替代之前的 Ubuntu 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。
FROM scratch is a completely empty filesystem. You have no installed libraries, and no shell (like /bin/sh) included in there. To use this as your base, you’d need a statically linked binary, or you’ll need to install all of the normal tools that are included with a linux distribution.
The latter is what is prepackaged in the various busybox, debian, ubuntu, centos, etc images on the docker hub. The fast way to make your image work with a minimal base image is to change the from to FROM busybox and change your /bin/bash to /bin/sh.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
passport-busybox 1.0.1 1028fbd88847 32 seconds ago 36.3MB
passport-scratch 1.0.1 aa407fee8d95 33 minutes ago 35.1MB
passport-multi-stage 1.0.9 dd8a070d96e9 2 days ago 59.4MB
Go应用本身的二进制文件为33MB
指令说明:
FROM
FROM指令初始化一个新的构建阶段,并为后续指令设置基本映像。因此,有效的 Dockerfile 必须以 FROM 指令开头。
格式:
FROM [AS ]
Or
FROM [:] [AS ]
Or
FROM [@] [AS ]
ARG 是 Dockerfile 中唯一可以出现在 FROM 指令之前的指令。
FROM可以在单个Dockerfile中多次出现,以创建多个映像或将一个构建阶段用作对另一个构建阶段的依赖。 只需在每个新的FROM指令之前记录一次提交输出的最后一个图像ID。 每个FROM指令清除由先前指令创建的任何状态。
通过将AS名称添加到FROM指令中,可以选择为新的构建阶段指定名称。 该名称可以在后续的FROM和COPY --from = <名称|索引>指令中使用,以引用在此阶段构建的映像。
tag or digest 值是可选的。 如果您忽略其中任何一个,那么缺省情况下构建器都会采用 latest 标签。 如果构建器找不到标签值,则返回错误。
MAINTAINER
MAINTAINER指令设置生成图像的“作者”字段。 LABEL指令是此指令的更为灵活的版本,您应该使用它,因为它可以设置所需的任何元数据,并且可以轻松查看,例如使用docker inspect。 要设置与MAINTAINER字段相对应的标签,可以使用:
COPY [–chown=:] ...
COPY [--chown=:] ["",... ""] (this form is required for paths containing whitespace)
每个都可以包含通配符,并且将使用Go的filepath.Match规则进行匹配。例如:
COPY hom* /mydir/ # adds all files starting with “hom”
COPY hom?.txt /mydir/ # ? is replaced with any single character, e.g., “home.txt”
是绝对路径,或相对于WORKDIR的路径,源将在目标容器内复制到该路径。
COPY test relativeDir/ # adds "test" to `WORKDIR`/relativeDir/
COPY test /absoluteDir/ # adds "test" to /absoluteDir/
复制包含特殊字符 (such as [ and ]), 的文件或目录时,需要遵循Golang规则转义那些路径,以防止将它们视为匹配模式。例如,要复制名为 arr[0].txt 的文件,请使用以下命令:
COPY arr[[]0].txt /mydir/ # copy a file named "arr[0].txt" to /mydir/
可选地,COPY接受 --from=<name|index> 标志,该标志可用于将源位置设置为先前的构建阶段 (created with FROM .. AS ) ,该阶段将用于代替由发送的构建上下文用户。如果找不到具有指定名称的构建阶段,则尝试改用具有相同名称的图像。
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd
该Dockerfile中最后一个pwd命令的输出为 /path/$DIRNAME
RUN
RUN指令将在当前映像顶部的新层中执行任何命令,并提交结果。生成的提交映像将用于Dockerfile中的下一步。
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN ["executable", "param1", "param2"] (exec form)
分层运行RUN指令并生成提交符合Docker的核心概念,在Docker上,提交很便宜,并且可以从映像历史记录的任何位置创建容器,就像源代码控制一样。
在shell形式中,可以使用(反斜杠)将一条RUN指令继续到下一行。
RUN /bin/bash -c 'source $HOME/.bashrc; \
echo $HOME'
Together they are equivalent to this single line:
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (在指定了 ENTRYPOINT 指令后,用 CMD 指定具体的参数。)
CMD command param1 param2 (shell form)
If you use the shell form of the CMD, then the will execute in /bin/sh -c:
FROM ubuntu
CMD echo "This is a test." | wc -
如果要在没有shell的情况下运行,则必须将命令表示为JSON数组,并提供可执行文件的完整路径。此数组形式是CMD的首选格式。任何其他参数必须在数组中分别表示为字符串:
FROM ubuntu
CMD ["/usr/bin/wc","--help"]
注意,指定了CMD命令以后,docker container run命令就不能附加命令了(比如 /bin/bash),否则它会覆盖CMD命令。
###以下是启动信息
2019-11-15T22:46:12.278+0800 DEBUG setting/etcd.go:35 setting.etcd: Backend config {"backend": "etcdv3", "machines": ["10.10.1.29:2379"], "keyspace": "", "traceId": ""}
2019-11-15T22:46:12.290+0800 DEBUG setting/etcd.go:65 setting.etcd: Retrieved mysql-key-val from etcd store {"key": "root/config/common/database/mysql/passport", "config": {"master":{"dsn":"","user":"root","pass":"GL@c*Nm#dkaLH!FNe","host":"rm-j6cli54dhwo5ski2quo.mysql.rds.aliyuncs.com","port":3306,"dbname":"peduli","max_open":100,"max_idle":10},"slave":{"dsn":"","user":"root","pass":"GL@c*Nm#dkaLH!FNe","host":"rm-j6cli54dhwo5ski2quo.mysql.rds.aliyuncs.com","port":3306,"dbname":"peduli","max_open":100,"max_idle":10}}, "traceId": ""}
DEBU[0001] new passport mysql store MasterDB="&{ 0 0xc42022ac80 false 2 {0xc42034a280} map[] 0xc4200e05a0 0x16c7aa0 0xc4201e39a0 false}" SlaveDB="&{ 0 0xc42022ae60 false 2 {0xc42034a280} map[] 0xc4200e06c0 0x16c7aa0 0xc4201e3b60 false}"
2019-11-15T22:46:13.888+0800 DEBUG setting/etcd.go:35 setting.etcd: Backend config {"backend": "etcdv3", "machines": ["10.10.1.29:2379"], "keyspace": "", "traceId": ""}
2019-11-15T22:46:13.894+0800 DEBUG setting/etcd.go:102 setting.etcd: Retrieved redis-key-val from etcd store {"key": "root/config/common/database/redis", "config": {"master":{"addr":"127.0.0.1:6379","password":"","db":1},"slave":{"addr":"127.0.0.1:6379","password":"","db":1}}, "traceId": ""}
2019-11-15T22:46:13.895+0800 DEBUG setting/etcd.go:35 setting.etcd: Backend config {"backend": "etcdv3", "machines": ["10.10.1.29:2379"], "keyspace": "", "traceId": ""}
2019-11-15T22:46:13.902+0800 DEBUG setting/etcd.go:102 setting.etcd: Retrieved redis-key-val from etcd store {"key": "root/config/common/database/redis", "config": {"master":{"addr":"127.0.0.1:6379","password":"","db":1},"slave":{"addr":"127.0.0.1:6379","password":"","db":1}}, "traceId": ""}
2019-11-15 22:46:13.905270 I | Initializing logging reporter
INFO[0001] new command Command="&{}"
2019-11-15T22:46:13.907+0800 DEBUG setting/etcd.go:35 setting.etcd: Backend config {"backend": "etcdv3", "machines": ["10.10.1.29:2379"], "keyspace": "", "traceId": ""}
2019-11-15T22:46:13.916+0800 DEBUG setting/etcd.go:65 setting.etcd: Retrieved mysql-key-val from etcd store {"key": "root/config/common/database/mysql/passport", "config": {"master":{"dsn":"","user":"root","pass":"GL@c*Nm#dkaLH!FNe","host":"rm-j6cli54dhwo5ski2quo.mysql.rds.aliyuncs.com","port":3306,"dbname":"peduli","max_open":100,"max_idle":10},"slave":{"dsn":"","user":"root","pass":"GL@c*Nm#dkaLH!FNe","host":"rm-j6cli54dhwo5ski2quo.mysql.rds.aliyuncs.com","port":3306,"dbname":"peduli","max_open":100,"max_idle":10}}, "traceId": ""}
DEBU[0003] new passport mysql store MasterDB="&{ 0 0xc4200ba6e0 false 2 {0xc42034a280} map[] 0xc4200e0a20 0x16c7aa0 0xc42019bee0 false}" SlaveDB="&{ 0 0xc4201c3ea0 false 2 {0xc42034a280} map[] 0xc420376480 0x16c7aa0 0xc4201e2780 false}"
DEBU[0003] new store MySQL="&{0xc4200e0a20 0xc420376480 0xc4200831e0}" config="&{dev [10.10.1.29:2379] 0 0 }"
2019-11-15T22:46:15.736+0800 DEBUG setting/etcd.go:35 setting.etcd: Backend config {"backend": "etcdv3", "machines": ["10.10.1.29:2379"], "keyspace": "", "traceId": ""}
2019-11-15T22:46:15.742+0800 DEBUG setting/etcd.go:83 setting.etcd: Retrieved service-key-val from etcd store {"key": "root/config/custom/go_service_passport", "config": {"Runmode":"","EtcdEndpoints":null,"AppConfigPath":"","ServiceName":"","ServiceIP":"","ServiceHttpPort":0,"ServiceRpcPort":0,"log_level":"debug","log_path":"","domain_www":"https://www.pedulisehat.id","domain_api":"","domain_passport":"https://passport-qa.pedulisehat.id","domain_project":"https://project-qa.pedulisehat.id","domain_trade":"https://trade-qa.pedulisehat.id","domain_static_avatar":"https://static-qa.pedulisehat.id/img/avatar","url_share_project":"","url_ico":"","host_passport":"","host_project":"","host_trade":"","url_share":"","domain_gtry":""}, "traceId": ""}
DEBU[0003] setting.NewConfig Config="&{dev [10.10.1.29:2379] go_service_passport 0.0.0.0 8080 9080 debug https://www.pedulisehat.id https://passport-qa.pedulisehat.id https://project-qa.pedulisehat.id https://trade-qa.pedulisehat.id https://static-qa.pedulisehat.id/img/avatar }"
INFO[0003] command run ... Command="&{ 0xc4201e21c0}"
2019-11-15 22:46:15.744629 I | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
2019-11-15 22:46:15.745561 I | RPC Server has been started up. 172.17.0.2:9080
可以发现我们的 passport 服务可以正常启动了,查看下容器的运行状态:
$ docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b58ee39088dd passport-multi-stage:1.0.9 "./go_service_passpo…" 5 seconds ago Up 4 seconds 9080/tcp, 0.0.0.0:80->8080/tcp recursing_poitras
参数说明:
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: isgiker
Password:
Login Succeeded
接着,为本地的 image 标注用户名和版本。
$ docker image tag [imageName] [username]/[repository]:[tag]
# 实例
$ docker image tag passport-multi-stage:1.0.9 isgiker/passport-multi-stage:1.0.9
最后,发布 image 文件。
$ docker image push isgiker/passport-multi-stage:1.0.9
The push refers to repository [docker.io/isgiker/passport-multi-stage]
e22072d3470d: Pushed
9136612a4372: Pushed
dac53910d311: Pushed
77cae8ab23bf: Mounted from library/alpine
1.0.9: digest: sha256:b5e9f0db2bd3e9ba684c8c359b087aa097adbb6a7426732b6d9246ca1b3dd6dc size: 1158
可以通过 docker search 命令来查找官方仓库中的镜像,
Command Description
docker image build Build an image from a Dockerfile
docker image history Show the history of an image
docker image import Import the contents from a tarball to create a filesystem image
docker image inspect Display detailed information on one or more images
docker image load Load an image from a tar archive or STDIN
docker image ls List images
docker image prune Remove unused images
docker image pull Pull an image or a repository from a registry
docker image push Push an image or a repository to a registry
docker image rm Remove one or more images
docker image save Save one or more images to a tar archive (streamed to STDOUT by default)
docker image tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker image 命令
Command Description
docker container attach Attach local standard input, output, and error streams to a running container
docker container commit Create a new image from a container’s changes
docker container cp Copy files/folders between a container and the local filesystem
docker container create Create a new container
docker container diff Inspect changes to files or directories on a container’s filesystem
docker container exec Run a command in a running container
docker container export Export a container’s filesystem as a tar archive
docker container inspect Display detailed information on one or more containers
docker container kill Kill one or more running containers
docker container logs Fetch the logs of a container
docker container ls List containers
docker container pause Pause all processes within one or more containers
docker container port List port mappings or a specific mapping for the container
docker container prune Remove all stopped containers
docker container rename Rename a container
docker container restart Restart one or more containers
docker container rm Remove one or more containers
docker container run Run a command in a new container
docker container start Start one or more stopped containers
docker container stats Display a live stream of container(s) resource usage statistics
docker container stop Stop one or more running containers
docker container top Display the running processes of a container
docker container unpause Unpause all processes within one or more containers
docker container update Update configuration of one or more containers
docker container wait Block until one or more containers stop, then print their exit codes
docker container 命令
docker 命令
Child commands
Command Description
docker attach Attach local standard input, output, and error streams to a running container
docker build Build an image from a Dockerfile
docker builder Manage builds
docker checkpoint Manage checkpoints
docker commit Create a new image from a container’s changes
docker config Manage Docker configs
docker container Manage containers
docker context Manage contexts
docker cp Copy files/folders between a container and the local filesystem
docker create Create a new container
docker deploy Deploy a new stack or update an existing stack
docker diff Inspect changes to files or directories on a container’s filesystem
docker engine Manage the docker engine
docker events Get real time events from the server
docker exec Run a command in a running container
docker export Export a container’s filesystem as a tar archive
docker history Show the history of an image
docker image Manage images
docker images List images
docker import Import the contents from a tarball to create a filesystem image
docker info Display system-wide information
docker inspect Return low-level information on Docker objects
docker kill Kill one or more running containers
docker load Load an image from a tar archive or STDIN
docker login Log in to a Docker registry
docker logout Log out from a Docker registry
docker logs Fetch the logs of a container
docker manifest Manage Docker image manifests and manifest lists
docker network Manage networks
docker node Manage Swarm nodes
docker pause Pause all processes within one or more containers
docker plugin Manage plugins
docker port List port mappings or a specific mapping for the container
docker ps List containers
docker pull Pull an image or a repository from a registry
docker push Push an image or a repository to a registry
docker rename Rename a container
docker restart Restart one or more containers
docker rm Remove one or more containers
docker rmi Remove one or more images
docker run Run a command in a new container
docker save Save one or more images to a tar archive (streamed to STDOUT by default)
docker search Search the Docker Hub for images
docker secret Manage Docker secrets
docker service Manage services
docker stack Manage Docker stacks
docker start Start one or more stopped containers
docker stats Display a live stream of container(s) resource usage statistics
docker stop Stop one or more running containers
docker swarm Manage Swarm
docker system Manage Docker
docker tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker top Display the running processes of a container
docker trust Manage trust on Docker images
docker unpause Unpause all processes within one or more containers
docker update Update configuration of one or more containers
docker version Show the Docker version information
docker volume Manage volumes
docker wait Block until one or more containers stop, then print their exit codes
docker container 命令 路径>路径2>路径1>端口2>端口1>容器端口>宿主端口>