Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Post: "Deploying Docker containers to systemd with GitLab CI" #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
layout: post
title: "Deploying Docker containers to systemd with GitLab CI"
date: 2016-10-23 12:00:00 -0400
tags: ["GitLab", "CI", "Docker", "systemd"]
---
```ini [email protected]
# unit to run any arbitrary Docker container
[Unit]
Description=%i
After=docker.service
Requires=docker.service

[Install]
WantedBy=multi-user.target

[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/docker start -a %i
ExecStop=/usr/bin/docker stop %i
```

```bash deploy-image.sh
#!/bin/bash
set -e
set -u

DOCKER_IMAGE="$CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME"
DOCKER_CONTAINER=$(echo "$CI_PROJECT_NAME" | tr '[:upper:]' '[:lower:]')".$CI_BUILD_REF_NAME"

if docker inspect "$DOCKER_CONTAINER" 2>&1 > /dev/null; then
if docker inspect "$DOCKER_CONTAINER.backup" 2>&1 > /dev/null; then
echo "Deleting previous backup $DOCKER_CONTAINER.backup"
docker rm "$DOCKER_CONTAINER.backup" > /dev/null
fi
echo "Backing up container $DOCKER_CONTAINER"
sudo systemctl stop "dockercontainer@$DOCKER_CONTAINER"
docker rename "$DOCKER_CONTAINER" "$DOCKER_CONTAINER.backup"
fi

docker pull "$DOCKER_IMAGE"
/usr/bin/docker create --name "$DOCKER_CONTAINER" "$DOCKER_IMAGE"

sudo systemctl start "dockercontainer@$DOCKER_CONTAINER"
sudo systemctl enable "dockercontainer@$DOCKER_CONTAINER"
```

```yaml .gitlab-ci.yml
stages:
- publish
- deploy

publish:
stage: publish
tags: [docker]
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker build --pull -t $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME .
- docker push $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME

deploy:
stage: deploy
environment: Live
tags: [ssh-executor]
variables: {GIT_STRATEGY: none}
when: manual
only: [master]
script:
- ~/Infrastructure-CoreOS/deploy-image.sh
```