Quick-start local docker registry
In this quick guide, I’ll walk you through setting up your own containerized Docker registry locally, ensuring persistent storage is kept separate for a decoupled system to experiment with Docker registry without data loss.
Design
Pre-requisite
- xcode — command line tools
- Homebrew
- Docker Desktop
Install Home-brew
# install homebrew through commandline
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update
Install Docker Desktop
#install docker desktop through commandline
brew install --cask docker
Storage Container
First, let’s create a storage container for the Docker registry. You can also use a local directory to be mounted.
# filename: Dockerfile
# Use a base image (e.g., Ubuntu) for creating the storage container
FROM ubuntu:latest
# Create a directory to store Jenkins workspace and other files
RUN mkdir -p /var/docker
# Set the directory as a volume to persist data
VOLUME /var/docker
# open access
RUN chmod -R 777 /var/docker
# Create a dummy file and tail it to keep the container running indefinitely
CMD touch /var/docker/keep_running && tail -f /var/docker/keep_running
# filename: run.sh
# Run this file to recreate container
# Note!! you will loose anything created by registry earlier
docker rm -f docker-registry-storage
docker rmi -f docker-registry-storage
docker build -t docker-registry-storage .
#start the storage container
docker run -d --name docker-registry-storage docker-registry-storage
Docker Registry Container
Now, let’s create a separate container for the Docker registry and mount the storage created earlier for usage.
# filename: run.sh
# delete the existing container
docker rm -f registry-configured
#run new container
docker run -d -p 5000:5000 \
--restart=always \
--name registry-configured\
--volumes-from docker-registry-storage
-v docker-registry-storage:/var/lib/registry
registry:latest
# if you want to use your current directory as storage directory instead of storage container
# use this command for running local docker registry instead of above
# docker run -d -p 5000:5000 --restart=always --name registry -v $(pwd)/docker-registry:/var/lib/registry registry:latest
Usage
Target project
Create your own project or use a sample Node.js project for testing the registry setup.
mkdir web-service
cd web-service
#create initial project
#for project with default values. keep tapping enter till the command ends
npm init
# create a sample project with express
npx express-generator --no-view src
cd src
npm install
npm audit fix --force
# run the below command goto localhost:3000 to test successful run
DEBUG=src:* npm start
create a docker file
# under web-service
# filename: Dockerfile
FROM node:18.16.0-alpine3.17
WORKDIR /app
COPY src/ .
RUN npm install
EXPOSE 3000
CMD [ "npm", "start"]
Create and Push a New Build
#command format
#docker build -t <registry address>/<appname>:<version> .
docker build -t localhost:5000/web-service:1.0.0 .
#command format
#docker push <registry address>/<appname>:<version>
docker push localhost:5000/web-service:1.0.0
Test Successful Push
for testing we will delete the build locally and pull it from the registry
docker rmi localhost:5000/web-service:1.0.0
check docker desktop, the image would be deleted.
Pull fresh image from your registry.
docker pull localhost:5000/web-service:1.0.0
Now you have successfully created your own containerised docker registry.
Run Your Service
docker run -d -p 3000:3000 --name web-service localhost:5000/web-service:1.0.0
Now, visit http://localhost:3000 on your browser to see your containerized app running. This guide provides a practical way to get started with your own containerized Docker registry. Happy learning!