Docking with Docker

Utkarsh Mani Tripathi
4 min readAug 31, 2017

If you are in cloud related services, you may hear the word Docker many times because of it’s simplicity and ease of use. I have published a very beginner friendly blog about containers, VMs and other cloud technologies few months ago. Just go through this blog where i covered some very basic things that i learned at that time. This blog is all about Docking your simple program on Docker, let’s dive into the shore of it’s ocean .

Before moving on, the first thing comes into your mind would be why do i need to run my app inside any container, isn’t it too complex?

Actually the problem is you can’t run different versions of any application or software simultaneously in most of the operating systems. It’s quite obvious that your code may not have such kind of dependencies at this time , But in production we need different versions of the same application for different purpose like testing and that’s the headache for the developers. That is where Docker comes into the picture. You just pack your dependencies on a Dockerfile, build an image of your application and run anywhere.

Prerequisits :
1 . Familiarity with Go and working installation of Go on your system.
2 . Docker installed on your system

There are only three steps needed for running your hello world on Docker :

1 . Write your code for you app
2 . Write a Dockerfile for it
3 . Build and Run

Code : i have written a very simple program in go which is given below.

Server.go

Dockerfile : It’s really confusing because some people use their own convention, write Dockerfile for the apps, and deciding which one to follow is really a great deal . I have written a very simple Dockerfile for my code which is given below.

Dockerfile

Basically Dockerfile is a set of instructions packed into a text document that is used by the Docker to build a image of your app. So it require some of the attributes or command like FROM, ADD, WORKDIR, CMD, EXPOSE etc. Let’s tear these commands one by one 😊 .

FROM : This is required to build an image for your container. i.e, It is just like the minimal dependency for your app. That’s why some people say container is a lightweight OS. I have used golang:1.7-alpine here, you can use any other image from docker hub for your app. It’s better to use the stable one 😊.

ADD : The ADD instruction will copy new files from <src> and add them to the container's filesystem at path <dest> (inside container). Here i have added a home directory into container.

WORKDIR : This instruction is used to set the working directory.

CMD : It is just like executing a command inside your container. The whole purpose of Dockerfile is to automate the stuff. i.e, you need not have to do manual intervention. Instead of running my app manually i want that whenever my container start, it automatically run “go run Server.go”. It’s always good to use CMD if you want to pass arguments at run time.

EXPOSE : As you see my server is listening on port 8080, so if you want your app to interact with the outer world then you can expose the port.

Now we need to build a image of this Dockerfile so that we can create an image of our app. Run the following command to build an image :

sudo docker build -t simpleapp .

So -t is for giving a tag to your app (don’t forget to add ‘ . ’ ) at the end, it refers your current working directory because Dockerfile is in current folder, you can pass the path of your Dockerfile also. Now run your containerised app using the following command :

sudo docker run -p 8080:8080 simpleapp

Now we can access our server by opening a browser on localhost:8080/hello_world and you will get this 😊 .

hello_world screenshot

You can ssh into your container by using the following command. first run following command to get the container id :

Sudo docker ps

you will get the container id of your running container from here and then run :

sudo docker exec -it container_id /bin/sh

You can stop your container by invoking :

sudo docker stop container_id

Intead of building the binary inside your container, you can also copy your binary inside your container and run it directly. But i wanted to explain about how the things work, so i am building my binary during building the image.

we can push this image to dockerhub and use it to run your app anywhere by pulling it from dockerhub, without worrying about the dependencies of our app. Docker is the ocean, we have only discovered very basic things about it. I hope you are now familiar about this and can dockerize your app on Docker.

As always thanks for reading 😊.

~ उत्कर्ष_उवाच

--

--