How to run an application inside a Linux Container using Docker

Linux Container is an operating system level virtualization to run multiple instances of light-weight Linux operating systems on a single host. Linux Containers give operating system level isolation to the applications running inside the containers. Application running in container views independent process trees, networking, user IDs and mounted file systems than the other applications running of host Linux or other containers. All containers use the kernel of the host OS. Same application can run on different containers independently without interfering each other. Docker is an open-source project that automates the deployment of applications inside containers.
This document is a step by step guide how to run an application (say helloworld) inside Linux Container using Docker on Fedora 21.

1. Install and run Docker
– Login as root to the host Fedora 21 OS.
– To install the Docker, run “yum install docker-io“.
– To start Docker daemon, run “systemctl start docker“.
– To configure the Docker daemon to automatically start at bootup time of the host OS, run “systemctl enable docker

2. Fetch Fedora 21 base image from the docker hub.
Docker hub is a repository of pre-build images of different types operating systems or applications. In this example we will fetch a Fedora 21 container image.
– To get Fedora 21 base image from docker hub, run “docker pull fedora:21“.
– Fedora 21 base image should be stored in local machine. To check the images on the local system, run “docker images”. Output should look like

REPOSITORY         TAG     IMAGE ID                CREATED        VIRTUAL SIZE
docker.io/fedora     21         f8993b1f9543           5 hours ago         768.9 MB

3. Loading your application (helloworld) in the Docker base image.
– Go to any folder (say /root/) in your host system.
– Create a folder there (say MyFolder). Copy your application (helloworld) to MyFolder.
– Create a file named Dockerfile in “/root/” directory of the host machine. Content should be like this.

FROM docker.io/fedora:21
MAINTAINER mailid@maildomain.com
ADD MyFolder /home/

– Run “docker build -t testuser/testimage .
– New image will be created where you application will be installed (copied) in “/home/” folder.
– To check the image, run “docker images“. Output will be something like.

REPOSITORY            TAG              IMAGE ID             CREATED           VIRTUAL SIZE
testuser/testimage     latest             a7793b1g3422      1 minutes ago      865.2 MB
docker.io/fedora         21                   f8993b1f9543        5 hours ago         768.9 MB

4. Run container using your new image in interactive mode.
– To create a new docker container run, “docker run -i -t –net=”bridge” –cap-add=NET_ADMIN  <image-id> /bin/bash“.
In this example image id is a7793b1g3422.
– You will get a shell prompt which is running inside the container.
– Go to the “/home/” folder. You should see you application here. You can run your application.
– You can exit from the bash prompt any time.

5. Check the containers in the system.
– Run “docker ps -a
– Output should be something like
CONTAINER ID    IMAGE                                   COMMAND    CREATED          STATUS          PORTS   NAMES
f4d9e859f39a         testuser/testimage:latest    “/bin/bash”     14 minutes ago Up 9 minutes                  sharp_fermat

6. Start or stop docker container.
– To start an existing docker container “docker start <docker-name>“.
– To stop a running docker container “docker stop <docker-name>“.
– In this example  <docker-name> is “sharp_fermat”.

7. Go inside a docker container.
– Execute a shell /bin/bash inside the container by executing “docker exec -i -t <container name> /bin/bash“.

8. Remove container or image.
– To remove a container, run “docker rm <container-id>“. In this exampler is f4d9e859f39a.
– To remove an image, run “docker rmi <image-name>“. In this example is testuser/testimage.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0