点击上方蓝色“3分钟云计算”关注我们,设个星标,每天学习点云计算知识
程序员最爱说的是:“ 我这本地运行的好好的,怎么在客户那就不行了呢!”
关于Docker 的文章和相关书籍非常繁多,从Docker 的概念,架构,到容器的编排管理等等,内容很多。但是对于非运维人员来说,其实一篇文章就够了,那些书籍,可以当作工具书使用,需要的时候再去查。
本文将给出一个简单的例子,带你轻松了解Docker到底是做什么的,为啥如今这么火?
1,安装Docker
[jzhang@dhcp-140-36 show]$ cat /etc/redhat-release
Fedora release 28 (Twenty Eight)
[root@preserve-olm-env data]# docker version
Client: Docker Engine - Community
Version: 19.03.9
API version: 1.40
Go version: go1.13.10
Git commit: 9d988398e7
Built: Fri May 15 00:25:27 2020
OS/Arch: linux/amd64
Experimental: false
[root@preserve-olm-env data]# which docker
/usr/bin/docker
[root@preserve-olm-env data]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set
with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
checkpoint Manage checkpoints
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
2,编写一个简单程序。Golang 代码,输出 Hello Docker! 如下,
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/docker", handleDocker)
http.ListenAndServe(":8080", nil)
}
func handleDocker(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello Docker!")
}
[jzhang@dhcp-140-36 show]$ export CGO_ENABLED=0
[jzhang@dhcp-140-36 show]$ go build
[jzhang@dhcp-140-36 show]$ ls
main.go show
[jzhang@dhcp-140-36 show]$ ./show
3,容器化,也就是把这个程序放到容器中去运行
[jzhang@dhcp-140-36 show]$ ls
Dockerfile main.go show
[jzhang@dhcp-140-36 show]$ cat Dockerfile
FROM scratch
Add show /
CMD ["/show"]
[jzhang@dhcp-140-36 show]$ docker build -t quay.io/jiazha/show:v1 .
Sending build context to Docker daemon 7.399MB
Step 1/3 : FROM scratch
--->
Step 2/3 : Add show /
---> e1d83a8ffad9
Step 3/3 : CMD ["/show"]
---> Running in 8a13dafb50df
Removing intermediate container 8a13dafb50df
---> 55ccf78c9a91
Successfully built 55ccf78c9a91
Successfully tagged quay.io/jiazha/show:v1
[jzhang@dhcp-140-36 show]$ docker image ls quay.io/jiazha/show:v1
REPOSITORY TAG IMAGE ID CREATED SIZE
quay.io/jiazha/show v1 55ccf78c9a91 4 minutes ago 7.39MB
4, 容器化运行
[jzhang@dhcp-140-36 show]$ docker run quay.io/jiazha/show:v1
[jzhang@dhcp-140-36 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61326acc97fb quay.io/jiazha/show:v1 "/show" 8 seconds ago Up 7 seconds kind_haibt
[jzhang@dhcp-140-36 ~]$ docker inspect 61326acc97fb --format='{{.NetworkSettings.IPAddress}}'
172.17.0.2
[jzhang@dhcp-140-36 ~]$ curl 172.17.0.2:8080/docker
Hello Docker!
[jzhang@dhcp-140-36 show]$ docker run -p 80:8080 quay.io/jiazha/show:v1
到此,程序容器化已经完成了,是不是很简单?
5, 发布该镜像
[jzhang@dhcp-140-36 show]$ docker push quay.io/jiazha/show:v1
The push refers to repository [quay.io/jiazha/show]
ce5f0c851050: Pushed
v1: digest: sha256:f052362823506b97b738ba48d4227981758936fd1c6c30ab69e76492825b3243 size: 528
6, 换个平台,运行show 镜像,这里使用的是Mac
mac:~ jianzhang$ docker pull quay.io/jiazha/show:v1
v1: Pulling from jiazha/show
038b2980cdca: Pull complete
Digest: sha256:f052362823506b97b738ba48d4227981758936fd1c6c30ab69e76492825b3243
Status: Downloaded newer image for quay.io/jiazha/show:v1
mac:~ jianzhang$ docker run -p 8083:8080 quay.io/jiazha/show:v1
看到没,在Fedora 系统编译的show 指令,在Mac 上完美运行。其实,它是在容器中运行的。
END
Docker 其实是提供了一个统一的运行环境,一次构建,随处运行。类似集装箱,把程序运行所需要的文件,配置都放到一个箱子里,不管这个箱子最终被搬到哪里,哪个平台,开箱即用。完美解决了开发环境与客户实际运行环境不一致的问题。
程序员还敢说开篇那句话吗?