[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# vim docker.repo
[docker]
name=docker-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
gpgcheck=0
[update]
name=centos
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/
gpgcheck=0
安装docker-ce(docker-ce为社区版,免费开源版本。docker-ee为企业版,商用)
[root@server1 yum.repos.d]# yum install -y docker-ce
[root@server1 ~]# systemctl enable --now docker
[root@server1 ~]# docker info
激活内核选项
[root@server1 ~]# vim /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
[root@server1 ~]# sysctl --system
[root@server1 ~]# systemctl restart docker
启动容器
docker run -d --name web1 -p 80:80 nginx
//docker run: 运行一个新的容器
-d: 后台运行容器
--name web1: 设置容器的名称为"web1"
-p 80:80: 映射容器的80端口到宿主机的80端口
nginx: 使用的镜像名称为nginx
显示激活的容器,访问网页
[root@server1 ~]# docker ps
删除容器
[root@server1 ~]# docker rm -f web1
查询镜像
[root@server1 ~]# docker search yakexi007
拉取镜像到本地后启动容器
[root@server1 ~]# docker pull yakexi007/mario
[root@server1 ~]# docker run -d --name web1 -p 80:80 yakexi007/mario
访问网页
最后删除容器
[root@server1 ~]# docker rm -f web1
[root@server1 ~]# docker run -it --name demo busybox //-it: 使用交互式模式启动容器
/ # touch file1
ctrl+d: 退出容器后容器自动关闭
ctrl+pq: 退出容器后继续在后台运行
显示所有容器
[root@server1 ~]# docker ps -a
启动容器、进入容器
[root@server1 ~]# docker start demo
[root@server1 ~]# docker attach demo
[root@server1 ~]# docker commit -m "add files" demo demo:v1
//将名为"demo"的Docker容器提交为名为"demo:v1"的新镜像,其中"-m"选项表示为新镜像添加提交信息。[root@server1 ~]# docker history demo:v1
删除镜像
[root@server1 ~]# mkdir docker
[root@server1 ~]# cd docker/
[root@server1 docker]# cp ~/nginx-1.23.3.tar.gz .
[root@server1 docker]# vim Dockerfile
FROM centos:7
ADD nginx-1.23.3.tar.gz /mnt
WORKDIR /mnt/nginx-1.23.3
RUN yum install -y gcc make pcre-devel openssl-devel
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --with-http_ssl_module --with-http_stub_status_module
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
//FROM centos:7:基础映像是CentOS 7。
ADD nginx-1.23.3.tar.gz /mnt:将Nginx源代码文件添加到容器的/mnt目录中。
WORKDIR /mnt/nginx-1.23.3:设置工作目录为/mnt/nginx-1.23.3。
RUN yum install -y gcc make pcre-devel openssl-devel:使用yum命令安装所需的软件包。
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc:将CFLAGS选项注释掉,以便在编译期间关闭调试模式。
RUN ./configure --with-http_ssl_module --with-http_stub_status_module:运行配置脚本以启用HTTP SSL和HTTP存根状态模块。
RUN make:编译Nginx。
RUN make install:将编译后的Nginx安装到/usr/local/nginx目录中。
EXPOSE 80:将容器的80端口公开,以便可以从主机访问Nginx服务器。
VOLUME ["/usr/local/nginx/html"]:将/usr/local/nginx/html目录设置为卷,以便将来可以将HTML内容加载到容器中。
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]:启动容器时运行该命令来启动Nginx服务器,并将其保持在前台运行。
构建镜像
[root@server1 docker]# docker build -t webserver:v1 .
[root@server1 docker]# docker images webserver
[root@server1 docker]# docker history webserver:v1
查看容器详情
[root@server1 docker]# docker inspect web1
数据卷挂载
自动分配的ip
[root@server1 docker]# vim Dockerfile
FROM centos:7 as build
ADD nginx-1.23.3.tar.gz /mnt
WORKDIR /mnt/nginx-1.23.3
RUN yum install -y gcc make pcre-devel openssl-devel && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && cd .. && rm -fr nginx-1.23.3 && yum clean all
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t webserver:v2 .
[root@server1 docker]# docker images webserver
[root@server1 docker]# vim Dockerfile
FROM centos:7 as build
ADD nginx-1.23.3.tar.gz /mnt
WORKDIR /mnt/nginx-1.23.3
RUN yum install -y gcc make pcre-devel openssl-devel && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && cd .. && rm -fr nginx-1.23.3 && yum clean all
FROM centos:7
COPY --from=build /usr/local/nginx /usr/local/nginx
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t webserver:v3 .
[root@server1 docker]# docker images webserver
导入基础镜像
[root@server1 ~]# docker load -i base-debian11.tar
[root@server1 ~]# mkdir new
[root@server1 ~]# cd new/
[root@server1 new]# vim Dockerfile
FROM nginx:1.23.3 as base
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
ARG TIME_ZONE
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libpcre* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian11
COPY --from=base /opt /
EXPOSE 80 443
ENTRYPOINT ["nginx", "-g", "daemon off;"]
[root@server1 new]# docker build -t webserver:v4 .
[root@server1 new]# docker images webserver
[root@server1 new]# docker run -d --name web1 webserver:v4
配置镜像加速器
[root@server1 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
[root@server1 ~]# systemctl restart docker
registry私有仓库
官网: Deploy a registry server | Docker Docs
拉取registry镜像
[root@server1 ~]# docker pull registry
运行registry
[root@server1 docker]# docker run -d -p 5000:5000 --restart=always --name registry registry
上传镜像
[root@server1 ~]# docker tag nginx:latest localhost:5000/nginx:latest
[root@server1 ~]# docker push localhost:5000/nginx
[root@server1 ~]# curl localhost:5000/v2/_catalog
下载镜像
[root@server1 ~]# docker pull localhost:5000/nginx
配置使用非加密端口
[root@server2 ~]# vim /etc/docker/daemon.json
{
"insecure-registries" : ["http://192.168.81.10:5000"]
}
[root@server2 docker]# systemctl restart docker
拉取镜像
[root@server2 docker]# docker pull 192.168.81.10:5000/nginx
升级软件包
[root@server1 ~]# yum install -y openssl11-1.1.1k-2.el7.x86_64.rpm openssl11-libs-1.1.1k-2.el7.x86_64.rpm
添加域名解析
[root@server1 ~]# vim /etc/hosts
192.168.81.10 server1 reg.westos.org
[root@server1 ~]# mkdir certs
[root@server1 ~]# openssl11 req -newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key -addext "subjectAltName = DNS:reg.westos.org" -x509 -days 365 -out certs/westos.org.crt
域名保持一致
[root@server1 ~]# docker run -d -p 443:443 --restart=always --name registry -v /opt/registry:/var/lib/registry -v /root/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key registry
部署客户端证书,不然会报错
[root@server1 ~]# docker tag nginx:latest reg.westos.org/nginx:latest
[root@server1 ~]# mkdir -p /etc/docker/certs.d/reg.westos.org/
[root@server1 ~]# cp /root/certs/westos.org.crt /etc/docker/certs.d/reg.westos.org/ca.crt
验证
[root@server1 ~]# docker push reg.westos.org/nginx:latest
[root@server1 reg.westos.org]# curl -k https://reg.westos.org/v2/_catalog
[root@server1 ~]# yum install -y httpd-tools
[root@server1 ~]# mkdir auth
[root@server1 ~]# htpasswd -Bc auth/htpasswd admin
第二次添加不要加“-c”选项,不然会覆盖
[root@server1 ~]# htpasswd -B auth/htpasswd shx
New password:
Re-type new password:
Adding password for user wxh
[root@server1 ~]# cat auth/htpasswd
删除registry,重建
[root@server1 ~]# docker rm -f registry
[root@server1 ~]# docker run -d -p 443:443 --restart=always --name registry -v /opt/registry:/var/lib/registry -v /root/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key -v /root/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
登录仓库,不然无法上传下载
[root@server1 ~]# docker login reg.westos.org
[root@server1 ~]# cat .docker/config.json
验证
[root@server1 ~]# docker tag busybox:latest reg.westos.org/busybox:latest
[root@server1 ~]# docker push reg.westos.org/busybox:latest
[root@server1 ~]# curl -k https://reg.westos.org/v2/_catalog -u admin:shg12345
登出
[root@server1 ~]# docker logout reg.westos.org
server2上移除非安全仓库设置
[root@server2 docker]# mv daemon.json /mnt/
[root@server2 docker]# systemctl restart docker
拷贝证书
[root@server1 ~]# cd /etc/docker/
[root@server1 ~]# scp -r certs.d/ server2:/etc/docker/
登录远程仓库
[root@server2 docker]# docker login reg.westos.org
拉取镜像
[root@server2 docker]# docker pull reg.westos.org/busybox
[root@server1 ~]# docker rm -f registry
[root@server1 ~]# tar zxf harbor-offline-installer-v2.5.0.tgz
[root@server1 ~]# cd harbor/
[root@server1 harbor]# cp harbor.yml.tmpl harbor.yml
[root@server1 harbor]# vim harbor.yml
hostname: reg.westos.org
certificate: /data/certs/westos.org.crt
private_key: /data/certs/westos.org.key
harbor_admin_password: westos
拷贝证书
[root@server1 ~ ]# mkdir /data
[root@server1 ~ ]# cp -r certs /data
部署docker-compose
[root@server1 ~]# mv docker-compose-linux-x86_64-v2.5.0 /usr/local/bin/docker-compose
[root@server1 ~]# chmod +x /usr/local/bin/docker-compose
部署harbor
[root@server1 harbor]# ./install.sh
使用浏览器登录仓库 用户名:admin 密码是上面配置文件设置的shg12345
[root@server1 ~]# docker login reg.westos.org
[root@server1 ~]# docker tag busybox:latest reg.westos.org/library/busybox:latest
[root@server1 ~]# docker tag nginx:latest reg.westos.org/library/nginx:latest
[root@server1 ~]# docker push reg.westos.org/library/nginx:latest
[root@server1 ~]# docker push reg.westos.org/library/busybox:latest
配置默认仓库
[root@server2 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://reg.westos.org"]
}
[root@server2 ~]# systemctl restart docker
library项目中的镜像下载时可以直接写镜像名称
公共仓库支持匿名拉取
创建私有仓库
创建用户
授权维护私有仓库
私有仓库上传下载都需要认证,并且还要指定仓库域名
[root@server1 ~]# docker tag ubuntu:latest reg.westos.org/westos/ubuntu:latest
[root@server1 ~]# docker push reg.westos.org/westos/ubuntu:latest
[root@server2 ~]# docker login reg.westos.org
[root@server2 ~]# docker pull reg.westos.org/westos/ubuntu
删除harbor仓库
[root@server1 harbor]# docker-compose down