I've created a gitlab-ci.yml pipeline to deploy a simple react.js application. I installed the gitlab-runner on EC2 (AWS) and inside my pipeline I build the image and push it to the registry.
This is my gitlab-ci.yml file:
image: docker:latest
services:
- docker:dind
stages:
- test
- deploy
test-build:
stage: test
only:
- master
tags:
- master
script:
- sudo docker build .
deploy-production:
stage: deploy
only:
- master
tags:
- master
before_script:
# remove the offending package golang-docker-credential-helpers without removing all of docker-compose
- sudo dpkg -r --ignore-depends=golang-docker-credential-helpers golang-docker-credential-helpers
- sudo docker version
- "sudo docker info"
- "sudo docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
script:
- "sudo docker build -t ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest --pull ."
- "sudo docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest"
- "sudo docker run -it -d -p 80:80 --rm --name daruma.comunicato ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest"
after_script:
- "sudo docker logout ${CI_REGISTRY}"
when: manual
The problem is: how do I run the latest pushed image?
If I run docker run -it ...
the pipeline fails with:
docker: Error response from daemon: Conflict. The container name "/app.test" is already in use by container "f8e888d6dff6fe5808d7577fe8fddc3e24cd8cc0e248a69d36e7ce51bf339ae6". You have to remove (or rename) that container to be able to reuse that name.
Because that container is already running and has the same name.
I don't want to stop all containers when deploying because I could have other docker containers running. I also thought to do docker stop app.test && docker rm app.test
but if for some reason the container is down that will give me an error and won't deploy.
What is the correct way to handle this situation?