Can I build a Docker and send request on CircleCI? (I don't want to push my custom Docker image to any cloud platform) - continuous-integration

I am trying to build my API and testing that API on CircleCI. This is my config file of CircleCI
version: 2.1
name: CI
on:
- push
jobs:
test:
docker:
- image: cimg/base:stable
steps:
- setup_remote_docker:
version: 20.10.14
- checkout
- run:
name: "Prepare API"
command: bash prepare_api.sh
- run:
name: "Build Docker"
command: docker build -t cf_api:0.0.1 .
- run:
name: "Run Docker"
command: docker run -dp 1234:1234 cf_api:0.0.1
- run:
name: "Send request to API"
command: bash send_request.sh
workflows:
default:
jobs:
- test
send_request.sh contain only a curl command like below;
curl -X 'POST' \
'http://localhost:1234/api/collaborativefilter' \
-H 'accept: application/json' \
-H 'token: serdarakyol55#outlook.com' \
-H 'Content-Type: application/json' \
-d '{
"item": [
"SGKZB70023"
],
"n_item": 3
}'
When CI comes to Send request to API step, CI throw curl: (7) Failed to connect to localhost port 1234: Connection refused error. Can someone help with this, please?

Related

docker-compose deployment configuration for Circle CI

I am using Circle CI to deploy a microservice to a Digital Ocean droplet and had a few questions about whether my approach is the right one.
My microservice is built using docker-compose, and therefore requires a docker-compose.yml file to pull, start the images that constitute it.
In a nutshell, my deployment approach would be:
Merge branch to master will kick off a CircleCI build
CircleCI will run unit tests
Upon all tests passing, docker-compose build and docker-compose push to Docker Hub
Stop all running images of that service on remote server.
Remove dangling images, and local networks.
Download the relevant docker-compose.yml, Dockerfile and docker-compose.env files.
Pull using docker-compose pull
Start images using docker-compose up
I am using this configuration in CircleCI:
version: 2.1
jobs:
build:
docker:
- image: "circleci/node:10.16.0"
steps:
- checkout
- run:
name: Update to latest npm version
command: "sudo npm install -g npm#latest"
- restore_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Install dependencies
command: npm install
- run:
name: Install `docker-compose`
command: |
curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
chmod +x ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
- setup_remote_docker:
docker_layer_caching: false
- run:
name: Build using `docker-compose`
command: |
docker-compose build
- run:
name: Login for Docker Hub
command: |
echo "$DOCKER_PASSWORD" | docker login --username $DOCKER_USERNAME --password-stdin
- run:
name: Push to Docker Hub
command: |
docker-compose push
- run: ssh-keyscan $DIGITALOCEAN_HOST >> ~/.ssh/known_hosts
- add_ssh_keys:
fingerprints:
- fo:of:fe:ef:af
- run:
name: Remove currently running containers
command: |
ssh root#$DIGITALOCEAN_HOST ./deploy_image.sh
I am planning on creating a bash script to handle steps 4 to 8 from my list above.
Is it a good idea to have a script take care of the Docker steps?
Or is there a better way to have a more "native" CircleCI configuration?

Gitlab Elasticsearch Service not connecting during pipeline run

We have managed to get both Mongo and PostgreSql working fine using Gitab service however we are facing real issues with elasticsearch.
Whenever we try to run the pipeline the connection to elastic fails.
I have tried the following steps in this thread:
https://gitlab.com/gitlab-org/gitlab-ce/issues/42214
But still no luck.
i.e. both
image: maven:latest
test:
stage: test
services:
- name: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
alias: elasticsearch
command: [ "bin/elasticsearch", "-Ediscovery.type=single-node" ]
stage: test
script:
- ps aux
- ss -plantu
- curl -v "http://elasticsearch:9200/_settings?pretty"
and:
image: maven:latest
test:
stage: test
services:
- elasticsearch:6.5.4
script:
- curl -v "http://127.0.0.1:9200/"
Result in connection errors.
Has anyone got this working for elasticsearch:6.5.4?
This was fixed by a 15 second sleep line.
ci file now looks like:
test:
stage: test
services:
- name: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
command: ["bin/elasticsearch", "-Expack.security.enabled=false", "-Ediscovery.type=single-node"]
script:
- echo "Sleeping for 15 seconds.."; sleep 15;

GitLab CI/CD: Do not destroy a docker container after building

My use case is the following: I want to deploy a PHP app to docker after a branch is updated and then leave it running for manual testing.
So I would like to access it under a certain URL.
What I did so far is to create a gitlab docker runner
gitlab-ci-multi-runner register --url "https://git.example.com/ci" --registration-token xxxx \
--description "dockertest" \
--executor docker \
--docker-image "php:7.0-apache" \
--docker-services mariadb:latest
And I have a .gitlabci.yml
stages:
- deploy
job-deploy-docker:
only:
- docker-ci-test
stage: deploy
environment: docker
tags:
- docker
image: php:7.0-apache
services:
- mariadb
script:
- apt-get update && apt-get --assume-yes install mysql-client
- mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mariadb -e "create database ci"
The deploy runs Job succeeded and then the container is destroyed.
How can I avoid destroying the container. Of course it should be cleaned up or reused if a new push is done to a branch.
How can I make the web app accessible under a certain external URL (branchname.ci.example.com)
Is that even the right approach or should I do it differently?

Set up mapping in Elasticsearch during Docker run

I am going to use official Elasticsearch image. What I want to do is set up the mapping of indexes during Docker run.
So I need to execute right after the container is started.
curl -XPUT localhost:9200/_template/http_request -d {.....}
So I can not do it in Dockerfile or can I?
Thank you
I ended up doing this
docker-composer.yml
version: '2'
services:
elasticsearch:
image: elasticsearch:2.3
command: elasticsearch -Des.network.host=0.0.0.0
ports:
- "9200:9200"
- "9300:9300"
elasticsearch-mapping-init:
build: elasticsearch-mapping-init
links:
- elasticsearch
depends_on:
- elasticsearch
and here is my elasticsearch-mapping-init/Dockerfile:
FROM ubuntu
# Install packages
RUN apt-get update && \
apt-get install -y curl
COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
and here is my elasticsearch-mapping-init/docker-entrypoint.sh
#!/bin/bash
for i in {30..0}; do
if curl elasticsearch:9200; then
curl -XPUT elasticsearch:9200/_template/log -d '
{
"template" : "log-*",
"settings": {
"number_of_shards": 1
},
"mappings" : {
}
}';
break;
fi
sleep 2
done
I believe this is not perfect, and I'm still looking for a better solution.

How to set environment variable as docker image name on build

When using the file circle.yml file to build a docker image I'm trying to pass an environment variable $CIRCLE_PROJECT_USERNAME so that the file is less project specific. Unfortunately Docker build with the tag argument -t fails with:
docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH . returned exit code 1
repository name component must match "[a-z0-9]+(?:[._-][a-z0-9]+)*" Action failed: docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH .
Circle.yml file:
machine:
environment:
services:
- docker
dependencies:
override:
- docker info
- docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH .
test:
override:
- docker run -d $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH
deployment:
hub:
branch: master
commands:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASSWORD
- docker push $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH
deployment:
hub:
branch: develop
commands:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASSWORD
- docker push $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH
Should work, check if the env are actually set and only contain allowed characters:
[a-z0-9]+(?:[._-][a-z0-9]+)*
Edit:
docker build -t CIRCLE_PROJECT_USERNAME ...
Maybe just missing a $ here? ;)

Resources