I deploy my project into docker swarm using Gitlab pipeline. I have a build step and deploy step. If it is helpful for you, I’ve pasted bellow my gitlab.yml and my dockerfile
stages:
- test
- build
- deploy
variables:
IMAGE_NAME: nexus.NAME-OF-MY-COMPANY.net:8082/content-proxy
.runner-tags: &runner-tags
tags:
- deploy
build:
<<: *runner-tags
image: docker:stable
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: "tcp://docker:2375/"
services:
- $DOCKER_DIND
before_script:
- docker login -u $NEXUS_USER -p $NEXUS_PASSWORD nexus.NAME-OF-MY-COMPANY.net:8082
stage: build
script:
- docker build -t ${IMAGE_NAME}:${CI_COMMIT_REF_NAME} -f Dockerfile-prod .
- docker push ${IMAGE_NAME}:${CI_COMMIT_REF_NAME}
only:
- tags
- master
test:
<<: *runner-tags
stage: test
image: elixir:1.7-alpine
before_script:
- mix local.hex --force
- mix local.rebar --force
- mix deps.get --only test
services:
- name: mongo:latest
alias: mongodb
script: mix test
only:
- merge_request
- master
.deploy-common: &deploy-common
<<: *runner-tags
stage: deploy
image: nexus.NAME-OF-MY-COMPANY.net:8082/ubuntu-openssh
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
deploy_staging:
<<: *deploy-common
script:
- ssh $SWARM_MANAGER_DEV docker stack deploy -c services/docker-compose.yml $DOCKER_SWARM_GROUP_NAME --with-registry-auth
only:
- master
environment:
name: staging
url: https://host-of-my-project-for-staging/
deploy_prod:
<<: *deploy-common
script:
- ssh $SWARM_MANAGER_PROD docker stack deploy -c services/docker-compose.yml $DOCKER_SWARM_GROUP_NAME --with-registry-auth
only:
- tags
environment:
name: prod
url: https://host-of-my-project-for-production
And my dockerfile
#===========
#Build Stage
#===========
FROM bitwalker/alpine-elixir:1.7.4 as build
ENV MIX_ENV="prod" \
APP_NAME="content_proxy"
#Copy the source folder into the Docker image
COPY . .
RUN mix deps.get && \
mix deps.compile && \
mix release && \
#Extract Release archive to /rel for copying in next stage
RELEASE_DIR=`ls -d _build/prod/rel/${APP_NAME}/releases/*/` && \
mkdir /export && \
tar -xf "${RELEASE_DIR}/${APP_NAME}.tar.gz" -C /export
#================
#Deployment Stage
#================
FROM alpine:3.8
#Set environment variables and expose port
ENV REPLACE_OS_VARS="true" \
LANG="en_US.UTF-8" \
HOME="/opt/app" \
TERM="xterm" \
DEPS="ncurses-libs zlib openssl bash"
EXPOSE 4000
#Container default workdir
WORKDIR ${HOME}
#Install Dependencies for Erlang and Distillery
RUN apk --update add --no-cache --upgrade \
${DEPS} && \
adduser -s /bin/sh -u 1001 -G root -h ${HOME} -S -D default && \
chown -R 1001:0 ${HOME} && \
rm -rf /var/cache/apk/*
#Copy and extract .tar.gz Release file from the previous stage
COPY --from=build /export/ .
#Change user
USER default
#Set default entrypoint and command
ENTRYPOINT ["/opt/app/bin/content_proxy"]
CMD ["foreground"]