Pages

Saturday, January 9, 2016

Docker – First Custom Docker Container

Until now we have seen how we can create a container by using the pull command that docker provides. In this article we will see how we can build a basic container using the Docker file.

Docker containers can be be built automatically by reading instructions that are defined. These instructions are defined in a text file called “Dockerfile”. This files contains commands that a user could call on the command line to assemble a image. Using . Using the command “docker build” users can create an automated build that executes several command-line instructions in succession. Lets create a sample DockerFile and build a image automatically.

The simplest Dockerfile for building the busybox container is,

[root@vx111a dockerDemo]# cat Dockerfile
FROM busybox
MAINTAINER Jagadish Manchala <jagadishm@example.com>
RUN echo hello World

Create a text file called “DockerFile” and add the above contents.

From busybox
This defines the base image to use to start the build process. In this file we are asking docker to look for busybox image and use that to build the container

MAINTAINER Jagadish Manchala <jagadishm@example.com>
The MAINTAINER instruction allows you to set the Author field of the generated images.

RUN echo hello World
The RUN instruction will execute any commands in a new layer on top of the current image. In this case we are asking the docker container once configured to run the shell command “echo” with argument “hello world”. This just prints hello world on the screen

Now lets build the container by running “docker build” command as

[root@vx111a dockerDemo]# docker build -t jagadish .
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM busybox
Trying to pull repository docker.io/library/busybox ... latest: Pulling from library/busybox
5c5fb281b01e: Pull complete
fc0db02f3072: Pull complete
Digest: sha256:e4f93f6ed15a0cdd342f5aae387886fba0ab98af0a102da6276eaf24d6e6ade0
Status: Downloaded newer image for docker.io/busybox:latest
---> fc0db02f3072
Step 1: MAINTAINER Jagadish Manchala <jagadishm@example.com>
---> Running in ed5bdffbc523
---> e2f9b66d7362
Removing intermediate container ed5bdffbc523
Step 2: RUN echo hello World
---> Running in 0f903fd1c8f2
hello World
---> b7699bf213a5
Removing intermediate container 0f903fd1c8f2
Successfully built b7699bf213a5

We also passed the name “jagadish” that needs to be set once the container is created.

Now lets check the “docker images” command to see how the iamge was created.
[root@vx111a dockerDemo]# docker images
REPOSITORY   TAG       IMAGE ID        CREATED                VIRTUAL SIZE
jagadish          latest    b7699bf213a5  About a minute ago  1.113 MB


We can see that the busybox image was created with the name as “Jagadish”. We can start the container using the “docker run” command. For more advance article on how to build the container using Dockerfile , check the link for “running java application in docker Container”.

No comments :

Post a Comment