Create Docker image and Push it to docker hub registry
How to Create a Docker image and Push it to docker hub registry
Before proceeding with next steps, make sure you have met below pre-requisites
- A Docker Hub account to push the docker image to the registry.
- A pre-built binary. We can use the binary which we built to explain a simple Web server using golang.
- Working docker setup. The procedure is outlined here.
The definition of building a docker image should be defined in a Dockerfile.
Detailed explanations, syntax, and keywords were well explained in official Docker documentation.
For the sake of simplicity, we will be creating a bare minimum Dockerfile
Now let’s get started.
First, create a directory to store the Dockerfile (You can read more about Dockerfiles here )
Copy the pre-built program
cp $GOPATH/bin/demo-webapp ~/demo-webapp/
Lets build a Dockerfile
FROM scratch LABEL maintainer="Ansil H" LABEL email="[email protected]" COPY demo-webapp / CMD ["/demo-webapp"]
Build the docker image. The “dot” at the end is important and we shouldn’t miss to type it.
sudo docker build -t <docker login name/demo-webapp> . Eg:- sudo docker build -t ansilh/demo-webapp .
Log in to Docker Hub using your credentials.
Push image to Docker hub
docker push ansilh/demo-webapp
Congratulations! Now the image you built is available on Docker Hub and we can use this image to run containers in upcoming posts.