How to specify Docker private registry credentials in Docker configuration File? - node.js

I am creating Docker container of nodejs application. Below is the sample of my Docker configuration file
FROM node:6.11
WORKDIR /usr/src/app
COPY package.json .
npm install
copy . /usr/src/app
EXPOSE 80
CMD [ "npm", "start" ]
This will download the node image from Docker hub and then it will create Docker image as per the configuration.
For security reasons I don't want to download nodejs image from Docker hub, Instead I want to use my private repository to download nodejs image.
As I have setup private repository I am not sure how to specify registry credentials in DockerFile.
Can anyone help me with this?

By default, docker pulls all images from Dockerhub. If you want to pull an image from another registry, you have to prefix the image name with the registry URL. Check the official docker pull documentation.
In your case, you have 2 options:
The first is to specify explicitly the registry inside the Dockerfile as such:
FROM <registry>:<port>/node:6.11
WORKDIR /usr/src/app
Once you build, the image will be downloaded from the private registry. Make sure that you are logged in to the registry before building using the docker login command.
Alternatively, if you don't want to change the docker file. Pull the image from the private registry using docker pull <registry>:<port>/node:6.11 and then force docker build to use this image by tagging it with only node:6.11
docker tag <registry>:<port>/node:6.11 node:6.11

Before you build the Docker image you'll have to do docker login to your private repo. Then pulls - explicit or implicit through FROM will use that registry (and while I can't find any documentation to back that up, I suspect it also fallback to Docker Hub if it can't find the image there, but that may be dependent on the registry settings????)

I guess you already have nodejs image in your local docker registry.
If you want to pull the nodejs image from local docker registry:
Make sure your docker daemon is pointing to local docker registry use --insecure-registry <registry_address>:<port> as mentioned here https://docs.docker.com/engine/reference/commandline/dockerd/
Change Dockerfile to point to image in registry. FROM <registry_address>:<port>/node:6.11 (actually this will be the complete name of your nodejs image in local docker registry)
The registry credentials can be set using docker login command https://docs.docker.com/engine/reference/commandline/login/ or you can manually set the credentials in ~/.docker/config.json file.
Now you can build the image, it should pull the base image from registry.

Related

Cannot get Docker to use pull-through cache

I am trying to cache images locally in a Docker registry using a pull through cache as described here: https://docs.docker.com/registry/recipes/mirror/. But when I do, Docker seems to ignore the pass-through cache and is instead pulling images directly from Docker Hub.
I have a Docker Compose file that I am using to run the registry:
version: '3.9'
services:
registry:
restart: always
image: registry:2
ports:
- 5000:5000
volumes:
- ~/.docker/registry:/var/lib/registry
- ./registry-config.yml:/etc/docker/registry/config.yml
And I have configured Docker to use the registry via the --registry-mirror option, which I have confirmed is taking effect by checking the output of docker info:
$ docker info
...
Registry Mirrors:
https://localhost:5000/
...
But when I try to pull an image, I see no activity in the registry's logs, as if Docker is ignoring the mirror option and just going straight to Docker Hub.
I have also confirmed the registry is working by pulling alpine:latest from Docker Hub, tagging it as localhost:5000/alpine:latest and pushing it to the registry, then pulling it back out again. Both of which work fine.
This is all being done on a Linux VM running Ubuntu.
Can someone please help me understand what I am doing wrong. and how I am supposed to get Docker to pull all images through the pull-through cache?
Thanks in advance for your help.

Deploy reactjs application on the container instance

I am very new to this Containers, so I am unable to deploy the application on the Container instance .
I tried below steps but i could not resolve the issue. Please help me out of this. Thanks in advance
steps:
1.I have created the Reactjs build.
2.Created the docker image and Create the container registry and pushed it into docker container instance.
my Docker file is :
FROM nginx: version
copy /build/user/share/nginx/html
I have created the docker image and build that image and successfully created the docker image
I have created the container rgistry and when I trying to push the docker image to container instance after that i am not able to access the application using web.
docker build -t image_name
Can anyone help me that how to access the application through UI
Thanks in Advance!
I tried to reproduce the same issue in my environment and got the below results
I have created and build the npm using below command
npm run build
I have created example docker file and run the file
#docker build -t image_name
FROM node:16.13.1-alpine as build
WORKDIR /app
ENV PATH /app/node/_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install react-scripts -g --silent
COPY . ./
RUN npm run build
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx","-g","daemon off"]
I have created the container registry
Enabled the access to to push the image to container registry
I have logged into the container registry using login server credentials
docker login server_name
username:XXXX
password:XXXXX
I have tagged and pushed the image into container registry
docker tag image_name login-server/image
docker push login-server/image_name
I have created the container instances to deploy the container image
While creating in Networking i have given the DNS label and created
By using FQDN link I can able to access the image through UI

Access private git repos via npm install in a Docker container

I am in the process of setting up a Docker container that will pull private repos from GitHub as part of the process. At the moment I am using an Access Token that I pass from the command line (will change once build gets triggered via Jenkins).
docker build -t my-container --build-arg GITHUB_API_TOKEN=123456 .
# Dockerfile
# Env Vars
ARG GITHUB_API_TOKEN
ENV GITHUB_API_TOKEN=${GITHUB_API_TOKEN}
RUN git clone https://${GITHUB_API_TOKEN}#github.com/org/my-repo
This works fine and seems to be a secure way of doing this? (though need to check the var GITHUB_API_TOKEN only being available at build time)
I am looking to find out how people deal with ssh keys or access tokens when running npm install and dependencies pull from github
"devDependencies": {
"my-repo": "git#github.com:org/my-repo.git",
"electron": "^1.7.4"
}
At the moment I cannot pull this repo as I get the error Please make sure you have the correct access rights as I have no ssh keys setup in this container
Use the multi-stage build approach.
Your Dockerfile should look something like this:
FROM alpine/git as base_clone
ARG GITHUB_API_TOKEN
WORKDIR /opt
RUN git clone https://${GITHUB_API_TOKEN}#github.com/org/my-repo
FROM <whatever>
COPY --from=base_clone /opt/my-repo /opt
...
...
...
Build:
docker build -t my-container --build-arg GITHUB_API_TOKEN=123456 .
The Github API Token secret won't be present in the final image.
docker secrets is a thing, but it's only available to containers that are part of a docker swarm. It is meant for handling things like SSH keys. You could do as the documentation suggests and create a swarm of 1 to utilize this feature.
docker-compose also supports secrets, though I haven't used them with compose.

How to launch a Docker container that i've got from another person?

I am a Docker-newbie and I've got a project from another developer including a Dockerfile. This shall give me the Virtual Machine to continue work with the (nodeJS-) project inside this project folder.
Docker is already installed on my machine.
How can I launch this container now?
I've read about a command
sudo docker run -name my_first_instance
but i can't find any container name in the Dockerfile.
The dockerfile will create an image for you that you can launch containers from. this being said , Follow this:
Create a folder.
Copy dockerfile in the folder
cd into the folder execute the following command:
docker build -t <your desired image name> .
This will create an image using directives in the dockerfile in the current folder.
Now launch a container from the image.
docker run -d --name <your container name> <imagename from previous step> <optional startup commands>
Useful docker commands:
You can expose ports in the previous command using -p switch.
You can list Images via docker images
You can list running containers via docker ps
you can list running + exited containers via docker ps -a
have a look at
https://hub.docker.com/_/node/
it is the official repository for NodeJS docker images
If you want some docker images based on NodeJS, you will need to pull them docker pull my_node_image
Then you can launch one with such a command
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/app -w /usr/src/app node:4 node your-daemon-or-script.js
The Dockerfile is just the recipe to build a docker image, for Nginx, Mysql, MongoDb, Redis, Wordpress, Spotify, atop, htop...
If docker images shows nothing, it means you have not yet pulled any docker image.

Getting docker pull to default pull from a private registry?

So when you docker pull an image, it by default looks to the Docker Hub registry to find the image. Is there any way that I can make it so when I docker pull, it by default looks into my private registry?

Resources