docker、dockerfile、docker swarm、docker service使用备注
镜像构建
#基础镜像 FROM centos:centos7 #容器运行时用户 USER 0:0 #工作目录 WORKDIR /root/ #运行指令,可以设置多个 RUN echo "hello world" #将本地资源、网络资源复制到镜像中,也可以使用COPY命令 ADD data . #CMD表示容器启动时,将执行的命令,会被docker run 所带的指令所覆盖 CMD ping www.baidu.com #ENTRYPOINT也表示容器启动将执行的命令,CMD指令至会作为其参数 ENTRYPOINT echo "hello entrypoint"
镜像发布
#登录 docker login --username=xx 仓库地址 #编译 #-t 镜像仓库地址:版本号 #-f 镜像构建文件 docker build -t imageName -f dockerfile.yml #上传镜像文件 docker push imageName
dockerfile.yml文件示例
#jenkins镜像构建 #2023-04-11 FROM centos:centos7.9.2009 USER 0:0 WORKDIR /root/ RUN ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && yum clean all && yum makecache \ && yum install -y vim wget tree iproute net-tools tcpdump sysstat psmisc \ lsof pv htop atop dstat nethogs nload iftop iotop mtr nc stress fio iperf \ bc zip unzip telnet screen tmux sshpass mlocate lrzsz lftp ntpdate figlet \ dialog autoconf automake gcc gcc-c++ subversion git lvm2 bash-completion \ createrepo mariadb iproute net-tools bind-utils dos2unix pigz jq python-netaddr expect \ && yum clean all && rm -rf /var/cache/yum/* #安装jdk1.8 RUN yum -y install java-1.8.0-openjdk.x86_64
#下载对应1.8版本的jenkins,此版本许多插件装不上,不再建议使用,建议使用jdk11或jdk17以上的jenkins版本 RUN wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.346.1-1.1.noarch.rpm --no-check-certificate #ADD jenkins-2.346.1-1.1.noarch.rpm ./ RUN rpm -ivh jenkins-2.346.1-1.1.noarch.rpm RUN sed -i 's#User=jenkins#User=root#g' /usr/lib/systemd/system/jenkins.service RUN sed -i 's#Group=jenkins#Group=root#g' /usr/lib/systemd/system/jenkins.service RUN chown -R root:root /var/lib/jenkins RUN chown -R root:root /var/cache/jenkins RUN chown -R root:root /var/log/jenkins CMD bash
docker swarm使用
#初始化swarm docker swarm init #创建服务 #-c 构建服务的脚本文件 #stackName工作空间的名字 #--with-registry-auth,拉取最新镜像启动服务 docker stack deploy -c service.yml stackName --with-registry-auth #工作空间查看 docker stack ls #服务查看 docker service ls #服务更新 docker service update --image imageName service.yml --with-registry-auth
docker swarm service脚本示例
version: "3.7" #============================ # Logging config section x-logging: &default-logging options: max-size: '12m' max-file: '10' driver: json-file #============================ # Network config section networks: dockernet: driver: overlay attachable: true hostnet: external: true name: host configs: timezone: file: /usr/share/zoneinfo/Asia/Shanghai services: jenkins: image: jenkinsci/blueocean logging: *default-logging deploy: mode: replicated replicas: 1 restart_policy: condition: on-failure networks: - dockernet ports: - "9081:8080" configs: - source: timezone target: /etc/localtime mode: 0444 volumes: - type: bind source: /data/docker-volume/jenkins/data target: /var/jenkins_home read_only: false - type: bind source: /data/docker-volume/jenkins/docker.sock target: /var/run/docker.sock read_only: false