掌柜
docker核心要素
01/28
本文最后更新于2023年07月07日,已超过515天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
架构
三大核心要素:镜像(Image)、容器(Container)、仓库(Registry)
- 镜像(Image)
打包了业务代码及运行环境的包,是静态的文件,不能直接对外提供服务。 - 容器(Container)
镜像的运行时,可以对外提供服务。 - 仓库(Registry)
存放镜像的地方 - 公有仓库,Docker Hub,阿里,网易...
私有仓库,企业内部搭建
- Docker Registry,Docker官方提供的镜像仓库存储服务
- Harbor, 是Docker Registry的更高级封装,它除了提供友好的Web UI界面,角色和用户权限管理,用户操作审计等功能
- 镜像访问地址形式 registry.devops.com/demo/hello:latest,若没有前面的url地址,则默认寻找Docker Hub中的镜像,若没有tag标签,则使用latest作为标签。 比如,docker pull nginx,会被解析成docker.io/library/nginx:latest
公有的仓库中,一般存在这么几类镜像
- 操作系统基础镜像(centos,ubuntu,suse,alpine)
- 中间件(nginx,redis,mysql,tomcat)
- 语言编译环境(python,java,golang)
- 业务镜像(django-demo...)
容器和仓库不会直接交互,都是以镜像为载体来操作。
镜像
# 拉取镜像
[root@test:~]$ docker pull nginx:alpine
alpine: Pulling from library/nginx
59bf1c3509f3: Pull complete
f3322597df46: Pull complete
d09cf91cabdc: Pull complete
3a97535ac2ef: Pull complete
919ade35f869: Pull complete
40e5d2fe5bcd: Pull complete
Digest: sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333
Status: Downloaded newer image for nginx:alpine
docker.io/library/nginx:alpine
# 查看本地镜像
[root@test:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine cc44224bfe20 4 weeks ago 23.5MB
# 使用 tag 命令,给镜像打标签
[root@test:~]$ docker tag nginx:alpine harbor.opstea.com/nginx:alpine
[root@test:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine cc44224bfe20 4 weeks ago 23.5MB
harbor.opstea.com/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
# 构建镜像
[root@test:~]$ docker build . -t my-nginx:ubuntu -f Dockerfile
容器
启动
# 使用镜像启动容器。--name 是命名该容器,-d是以后台进程方式运行
[root@test:~]$ docker run --name my-nginx-alpine -d nginx:alpine
b791e324466e03372c646b91382bd8898bce30481bf3d7aedc4ae8c144a1f790
# 查看正在运行的容器
[root@test:~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b791e324466e nginx:alpine "/docker-entrypoint.…" 2 seconds ago Up 2 seconds 80/tcp my-nginx-alpine
# 进入容器并查看容器进程
[root@test:~]$ docker exec -it my-nginx-alpine /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
33 nginx 0:00 nginx: worker process
34 nginx 0:00 nginx: worker process
35 root 0:00 /bin/sh
42 root 0:00 ps aux
/ #
# 了解容器启动后执行了什么命令
# 构建一个nginx镜像
[root@test:~/nginx]$ vim dockerfile
[root@test:~/nginx]$ cat dockerfile
FROM ubuntu
RUN apt-get update && apt install -y nginx
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
[root@test:~/nginx]$
[root@test:~/nginx]$ docker build . -t my-nginx:ubuntu -f dockerfile
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM ubuntu
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
---> ba6acccedd29
Step 2/3 : RUN apt-get update && apt install -y nginx
---> Running in db77803433d0
......
Setting up nginx (1.18.0-0ubuntu1.2) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Removing intermediate container db77803433d0
---> 5d6fe9c678aa
Step 3/3 : CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
---> Running in 5495ff9b2d2c
Removing intermediate container 5495ff9b2d2c
---> 5ffe8602f0cc
Successfully built 5ffe8602f0cc
Successfully tagged my-nginx:ubuntu
# 使用新镜像启动容器
[root@test:~/nginx]$ docker run --name my-nginx-ubuntu -d my-nginx:ubuntu
2c3551ff85baa1bdf1ec993ceb2972f99e1ee05e32938b702a9906e28c315302
[root@test:~/nginx]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c3551ff85ba my-nginx:ubuntu "/usr/sbin/nginx -g …" 4 seconds ago Up 3 seconds my-nginx-ubuntu
b791e324466e nginx:alpine "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 80/tcp my-nginx-alpine
[root@test:~/nginx]$
[root@test:~/nginx]$ docker exec -it my-nginx-ubuntu /bin/sh
# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 55268 5916 ? Ss 03:58 0:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 8 0.0 0.0 55596 2092 ? S 03:58 0:00 nginx: worker process
www-data 9 0.0 0.0 55596 2092 ? S 03:58 0:00 nginx: worker process
root 10 0.1 0.0 2600 648 pts/0 Ss 03:58 0:00 /bin/sh
root 17 0.0 0.0 5888 1476 pts/0 R+ 03:58 0:00 ps aux
#
# 上面进程1,就是 dockerfile CMD 指定运行的命令啦。
访问
- 在容器内访问
[root@test:~/nginx]$ docker exec -it my-nginx-alpine /bin/sh
/ # curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ #
- 容器外部访问
[root@test:~/nginx]$ docker rm -f my-nginx-alpine
my-nginx-alpine
[root@test:~/nginx]$ docker run --name my-nginx-alpine -d -p 8080:80 nginx:alpine
a26c53d60aec8d1c5b1de3337c7cd9615bd745d2a3b1345623e92c9c2bdeb7e3
[root@test:~/nginx]$ curl 172.16.3.132:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
通过浏览器访问: