docker-镜像
文章目录
镜像
Linux 操作系统由内核空间和用户空间组成。
- rootfs,用户空间的文件系统是 rootfs,包含我们熟悉的 /dev, /proc, /bin 等目录。 对于 base 镜像来说,底层直接用 Host 的 kernel,自己只需要提供 rootfs 就行了。
- bootfs,内核空间是 kernel,Linux 刚启动时会加载 bootfs 文件系统,之后 bootfs 会被卸载掉。不同 Linux 发行版的区别主要就是 rootfs。
最小的镜像
bash
[root@docker ~]# docker pull hello-world
[root@docker ~]# docker images
[root@docker ~ 14:19:06]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
#第一次运行前会多出下面几行,表示从仓库拉取镜像
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:d4aaab6242e0cace87e2ec17a2ed3d779d18fbfd03042ea58f2995626396a274
Status: Downloaded newer image for hello-world:latest
base镜像
bash
[root@docker ~]# docker pull centos:7
[root@docker ~]# docker images centos:7
[root@docker ~ 14:21:05]# cat /etc/os-release
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Stream 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
[root@docker ~ 14:21:17]# uname -r
4.18.0-553.6.1.el8.x86_64
#Host OS kernel 为 4.18.0
[root@docker ~]# docker run -it ubuntu
root@4264749aa4af:/# uname -r
4.18.0-553.6.1.el8.x86_64
[root@docker ~]# docker run -it centos:7
[root@72397b60bb10 /]# uname -r
4.18.0-553.6.1.el8.x86_64
创建镜像
bash
#在 ubuntu base 镜像中安装 vim并保存为新镜像
[root@docker ~]# docker run -it ubuntu
#以交互模式进入容器,并打开终端
root@8dbdff6d3d88:/# vim
bash: vim: command not found
root@8dbdff6d3d88:/# apt-get update
root@8dbdff6d3d8:/# apt-get install -y vim
Geographic area: 5
Time zone: 69
#打开一个新窗口中查看当前运行的容器
[root@docker ~]# docker ps
[root@docker / 15:06:54]# docker commit laughing_beaver ubuntu-with-vim
sha256:6bcd3d830bac99d0d8543f28302fa105a15cff1b7125a7eaaa9000158489040a
[root@docker / 15:08:38]# docker images
[root@docker / 15:08:47]# docker run -it ubuntu-with-vim
root@74b39be0eb19:/# which vim
/usr/bin/vim
root@74b39be0eb19:/# vim file1
#构建镜像
[root@docker ~ 15:33:32]# pwd
/root
[root@docker ~ 15:33:36]# cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y vim
[root@docker ~ 15:29:20]# docker build -t ubuntu-with-vim-dockerfile .
[root@docker ~ 15:32:18]# docker images
#对比
[root@docker ~ 15:32:24]# docker history ubuntu
[root@docker ~ 15:32:45]# docker history ubuntu-with-vim-dockerfile
#镜像的缓存特性
[root@docker ~]# pwd
/root
[root@docker ~]# ls
Dockerfile
[root@docker ~]# touch testfile
[root@docker ~]# ls
Dockerfile testfile
[root@docker ~]# vim Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y vim
COPY testfile /
[root@docker ~]# docker build -t ubuntu-with-vim-dockerfile-2 .
[root@docker ~]# vim Dockerfile
FROM ubuntu
COPY testfile /
RUN apt-get update && apt-get install -y vim
[root@docker ~]# docker build -t ubuntu-with-vim-dockerfile-3 .
#调试Dockerfile
[root@docker ~]# ls
Dockerfile testfile
[root@docker ~]# vim Dockerfile
FROM busybox
RUN touch tmpfile
RUN /bin/bash -c "echo continue to build..."
COPY testfile /
[root@docker ~]# docker build -t image-debug .
#Dockerfile 在执行第三步 RUN 指令时失败
# 找出错误原因,修改错误
[root@docker ~]# vim Dockerfile
FROM busybox
RUN touch tmpfile
RUN /bin/sh -c "echo continue to builld..."
#将错误的/bin/bash修改为正确的/bin/sh
COPY testfile /
[root@docker ~]# docker build -t image-debug .
综合实验
bash
[root@docker ~ 10:03:54]# pwd
/root
[root@docker ~ 10:04:06]# ls
anaconda-ks.cfg
[root@docker ~ 10:04:09]# rm -f anaconda-ks.cfg
[root@docker ~ 10:04:15]# ls
[root@docker ~ 10:04:15]# touch tmpfile2
[root@docker ~ 10:04:29]# cp /etc/passwd .
[root@docker ~ 10:04:38]# tar -cvzf passwd.tar.gz passwd
passwd
[root@docker ~ 10:05:05]# ls
passwd passwd.tar.gz tmpfile2
[root@docker ~ 10:05:06]# rm -f passwd
[root@docker ~ 10:05:29]# vim Dockerfile
[root@docker ~ 10:07:56]# cat Dockerfile
FROM busybox
MAINTAINER 123@qq.com
WORKDIR /testdir
COPY tmpfile2 .
ADD passwd.tar.gz .
ENV WELCOME "You are in my container,welcome!"
[root@docker ~ 10:07:58]# ls
Dockerfile passwd.tar.gz tmpfile2
[root@docker ~ 10:08:22]# docker build -t my-image .
[+] Building 6.5s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 174B 0.0s
=> [internal] load metadata for docker.io/library/busybox:latest 4.3s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/busybox:latest@sha256:2383baad1860bbe9d8a7a8437 2.0s
=> => resolve docker.io/library/busybox:latest@sha256:2383baad1860bbe9d8a7a8437 0.0s
=> => sha256:08ef35a1c3f050afbbd64194ffd1b8d5878659f5491567f26d1c81 459B / 459B 0.0s
=> => sha256:e59838ecfec5e79eb4371e9995ef86c8000fe1c67d7b9fa7b5 2.21MB / 2.21MB 1.9s
=> => sha256:2383baad1860bbe9d8a7a843775048fd07d8afe292b94bd876 9.54kB / 9.54kB 0.0s
=> => sha256:870e815c3a50dd0f6b40efddb319c72c32c3ee340b5a3e89459042 610B / 610B 0.0s
=> => extracting sha256:e59838ecfec5e79eb4371e9995ef86c8000fe1c67d7b9fa7b57e996 0.1s
=> [internal] load build context 0.0s
=> => transferring context: 618B 0.0s
=> [2/4] WORKDIR /testdir 0.0s
=> [3/4] COPY tmpfile2 . 0.0s
=> [4/4] ADD passwd.tar.gz . 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:ffd7f5831ec0d59bcfd17934fa9f74d1ae66da023c311160c2f5 0.0s
=> => naming to docker.io/library/my-image 0.0s
[root@docker ~ 10:08:45]# docker run -it my-image
/testdir # pwd
/testdir
/testdir # ls
passwd tmpfile2
/testdir # echo $WELCOME
You are in my container,welcome!
不同格式
bash
#Shell 格式
[root@docker ~]# vim Dockerfile
FROM busybox
ENV name xxx
ENTRYPOINT echo "Hello, $name"
[root@docker ~]# docker build -t dockerfile1 .
[root@docker ~]# docker run dockerfile1
Hello,xxx
#Exec 格式
[root@docker ~]# vim Dockerfile
FROM busybox
ENV name xxx
ENTRYPOINT ["/bin/echo","Hello, $name"]
[root@docker ~]# docker build -t dockerfile2 .
[root@docker ~]# docker run dockerfile2
hello,$name
#
[root@docker ~]# vim Dockerfile
FROM busybox
ENV name xxx
ENTRYPOINT ["/bin/sh", "-c", "echo Hello, $name"]
[root@docker ~]# docker build -t dockerfile3 .
[root@docker ~]# docker run dockerfile3
Hello, xxx
#CMD
[root@docker ~ 11:06:56]# vim Dockerfile
[root@docker ~ 11:07:28]# docker build -t dockerfile5 .
[+] Building 2.2s (5/5) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 73B 0.0s
=> [internal] load metadata for docker.io/library/busybox:latest 2.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/1] FROM docker.io/library/busybox:latest@sha256:2383baad1860bbe9d8 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d153164533cdba1b9abd1a715b77d7669e5d22fa6c312399c331 0.0s
=> => naming to docker.io/library/dockerfile5 0.0s
[root@docker ~ 11:07:39]# docker run -it dockerfile5
Hello,world
#Exec 格式
[root@docker ~ 11:07:48]# vim Dockerfile
[root@docker ~ 11:09:17]# cat Dockerfile
FROM busybox
ENTRYPOINT ["/bin/echo","Hello"]
CMD ["world"]
[root@docker ~ 11:09:18]# docker build -t dockerfile6 .
[+] Building 0.6s (5/5) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 97B 0.0s
=> [internal] load metadata for docker.io/library/busybox:latest 0.6s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/1] FROM docker.io/library/busybox:latest@sha256:2383baad1860bbe9d8 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:24e440bbbfc720fd05894aa2d6cc95a0db58eaff7460a38cdb61 0.0s
=> => naming to docker.io/library/dockerfile6 0.0s
[root@docker ~ 11:09:37]# docker run -it dockerfile6
Hello world
[root@docker ~ 11:09:45]# docker run -it dockerfile6 xxx
Hello xxx
#Shell 格式
FROM busybox
ENTRYPOINT echo "Hello"
CMD ["world"]
[root@docker ~]# docker build -t dockerfile7 .
[root@docker ~]# docker run -it dockerfile7
Hello,
[root@docker ~]# docker run -it dockerfile7 gqd
Hello
配置SSH镜像
bash
[root@docker ~ 11:44:44]# cat centos.ssh.dockerfile
FROM centos:8.4.2105
MAINTAINER gaoqiaodong
RUN minorver=8.4.2105 && \
sed -e "s|^mirrorlist=|#mirrorlist=|g" \
-e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo
RUN yum install -y openssh-server
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
RUN echo "root:huawei" | chpasswd
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
[root@docker ~ 11:09:57]# vim centos.ssh.dockerfile
[root@docker ~ 11:41:36]# ls
centos.ssh.dockerfile Dockerfile passwd.tar.gz tmpfile2
[root@docker ~ 11:41:40]# docker build -t centos:ssh -f centos.ssh.dockerfile .
[root@docker ~ 11:42:45]# docker history centos:ssh
IMAGE CREATED CREATED BY SIZE COMMENT
80ab9d815488 24 seconds ago CMD ["/usr/sbin/sshd" "-D"] 0B buildkit.dockerfile.v0
<missing> 24 seconds ago EXPOSE map[22/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 24 seconds ago RUN /bin/sh -c echo "root:huawei" | chpasswd... 1.77kB buildkit.dockerfile.v0
<missing> 24 seconds ago RUN /bin/sh -c ssh-keygen -t ecdsa -f /etc/s... 695B buildkit.dockerfile.v0
<missing> 25 seconds ago RUN /bin/sh -c ssh-keygen -t rsa -f /etc/ssh... 3.18kB buildkit.dockerfile.v0
<missing> 25 seconds ago RUN /bin/sh -c yum install -y openssh-server... 51.9MB buildkit.dockerfile.v0
<missing> 40 seconds ago RUN /bin/sh -c minorver=8.4.2105 && sed -e "... 17.6kB buildkit.dockerfile.v0
<missing> 40 seconds ago MAINTAINER gaoqiaodong 0B buildkit.dockerfile.v0
<missing> 4 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc... 0B
<missing> 4 years ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0... 231MB
[root@docker ~ 11:43:09]# docker run -d -p 2022:22 --name sshtest centos:ssh
1f1ac0537e7349b314e7de5ba5f4d99583115241471aa4352cfe102603315e3b
[root@docker ~ 11:43:25]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1ac0537e73 centos:ssh "/usr/sbin/sshd -D" 5 seconds ago Up 5 seconds 0.0.0.0:2022->22/tcp, :::2022->22/tcp sshtest
[root@docker ~ 11:43:30]# ssh root@localhost -p 2022
[root@1f1ac0537e73 ~]#
自定义httpd镜像
bash
[root@docker ~ 14:01:20]# vim httpd.dockerfile
[root@docker ~ 14:02:18]# echo Hello world > index.html
[root@docker ~ 14:02:38]# docker build -t httpd:centos -f httpd.dockerfile .
[+] Building 13.1s (9/9) FINISHED docker:default
=> [internal] load build definition from httpd.dockerfile 0.0s
=> => transferring dockerfile: 469B 0.0s
=> [internal] load metadata for docker.io/library/centos:8.4.2105 1.4s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/centos:8.4.2105@sha256:a27fd8080b517143cbbbab9d 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 49B 0.0s
=> CACHED [2/4] RUN minorver=8.4.2105 && sed -e "s|^mirrorlist=|#mirrorlist=|g" 0.0s
=> [3/4] RUN yum install -y httpd && yum clean all && rm -rf /var/cache/yum 11.5s
=> [4/4] COPY index.html /var/www/html/ 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:8cf2be2d8f788f52f036432cc6c8369a68f179de986a513daced 0.0s
=> => naming to docker.io/library/httpd:centos 0.0s
[root@docker ~ 14:03:30]# docker history httpd:centos
IMAGE CREATED CREATED BY SIZE COMMENT
8cf2be2d8f78 1 second ago CMD ["/usr/sbin/httpd" "-DFOREGROUND"] 0B buildkit.dockerfile.v0
<missing> 1 second ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 1 second ago COPY index.html /var/www/html/ # buildkit 12B buildkit.dockerfile.v0
<missing> 1 second ago RUN /bin/sh -c yum install -y httpd && yum c... 21.6MB buildkit.dockerfile.v0
<missing> 2 hours ago RUN /bin/sh -c minorver=8.4.2105 && sed -e "... 17.6kB buildkit.dockerfile.v0
<missing> 2 hours ago MAINTAINER gaoqiaodong 0B buildkit.dockerfile.v0
<missing> 4 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc... 0B
<missing> 4 years ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0... 231MB
[root@docker ~ 14:03:31]# docker run -d -p 80:80 --name myweb httpd:centos
d4d26efc80f9fbe6bb1048beab3935d8601c4d07caf71a23671875d21fd92de3
[root@docker ~ 14:03:38]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4d26efc80f9 httpd:centos "/usr/sbin/httpd -DF..." 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp myweb
[root@docker ~ 14:03:42]# curl localhost
Hello world
[root@docker ~ 14:03:48]# cat httpd.dockerfile
FROM centos:8.4.2105
MAINTAINER gaoqiaodong
RUN minorver=8.4.2105 \
&& sed -e "s|^mirrorlist=|#mirrorlist=|g" -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" -i.bak /etc/yum.repos.d/CentOS-*.repo
RUN yum install -y httpd && yum clean all && rm -rf /var/cache/yum
COPY index.html /var/www/html/
EXPOSE 80
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
自定义mycentosjava8
bash
[root@docker ~ 14:04:09]# mkdir myfile ; cd myfile
[root@docker myfile 14:19:27]# vim Dockerfile
[root@docker myfile 14:20:18]# cat Dockerfile
FROM centos:8.4.2105
MAINTAINER gaoqiaodong<6946630@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置yum源
RUN minorver=8.4.2105 \
&& sed -e "s|^mirrorlist=|#mirrorlist=|g" -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" -i.bak /etc/yum.repos.d/CentOS-*.repo
#安装vim编辑器
RUN yum -y install vim
#安装ifconfig命令查看网络IP
RUN yum -y install net-tools
#安装java8及lib库
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把jdk-8u461-linux-x64.tar.gz添加到容器中,安装包必须要和Dockerfile文件在同一位置
ADD jdk-8u461-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_461
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "success--------------ok"
CMD /bin/bash
[root@docker myfile 14:20:21]# ls
Dockerfile
[root@docker myfile 14:20:27]# ls
Dockerfile jdk-8u461-linux-x64.tar.gz
[root@docker myfile 14:21:25]# docker build -t centosjava8:461 .
[+] Building 51.9s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.04kB 0.0s
=> [internal] load metadata for docker.io/library/centos:8.4.2105 2.5s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/8] FROM docker.io/library/centos:8.4.2105@sha256:a27fd8080b517143c 0.0s
=> [internal] load build context 1.0s
=> => transferring context: 79.45MB 1.0s
=> [2/8] WORKDIR /usr/local 0.1s
=> [3/8] RUN minorver=8.4.2105 && sed -e "s|^mirrorlist=|#mirrorlist=|g" -e "s| 1.0s
=> [4/8] RUN yum -y install vim 32.5s
=> [5/8] RUN yum -y install net-tools 2.6s
=> [6/8] RUN yum -y install glibc.i686 11.2s
=> [7/8] RUN mkdir /usr/local/java 0.5s
=> [8/8] ADD jdk-8u461-linux-x64.tar.gz /usr/local/java/ 1.3s
=> exporting to image 0.3s
=> => exporting layers 0.3s
=> => writing image sha256:679a09ba26c72247bf2e9b5081cf1f1ef2d74a1a5530001ab0cf 0.0s
=> => naming to docker.io/library/centosjava8:461 0.0s
[root@docker myfile 14:22:53]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centosjava8 461 679a09ba26c7 5 seconds ago 537MB
[root@docker myfile 14:22:57]# docker run -it centosjava8:461 /bin/bash
[root@684fb0a8dc9b local]# pwd
/usr/local
[root@684fb0a8dc9b local]# java -version
java version "1.8.0_461"
Java(TM) SE Runtime Environment (build 1.8.0_461-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)
[root@684fb0a8dc9b local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@684fb0a8dc9b local]# vim file1
删除镜像容器命令
bash
#删除容器
[root@docker harbor 16:39:06]# docker rm -f $(docker ps -aq)
#删除镜像
[root@docker ~ 16:09:41]# docker rmi -f $(docker images -aq)
企业级私有仓库Harbor
bash
#harbor下载:https://github.com/goharbor/harbor/releases
[root@docker ~]# wget
https://github.com/goharbor/harbor/releases/download/v2.9.1/harbor-offlineinstaller-v2.9.1.tgz
[root@docker ~ 16:26:34]# tar -xvf harbor-offline-installer-v2.9.1.tgz
harbor/harbor.v2.9.1.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@docker ~ 16:27:30]# mkdir /opt/harbor
[root@docker ~ 16:27:42]# mv harbor/* /opt/harbor/
[root@docker ~ 16:27:56]# cd /opt/harbor/
[root@docker harbor 16:28:00]# docker load -i harbor.v2.9.1.tar.gz
[root@docker harbor 16:28:40]# cp -ar harbor.yml.tmpl harbor.yml
[root@docker harbor 16:29:03]# vim harbor.yml
[root@docker harbor 16:29:58]# ./prepare
[root@docker harbor 16:30:09]# ./install.sh
#先清理占用端口容器
[root@docker harbor 16:39:06]# docker rm -f $(docker ps -aq)
[root@docker harbor 16:39:56]# vim /etc/docker/daemon.json
[root@docker harbor 16:48:11]# cat /etc/docker/daemon.json
{
"insecure-registries": ["192.168.108.30"],
"registry-mirrors": [ "https://054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweic
}
[root@docker harbor 16:49:21]# systemctl daemon-reload
[root@docker harbor 16:49:30]# systemctl restart docker
[root@docker harbor 16:49:42]# ./install.sh
[root@docker harbor 16:50:35]# cd ~/.docker/
[root@docker .docker 16:50:42]# pwd
/root/.docker
[root@docker .docker 16:50:45]# cat config.json
{
"auths": {
"192.168.108.30": {
"auth": "aW1hZ2VzX2FkbWluOkxhb21hQDEyMw=="
},
"swr.cn-east-3.myhuaweicloud.com": {
"auth": "Y24tZWFzdC0zQEhTVDNXRktRSEhUTUlJN0xGWlcxOjc2NmEyNzU3OTUxMDYzNDQ0NjAyYTgwYzAyN2Q2ZTM5NDRhNTI5NDgxMzJlYWM4OTY2MzdmZmI5ODUyMWYzNzE="
}
}
}
#例如拉取nginx镜像
#格式:docker tag SOURCE_IMAGE[:TAG] 192.168.108.30/cloud/REPOSITORY[:TAG]
[root@docker .docker 16:54:07]# docker pull nginx
[root@docker .docker 16:56:15]# docker tag nginx:latest 192.168.108.30/cloud/nginx:latest
[root@docker .docker 16:56:20]# docker push 192.168.108.30/cloud/nginx:latest
[root@docker .docker 16:56:29]# docker pull 192.168.108.30/cloud/nginx
Using default tag: latest
latest: Pulling from cloud/nginx
Digest: sha256:a6dd519f4cc2f69a8f049f35b56aec2e30b7ddfedee12976c9e289c07b421804
Status: Image is up to date for 192.168.108.30/cloud/nginx:latest
192.168.108.30/cloud/nginx:latest
#清理环境
[root@docker .docker 16:58:41]# cd /opt/harbor/
[root@docker harbor 16:58:48]# docker compose down
WARN[0000] /opt/harbor/docker-compose.yml: `version` is obsolete
[+] Running 10/10
✔ Container registryctl Removed 0.1s
✔ Container nginx Removed 0.2s
✔ Container harbor-jobservice Removed 0.1s
✔ Container harbor-portal Removed 0.1s
✔ Container harbor-core Removed 0.1s
✔ Container registry Removed 0.1s
✔ Container redis Removed 0.2s
✔ Container harbor-db Removed 0.1s
✔ Container harbor-log Removed 10.1s
✔ Network harbor_harbor Removed 0.1s
[root@docker harbor 16:59:04]# docker images |grep harbor|awk '{print $1":"$2}' | xargs docker rmi
[root@docker harbor 16:59:13]# rm -rf /data
[root@docker harbor 16:59:17]# cd
镜像保存和导入
bash
[root@docker ~]# docker images busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 6fd955f66c23 16 months ago 4.26MB
[root@docker ~]# docker tag busybox:latest busybox:v1
[root@docker ~]# docker tag busybox:latest busybox:v2
[root@docker ~]# docker images busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 6fd955f66c23 16 months ago 4.26MB
busybox v1 6fd955f66c23 16 months ago 4.26MB
busybox v2 6fd955f66c23 16 months ago 4.26MB
#保存
[root@docker ~]# docker save httpd -o httpd.tar
[root@docker ~]# docker save httpd hello-world -o images.tar
[root@docker ~]# ls
httpd.tar images.tar
#导入
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 90f191b9781e 11 days ago 148MB
hello-world latest 74cc54e27dc4 6 months ago 10.1kB
[root@docker ~]# docker rmi httpd:latest hello-world:latest
[root@docker ~]# docker load -i images.tar
busybox latest 6fd955f66c23 16 months ago 4.26MB
busybox v1 6fd955f66c23 16 months ago 4.26MB
busybox v2 6fd955f66c23 16 months ago 4.26MB
#保存
root@docker \~\]# docker save httpd -o httpd.tar \[root@docker \~\]# docker save httpd hello-world -o images.tar \[root@docker \~\]# ls httpd.tar images.tar #导入 \[root@docker \~\]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest 90f191b9781e 11 days ago 148MB hello-world latest 74cc54e27dc4 6 months ago 10.1kB \[root@docker \~\]# docker rmi httpd:latest hello-world:latest \[root@docker \~\]# docker load -i images.tar ``` ```