How to specify Docker private registry credentials in Docker configuration File?

Viewed 5332

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?

3 Answers

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????)

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

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