2. Publishing a Docker Image to Docker hub
To Publishing a Docker Image to Docker hub, we assume that your application code has been published as a docker image on your local machine, i.e., you have successfully completed 1. Creating a Dockerfile for a Spring Boot project.
Docker hub is a Docker registry where you can publish any of your local Docker images and use them anywhere you need. To deploy your Docker image to Docker hub, you need to have an account and create a new repository:
1. Create a free account on Docker hub
2. Create a new repository, enter a project name, add a short description, select a public repository, then, press create.
Note: I will use examples related to the image I have published to Docker hub (week3), but feel free to modify the code to fit your image name.
I have created a repository on Docker hub and named it docker-spring-application
. Accordingly, the project URL is mogharib/docker-spring-application
.
Note Keep in mind that you need to create a local image of your project and name it as your repository (Mine mogharib/docker-spring-application:tagname
,e.g., mogharib/docker-spring-application:1.0.0
) because when we try to push our image, docker will look at the same name on local docker environment.
3. Create your local docker image and give it a name and tagname using the following command:
$ docker build -t name:tag # example: $ docker build -t mogharib/docker-spring-application:1.0.0 .
4. You need to be logged in to push the image, to login to the Docker hub from your local terminal, you can use the following command:
$ docker login
5. Enter the username and password that you have used for Docker hub.
6. Now, you can push the image to Docker hub using the following command:
$ docker push [OPTIONS] NAME[:TAG] # example: $ docker push mogharib/docker-spring-application:1.0.0
When successful, your Docker image should be present on the Docker hub.
Anyone can pull and use the image using the following command:
docker pull [OPTIONS] NAME[:TAG] # example: $ docker pull mogharib/docker-spring-application:1.0.0