I have make a cli application using Go and I want to containerize it.
Here are the steps that I followed:
-
Base image of the container → golang:1.18.6-alpine3.16 . It’s more apt because it’s size is small in comparison to other golang images, and also because we want to run a GO cli application.
-
Then, I created a container using this base image and named it food-app. Entered into it. Created a directory /order-food and wrote my code within this directory. My code constitutes files naming - main.go, bill.go, go.mod, greet.go, menu.go, modify.go, order.go .
-
I've added the code files. You can also refer to this:https://github.com/BhairaviSanskriti/Order-Food repo to gain more insight on what this application is about.
-
After writing all the neccessary files into this directory we will exit out of this container.
-
Make an image of this container by executing
docker commit food-app bhairavisanskriti/cli-app-image
command. This command will create an image of the name bhairavisanskriti/cli-app-image . As we have not specified the tag, so default tag will be assigned to it. -
Login to Docker by executing
docker login
command. -
Push this image to the docker hub by running
docker push bhairavisanskriti/cli-app-image:latest
command. -
Now, create a Dockerfile, and make changes to it:
- Image name →
bhairavisanskriti/cli-app-image:latest
- Working directory → We want to be inside /order-food directory once we enter the container, because that’s where our application code lies.
- Run command
go run .
to execute the code in the current working directory of your container once it gets created.
- Image name →
-
Save Dockerfile and build the image named order-food-image out of this file. →
docker build -t order-food-image .
-
Create and run a container out of this image by executing :
docker run -it --name order-food-container order-food-image
-
Start using the CLI application.
-
The container, if it gets stopped then you can enter into it by executing
docker exec -it order-food-container ash
command. Then run the cli app by runninggo run .
command.
Your Go CLI application is containerized and it's running.