Skip to content

Instantly share code, notes, and snippets.

@cbwar
Last active September 22, 2022 08:35
Show Gist options
  • Save cbwar/29c5d2ed8f86a0ca01f011af020e2579 to your computer and use it in GitHub Desktop.
Save cbwar/29c5d2ed8f86a0ca01f011af020e2579 to your computer and use it in GitHub Desktop.
Build a docker image and push it to a local docker registry
FROM php:8.1-cli
USER root
[...]
# Add dev user
ARG user_id
ARG group_id
RUN groupadd -g ${group_id} dev
RUN useradd dev -u ${user_id} -g dev --shell /bin/bash --create-home
# Create cache dirs
RUN mkdir /composer-cache && chown dev.dev /composer-cache && \
mkdir /yarn-cache && chown dev.dev /yarn-cache
VOLUME [ "/yarn-cache" ]
VOLUME [ "/composer-cache" ]
USER dev
ENV COMPOSER_HOME=/composer-cache
RUN yarn config set cache-folder /yarn-cache
WORKDIR /home/dev
---
# file: templates/daemon.json
# {
# "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
# "insecure-registries":[ "localhost:5000" ]
# }
#
# file: templates/override.conf
# Disable flags to dockerd, all settings are done in /etc/docker/daemon.json
# [Service]
# ExecStart=
# ExecStart=/usr/bin/dockerd
#
- name: configure registry
template:
src: templates/daemon.json
dest: /etc/docker/daemon.json
mode: 0600
- file:
path: /etc/systemd/system/docker.service.d/
state: directory
- name: override docker
template:
src: templates/override.conf
dest: /etc/systemd/system/docker.service.d/override.conf
mode: 0600
- shell: systemctl daemon-reload
- shell: systemctl restart docker
# install with command: ansible-galaxy collection install community.docker
- name: start registry container
community.docker.docker_container:
container_default_behavior: no_defaults
name: my-registry
image: registry
state: started
pull: yes
restart_policy: always
ports:
- "5000:5000"
volumes:
- "/home/docker-volumes/registry:/var/lib/registry"
pipeline {
environment {
registry = 'localhost:5000/my-image-name'
dockerImage = ''
uid = sh(returnStdout: true, script: 'id -u').trim()
gid = sh(returnStdout: true, script: 'id -g').trim()
}
agent any
stages {
stage('build') {
steps {
script {
// The "dev" user inside the image is matching the host "jenkins" user
dockerImage = docker.build("$registry:$BUILD_NUMBER",
"--build-arg user_id=${uid} --build-arg group_id=${gid} .")
}
}
}
stage('push') {
steps {
script {
dockerImage.push()
dockerImage.push('latest')
}
}
}
}
}
pipeline {
agent {
docker {
image 'localhost:5000/my-image-name:latest'
reuseNode true
// Shared resources with jenkins host
args '-v $HOME/.ssh:/home/dev/.ssh -v $HOME/composer-cache:/composer-cache -v $HOME/yarn-cache:/yarn-cache'
}
}
stages {
[...]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment