jenkins sh构建运行shell脚本 docker: command not found
The simple way to run Docker-in-Docker for CI:https://tutorials.releaseworksacademy.com/learn/the-simple-way-to-run-docker-in-docker-for-ci
需要将宿主机的docker环境变量映射
-v /var/run/docker.sock:/var/run/docker.sock \
参数多了使用docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
docker-compose -v
cd /home
vi docker-compose.yml
version: '3' services: jeknins: container_name: 'jenkins_pen' image: jenkins/jenkins:lts restart: always user: root ports: - "11005:8080" - "5000:5000" volumes: - /home/jenkins/data:/var/jenkins_home - /usr/bin/docker:/usr/bin/docker - /var/run/docker.sock:/var/run/docker.sock
//若docker-compose.yml user 权限不够会有报错
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
https://www.cnblogs.com/Uni-Hoang/p/12901055.html
https://www.cnblogs.com/Uni-Hoang/p/12901055.html
若使用docker-compose.yml也要注意
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
//执行docker-compose.yml配置文件
# 删除上次构建的容器
docker-compose down
# 重新构建镜像 --force-rm 删除构建过程中的临时容器。
docker-compose build --force-rm
# 运行容器
docker-compose up -d
# docker-compose.yml version: "3" services: # 服务 node: # node 服务 build: . # Dockerfile 的目录,用于构建镜像 container_name: node-server-1 # 容器名称 ports: # 暴露的端口 - "3000:3000" restart: always # 自动重启 environment: - NODE_ENV=production networks: # 加入网络 - "my-network" command: npm run start # 覆盖容器启动后默认执行的命令 dockerfile定义了workdir nginx: image: nginx:latest 指定 nginx 镜像 ports: # 将本机的 8080 端口映射到容器的80端口 - "8080:80" container_name: nginx-node restart: always volumes: # 映射本机 F:/nginx.conf 文件到 容器的 /etc/nginx/nginx.conf:ro 文件 - "F:/nginx.conf:/etc/nginx/nginx.conf:ro" networks: - "my-network" links: # 设置 node 服务别名,其实是设置/etc/hosts的域名解析 - "node" depends_on: # 指定依赖于哪个服务 - node networks: # 网络 my-network: # 网络名称 driver: bridge
2.docker容器的内数据备份
//docker cp docker cp containerId:/var/jenkins_home /home/jenkins/data
#1.从宿主机向容器中复制文件
[root@localhost data]# sudo docker cp aa.txt ssh001:/data
把当前目录下的aa.txt复制到容器名称为【ssh001】的data目录下
//官方备份volumes https://docs.docker.com/storage/volumes/#backup-restore-or-migrate-data-volumes
docker run --rm --volumes-from jenkins_pen -v /temp/backup:/backup ubuntu tar cvf /backup/backup.tar /var/jenkins_home