Docker

2024-09-08 23 0

Docker Hub

Docker Hub是一个基于云的注册服务,它允许您链接到代码库,构建并测试您的映像,存储手动推送的映像,并链接到Docker Cloud,以便您可以将映像部署到您的主机。

它为容器映像发现、分发和变更管理、用户和团队协作以及整个开发流程中的工作流自动化提供了一个集中的资源。

图片

Docker Hub提供以下主要功能:

  • 图像存储库
  • 从社区和官方图书馆查找和提取图像,从您有权访问的私人图像库中管理、推送和提取图像。
  • 自动化构建
  • 当您对源代码库进行更改时,自动创建新的映像。
  • Web手册
  • 作为自动化构建的一个特性,Webhooks允许您在成功推送存储库之后触发操作。
  • 组织
  • 创建工作组来管理对图像存储库的访问。
  • GitHub和Bitbucket集成
  • 将中心和Docker图像添加到您当前的工作流程中。

docker镜像的获取

要从远程注册表(如您自己的Docker注册表)获取Docker映像并将其添加到您的本地系统,请使用docker pull命令:

docker pull (registry)[:(port)]/(namespace)/:(tag)

是在TCP上提供docker分发服务的主机(默认值:5000)

并在注册处识别由控制的特定图像

  • 有些注册表还支持raw对于那些,是可选的
  • 但是,当包含它时,提供的额外层级对于区分具有相同

层次结构的附加级别

命名空间示例(<命名空间>/<名称>)
组织red hat/kubernetes,google/kubernetes
登录(用户名)爱丽丝/应用程序,鲍勃/应用程序
角色开发/数据库、测试/数据库、生产/数据库

镜像的生成

镜像的生成途径:

  • Dockerfile
  • 基于容器制作
  • Docker Hub automated builds
图片

基于容器制作镜像

根据容器的更改创建新图像

使用方法:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

选项默认描述
—author, -a作者(例如,“约翰·汉尼拔·史密斯·汉尼拔@a-team.com”)
-c, --change list对创建的图像应用Dockerfile指令
-m, --message string提交消息
-p, --pausetrue提交期间暂停容器
[root@localhost ~]# docker pull busybox

[root@localhost ~]# docker run --name bi -it busybox
/ # ls
bin dev etc home proc root sys tmp usr var
/ # mkdir data
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # echo 'nihao' > data/index.html
/ # cat data/index.html
nihao

在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行

[root@localhost ~]# docker commit -p bi
sha256:b726393438ac33e9f2fb6701a64b4f982e432c4c75534538de6ac6d0273ce1f1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b726393438ac 7 seconds ago 1.24MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker tag b726393438ac luojialong123/v1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
luojialong123/v1 latest b726393438ac About a minute ago 1.24MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB

登录

[root@localhost ~]# 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: luojialong123
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
[root@localhost ~]# docker push luojialong123/v1
Using default tag: latest
The push refers to repository [docker.io/luojialong123/v1]
681c623b60a6: Pushed
01fd6df81c8e: Mounted from library/busybox
latest: digest: sha256:8050cdd117871dda41cd5f5209f84559954a8efb80ae4215abcb4868890cd71b size: 734
图片

使用新生成的镜像创建容器

[root@localhost ~]# docker run --name t1 -it luojialong123/v1
/ # ls data/
index.html
/ # cat data/index.html
nihao

由此可见,新生成的镜像中是包含了新增的内容的,但是此时有一个问题,那就是容器默认要启动的进程是什么?在这里,默认情况下是启动的sh进程,但我们是要启动一个http站点,所以我们要在创建镜像时将容器默认启动的进程设为httpd,这样一来我们就可以通过新生成的镜像来快速构建一个简单的http站点了。

使用docker inspect命令查看b1容器启动的默认进程是什么

[root@localhost ~]# docker inspect bi

"Cmd": [
"sh"
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",

重新生成镜像并上传

[root@localhost ~]# docker commit -a 'sean <sean1002@126.com>' -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p  bi luojialong123/v2
sha256:454d18d47b5d49afed0b5ce580862a5aff3e11e381d0a72e9fbbfbf85644f8d6

[root@localhost ~]# docker push luojialong123/v2
Using default tag: latest
The push refers to repository [docker.io/luojialong123/v2]
681c623b60a6: Mounted from luojialong123/v1
01fd6df81c8e: Mounted from luojialong123/v1
latest: digest: sha256:5852124f7a55fa90a76a21e0bee0fda36ff1443d1907d68029a0ee0caca99cca size: 734

使用新生成的镜像创建容器

[root@localhost ~]# docker run --name t2 -d luojialong123/v2
b55e354f84c13f427c5acc5bdfcdede78705501aab1cb2c742d3e1ff05542013
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b55e354f84c1 luojialong123/v2 "/bin/httpd -f -h /d…" 16 seconds ago Up 13 seconds t2

使用docker inspect命令查看t2容器启动的默认进程是什么,以及其IP地址,然后用curl命令访问该IP,看是否能访问到网页

[root@localhost ~]# curl 172.17.0.4
nihao

镜像的导入与导出

假如有2台主机,我们在主机1上做了一个镜像,主机2想用这个镜像怎么办呢?

我们可以在主机1上push镜像到镜像仓库中,然后在主机2上pull把镜像拉下来使用,这种方式就显得比较麻烦,假如我只是测试用的,在一台主机上做好镜像后在另一台主机上跑一下就行了,没必要推到仓库上然后又把它拉到本地来。

此时我们可以在已有镜像的基础上把镜像打包成一个压缩文件,然后拷贝到另一台主机上将其导入,这就是镜像的导入和导出功能。

docker中我们使用docker save进行导出,使用docker load进行导入。

在已生成镜像的主机上执行docker save导出镜像

[root@localhost ~]# docker save -o busybox  luojialong123/v1
[root@localhost ~]# ls
anaconda-ks.cfg busybox

在另一台服务器上查看并导入v1

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
luojialong123/v2 latest 454d18d47b5d 24 minutes ago 1.24MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB

[root@localhost ~]# docker load -i busybox
Loaded image: luojialong123/v1:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
luojialong123/v2 latest 454d18d47b5d 25 minutes ago 1.24MB
luojialong123/v1 latest b726393438ac 43 minutes ago 1.24MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB

练习题

查找
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7271 [OK]
拉取centos
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.io/library/centos:latest
查看centos镜像[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
luojialong123/v2 latest 454d18d47b5d 23 hours ago 1.24MB
luojialong123/v1 latest b726393438ac 23 hours ago 1.24MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
centos latest 5d0da3dc9764 10 months ago 231MB
创建centos容器
[root@localhost ~]# docker run -it --name centos centos /bin/bash
[root@bdb23a6fee64 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr

配置yum源
[root@bdb23a6fee64 /]# rm -rf /etc/yum.repos.d/*

[root@bdb23a6fee64 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 5447 0 --:--:-- --:--:-- --:--:-- 5435

[root@bdb23a6fee64 /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

[root@bdb23a6fee64 /]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
过程省略...
Complete!

[root@bdb23a6fee64 src]#wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@bdb23a6fee64 src]#wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@bdb23a6fee64 src]#wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2

安装apr
[root@bdb23a6fee64 src]# yum groups mark install "Development Tools"
Is this ok [y/N]: y
Complete!

[root@bdb23a6fee64 src]# useradd -r apache

[root@bdb23a6fee64 src]# yum -y install openssl-devel pcre-devel expat-devel libtool

[root@bdb23a6fee64 src]# tar xf apr-1.6.5.tar.bz2
[root@bdb23a6fee64 apr-1.6.5]# cd apr-1.6.5
[root@bdb23a6fee64 apr-1.6.5]# vim configure
# $RM "$cfgfile" 在行首添加#

[root@bdb23a6fee64 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@bdb23a6fee64 apr-1.6.5]# make && make install

安装apr-util
[root@bdb23a6fee64 ~]# cd /usr/src/
[root@bdb23a6fee64 src]# tar xf apr-util-1.6.1.tar.bz2
[root@bdb23a6fee64 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@bdb23a6fee64 apr-util-1.6.1]# make && make install

安装httpd
[root@bdb23a6fee64 apr-util-1.6.1]# cd /usr/src/
[root@bdb23a6fee64 src]# tar xf httpd-2.4.54.tar.bz2
[root@bdb23a6fee64 src]# cd httpd-2.4.54
[root@bdb23a6fee64 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
[root@bdb23a6fee64 httpd-2.4.54]# make && make install

编辑脚本
[root@bdb23a6fee64 httpd-2.4.54]# cd
[root@bdb23a6fee64 ~]# vim httpd.sh

#!/bin/bash
/usr/local/apache/bin/apachectl
sleep 5d
[root@bdb23a6fee64 ~]# chmod +x httpd.sh

在另一个终端生成镜像
[root@localhost ~]# docker commit -a 'frices <frices 1@2.com>' -c 'CMD ["./httpd.sh"]' -p centos frices/centos:httpd
sha256:db99d7691ae9459d43c491658edc75012fcd0bf643ff5b8f9f0b448ccd15af93
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
frices/centos httpd db99d7691ae9 10 seconds ago 730MB
centos latest 5d0da3dc9764 10 months ago 231MB



用新生成的镜像创建容器
[root@localhost ~]# docker run -d -it --name centos-httpd frices/centos:httpd
bce83cae524d67155171df93d3970912bc5715c2c896f7f82c46aebb06d05f76
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bce83cae524d frices/centos:httpd "./httpd.sh" About a minute ago Up About a minute centos-httpd
bdb23a6fee64 centos "/bin/bash" 3 hours ago Up 3 hours

[root@localhost ~]# docker inspect centos-httpd

"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
[root@localhost ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>

配置端口映射
[root@localhost ~]# docker run -d -it --name centos-httpd2 -p 80:80 frices/centos:httpd
d2d1700e3a571f4a7d61d116520408774a1a375eecf1db679e3d54cd4345ad80
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2d1700e3a57 frices/centos:httpd "./httpd.sh" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp centos-httpd2
bce83cae524d frices/centos:httpd "./httpd.sh" 6 minutes ago Up 6 minutes centos-httpd
bdb23a6fee64 centos "/bin/bash" 3 hours ago Up 3 hours centos
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*

相关文章

MySQL正则表达式
转VPS必备的开源项目
Docker配置容器自启动
Linux下”>/dev/null 2>&1″
GitHub打不开解决方案
Centos安装docker compose

发布评论