Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止和rm旧docker容器并使用Gitlab CI启动新容器_Docker_Ssh_Gitlab_Gitlab Ci - Fatal编程技术网

停止和rm旧docker容器并使用Gitlab CI启动新容器

停止和rm旧docker容器并使用Gitlab CI启动新容器,docker,ssh,gitlab,gitlab-ci,Docker,Ssh,Gitlab,Gitlab Ci,嗨,我在Spring Boot应用程序上使用GitLab CI 这就是我想做的: -执行我的测试 -构建应用程序 -将其固定并移除旧容器 这是我的.gitlab ci.yml: image: maven:latest services: - docker:dind cache: paths: - .m2/repository variables: MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" DOCKER_HOST:

嗨,我在Spring Boot应用程序上使用GitLab CI

这就是我想做的: -执行我的测试 -构建应用程序 -将其固定并移除旧容器

这是我的
.gitlab ci.yml

image: maven:latest

services:
  - docker:dind

cache:
  paths:
    - .m2/repository

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  DOCKER_HOST: tcp://docker:2375
  IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  IMAGE_TAG: ${CI_COMMIT_REF_SLUG}

stages:
  - test
  - build
  - release
  - deploy

test:
  stage: test
  script:
    - mvn test

project-build:
  stage: build
  script:
    - mvn clean package

release:
  image: docker:git
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay
  stage: release
  before_script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
  script:
    - docker build --tag=$IMAGE_TAG . --pull -t $IMAGE_NAME
    - docker push $IMAGE_NAME
  only:
    - master

deploy-staging:
  stage: deploy
  image: gitlab/dind:latest
  cache: {}
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay
  before_script:
    # add the server as a known host
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan -H $DEPLOYMENT_SERVER_IP >> ~/.ssh/known_hosts
  script:
    - ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
    # stop container, remove image.
    - ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker stop ${IMAGE_TAG}" || true
    - ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker rm ${IMAGE_TAG}" || true
    - ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker rmi -f ${IMAGE_NAME}" || true
    # start new container
    - ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker run --publish=8080:8080 -d ${IMAGE_NAME}"
  only:
    - master

但是docker stop和docker rm/rmi线路不工作。以下是堆栈跟踪:

108 $ ssh-keyscan -H $DEPLOYMENT_SERVER_IP >> ~/.ssh/known_hosts
109 # MY_SERVER_IP SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
110 # MY_SERVER_IP SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
111 $ ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
112 WARNING! Using --password via the CLI is insecure. Use --password-stdin.
113 WARNING! Your password will be stored unencrypted in /home/deployer/.docker/config.json.
114 Configure a credential helper to remove this warning. See
115 https://docs.docker.com/engine/reference/commandline/login/#credentials-store
116 Login Succeeded
117 $ ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker rm -f ${IMAGE_NAME} 2>/dev/null || exit 0"
118 $ ssh $DEPLOYER_USER@$DEPLOYMENT_SERVER_IP "docker run --publish=8080:8080 -d ${IMAGE_NAME}"
119 0ff2aeeb0bf19b3c528dacaf2f8e6022587c1f3b4d845c1c583731eb76c1b65b
120 docker: Error response from daemon: driver failed programming external connectivity on endpoint pedantic_lamarr (96dd544514d257320160c15adb1b4c0f4a91c7423e20643ea901774d1528c4f1): Bind for 0.0.0.0:8080 failed: port is already allocated.
122
ln: failed to create symbolic link '/sys/fs/cgroup/systemd/name=systemd': Operation not permitted
总而言之:前3个作业可以工作,但是如果我的服务器中运行了一个容器,那么最后一个作业会失败,因为我无法停止并删除旧的容器

有什么想法吗