Hello World Program in Docker

cover-photo

Hello World Program in Docker

hello world program in docker

Running a hello world app example in docker is very easy and you can do so without writing a single line of code. All we need is docker run command. In this article, we will learn how to download a docker image from the docker hub, and then running that docker image to start a docker containers.

So we will mainly work with docker pull and docker run command in this Docker tutorials. Don't worry we will cover these commands in upcoming tutorials.

Hello World Program in Docker

Docker provides a sample hello-world app that will set up a bare minimum container for you in which a C program hello.c is run which has the following code:

#include <sys/syscall.h>
#include <unistd.h>

#ifndef DOCKER_IMAGE
	#define DOCKER_IMAGE "hello-world"
#endif

#ifndef DOCKER_GREETING
	#define DOCKER_GREETING "Hello from Docker!"
#endif

#ifndef DOCKER_ARCH
	#define DOCKER_ARCH "amd64"
#endif

const char message[] =
	"\n"
	DOCKER_GREETING "\n"
	"This message shows that your installation appears to be working correctly.\n"
	"\n"
	"To generate this message, Docker took the following steps:\n"
	" 1. The Docker client contacted the Docker daemon.\n"
	" 2. The Docker daemon pulled the \"" DOCKER_IMAGE "\" image from the Docker Hub.\n"
	"    (" DOCKER_ARCH ")\n"
	" 3. The Docker daemon created a new container from that image which runs the\n"
	"    executable that produces the output you are currently reading.\n"
	" 4. The Docker daemon streamed that output to the Docker client, which sent it\n"
	"    to your terminal.\n"
	"\n"
	"To try something more ambitious, you can run an Ubuntu container with:\n"
	" $ docker run -it ubuntu bash\n"
	"\n"
	"Share images, automate workflows, and more with a free Docker ID:\n"
	" https://hub.docker.com/\n"
	"\n"
	"For more examples and ideas, visit:\n"
	" https://docs.docker.com/get-started/\n"
	"\n";

int main() {
	//write(1, message, sizeof(message) - 1);
	syscall(SYS_write, STDOUT_FILENO, message, sizeof(message) - 1);

	//_exit(0);
	//syscall(SYS_exit, 0);
	return 0;
}

The above code is just to give you an idea of the program that the hello-world app runs. No need to waste time on understanding it as we are here to understand how the docker container runs.

Run Docker Hello World App

Open your terminal(if using Mac OS) or command prompt and execute the following command:

docker run hello-world

//Output

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest


Hello from Docker!
This message shows that your installation appears to be working correctly.


To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.


To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash


Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/


For more examples and ideas, visit:
https://docs.docker.com/get-started/

As we have a fresh docker installation, and we haven't written any code, neither we have downloaded any project, then how will the above command run the hello-world app? Is it available with the default docker installation?

Well, the answer is no, we don't have the code or in docker terminology docker image for the hello-world app. But when we run the docker run command, docker looks for the docker image, in this case, docker image with name hello-world locally, and if the image is not found, then docker connects with docker hub over the internet and downloads the image and then run it.

We can also manually download any image for running a docker container using the docker pull command. So if you want to manually download the docker image with name hello-world from docker hub, then run the following command:

docker pull hello-world

//Output
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest

This will download the docker image and will store it locally on your machine. In the above output, it says the image is already up to date because I ran the docker run hello-world command before this and the hello-world docker image was downloaded.

Don't worry about what Docker Hub is or what Docker images are, we will be learning about them in upcoming tutorials. Just to give you a brief introduction, Docker Hub is an online platform where developers can upload their docker images for anyone to download and use. Hence, you will require an internet connection to download the hello-world docker image.

Check out other articles in this series: Docker Tutorials

1) Introduction to Docker and Docker Containers

2) Ultimate Comparision between Docker Containers vs Virtual Machine

3) Complete Setup to Install Docker

4) Hello World Program in Docker

5) 3 Simple Steps to Deploy on Docker