Pre-requisites: Docker
Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing container images. Just like GitHub allows the hosting of code of our application, DockerHub allows the hosting of Images of our applications.
Docker Image
In order to run these applications, we first need to create an image of the current application state. An image can be sometimes referred to as a snapshot of our project. Images are read-only in nature and consist of file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run.
A Docker image is a read-only template that contains a set of instructions for creating a container which can run on the Docker platform.
Docker Registry
A Docker registry is a service that stores and manages Docker images. Docker registry could be hosted by a third party, as a public or private registry. Some examples of Docker registries are as follows:
- Docker Hub
- GitLab
- AWS Container Registry
- Google Container Registry
- Docker – Private Registries
Docker Repository
A Docker repository is a collection of different Docker images with the same name, that have different tags. Tags basically are identifiers of the image within a repository.
In this tutorial, we will use Docker Hub to host our repositories, which is free for public use.
Steps
Step 1. Creating An Account On Docker Hub. Go to DockerHub and create a new account or log in to your existing account.
Step 2. Creating A Repository (optional)
On the docker hub, you can create a repository by clicking on create repository button. Give the repository a name and description and make sure it is marked as public. This step is not necessary when you are hosting a public repository. It is used while hosting a private one.
Repository Name image widget.
Step 3. Build a Docker Image
Now we will generate a basic express application and create an Image out of it.
$ mkdir express-app && cd express-app $ npx express-generator -e
Now, create a Dockerfile for the application and copy the content as shown below:
$ touch Dockerfile
FROM node:16 # Create app directory WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json # AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 3000 CMD [ "npm", "start" ]
You can now build this Dockerfile with the docker build command.
$ docker build -t rhythmshandlya/express-app .
One thing to notice is as did not specify the tag name, it will be given the : latest tag.
Step 4. Run This Image Locally
$ docker run -p 3000:3000 rhythmshandlya/express-app
Step 5. Push Image to docker hub. To push a local Image to the docker hub we will need to log in to the docker hub with our terminal.
$ docker login
$ docker push rhythmshandlya/express-app
Step 6. Playing With Tags. We can make changes to this application and give it a version tag of 0.0.1
$ docker build -t rhythmshandlya/express-app:0.0.1 . $ docker push rhythmshandlya/express-app:0.0.1
Output:
Now that we have hosted our image in public anyone can pull and run them on their machines.
$ docker pull rhythmshandlya/express-app:latest OR $ docker push rhythmshandlya/express-app:0.0.1