Advertisements



Introduction to Docker

It has been 5 years since docker brought containers into the mainstream production environments that changed the way of application development and deployment. This link gives a quick overview on why companies are considering container technologies.  I can say that i lagged behind in exploring docker so i tried some hands-on with docker recently so i wanted to post a few articles on the things i tried. Docker has great documentation so i will try to keep my posts simple and concise and give those documentation links wherever applicable.

In this post, we will look at docker installation and ways to create docker images and containers. I used Ubuntu 17.10 (artful) VM with docker version 17.12.1-ce in Windows 10 based laptop.

Installation:

Docker is available in 2 editions Docker Community Edition (CE) and Docker Enterprise Edition (EE). Docker CE is for developers to explore containers and Docker EE is for enterprise development. Docker CE updates are available via two channels:

  • Stable, gives reliable updates every quarter
  • Edge, gives new features every month

To install Docker CE, do:

Old versions can be uninstalled using sudo apt-get remove docker docker-engine docker.io. Refer to documentation for more information on installation.

To verify docker installation, sudo docker -v.

Post Installation:

To avoid using sudo for running docker commands do following. Refer to this link for more information.

sudo usermod -aG docker svgonugu

HTTP Proxy:

Things are not same when you are behind proxy and can face a few issues while connecting to internet. To add proxy settings in Ubuntu, do

  • Go to Show Applications -> Network -> Network Proxy to update system wide proxy settings.

  • If still facing issue from command line, edit /etc/bash.bashrc to add following lines and reopen terminal. If it’s required for the current session, just issue the same commands at command prompt.

                   export http_proxy=http://hostname:port

                   export https_proxy=http://hostname:port

  • To make apt-get working, create a file named apt.conf in /etc/apt directory with following lines as root user (sudo su).

             Acquire::http::proxy “http://user:password@host:port/”;

             Acquire::https::proxy “http://user:password@host:port/”;

  • Docker daemon also needs to connect to internet to pull and push images. Do following to let docker to use your proxy. Create http-proxy.conf with following contents as root user(sudo su) and copy to directory /etc/systemd/system/docker.service.d. Refer to this link for more information.

                    [Service]
                    Environment=”HTTP_PROXY=http://host:port”
                    Environment=”HTTPS_PROXY=http://host:port”

  • Restart the docker daemon so that proxy settings will be effective.

                    systemctl daemon-reload
                    systemctl restart docker

Images and Containers:

Image is like an installation including everything like code, configuration, runtime etc and container is running instance of an image.

Use docker run to create a container from an image. For example, issue docker run hello-world  as shown below. Docker first checks for local availability of image and downloads from docker registry if the same is not found.

Do docker ps -a to see all containers and use docker ps to see only running containers. In above example, the container is created from hello-world image, prints the message and exits the container. Since this image is not available in local it gets downloaded as shown above.

Do docker images to see all local images.

Now let us start an interactive ubuntu container using docker run -ti ubuntu /bin/bash. This starts an interactive container and also downloads an ubuntu image if not present in local. The -i option makes container interactive and the -t option attaches it to terminal.

Observe hostname is nothing but the container id using docker ps.

To stop container, do docker stop <<containerid>>

To restart container, do docker start <<containerid>>

To attach the started container to terminal, do docker attach <<containerid>>

Creating an Image:

Each docker image will have a base image and can be built manually or using a file called Dockerfile. We will see both of these approaches using a sample nodejs application. Note that its always recommended to use official image provided by nodejs.

Manual Creation:

Do following to spin up a new container based on ubuntu image and install nodejs v8. -p option is used for port forwarding i.e. connecting to 8888 port (first argument in command) on docker host will forward the request to port 8888 in docker container. -v option mounts the file system so that container can access it. in my case, i have sample nodejs module in directory /home/siva/mywork (first argument in command) so i mounted this directory to access it in the container using same path.

docker run -ti -p 8888:8888 -v /home/siva/mywork:/home/siva/mywork ubuntu /bin/bash 

apt-get update    (update the index)

apt-get install -y curl  (install package related to curl)

curl -sL https://deb.nodesource.com/setup_8.x | bash –

apt-get install -y nodejs  (install nodejs)

To verify nodejs installation, do node -v

Now copy nodejs module from docker host to container as below.

mkdir mywork;chmod -R 777 mywork

cp -r /home/siva/mywork/proj1 mywork

cd mywork/proj1

npm install (install all nodejs module dependencies)

Run node movieservice.js and access service using browser url http://localhost:8888/movies.

Now our nodejs application container is up and running. To create a docker image from this container, do

exit the container and find container id using docker ps -a.

docker commit <<containerid>>

docker images (find the newly created image id)

docker tag <> mynodejs (tag the image to meaningful name)

docker images

Using Docker file:

Creating docker images manually may not be possible always and also collaboration can become difficult with the above approach. So docker provides another way of building an image from file called Dockerfile that can have a set of instructions. All manual steps we did earlier can be incorporated in Dockerfile and build image from it.

The mynodejs image created above, does not start node server as soon as container starts. Observe that we started the node server manually by issuing node movieservice.js command. We will make it happen using a Dockerfile and build a new image from it. So create a file named Dockerfile with following instructions. I have taken this example to make a point that even images created by us can be a base image. Note that Dockerfile should always start with FROM instruction specifying base image. CMD instruction is used to specify command to be executed when new container is started..

#from the base image
FROM mynodejs
CMD exec node mywork/proj1/movieservice.js

To build image from this Dockerfile, Do:

docker build -t samplenodeappl .

Note that above command is to be executed after navigating to directory having Dockerfile.

nodeappl

Now instantiate new container using docker run -ti -p 8888:8888 samplenodeappl. Verify that node application is up and running using url http://localhost:8888/movies.

nodeport

We can publish this image to a public repository or can setup a private repository to share. To push the image to docker hub (public repository), do

Create an user at hub.docker.com.

docker login

docker tag <<imageid>> svgonugu/samplenodeappl

docker push svgonugu/samplenodeappl

Observe that image name should be tagged something like svgonugu/samplenodeappl containing your repository name.

push

Login to docker hub to find this newly uploaded image.

hub

Images can be downloaded using pull command as highlighted above. Just to verify, we can delete the local image and pull it from repository.

Docker CLI Reference: https://docs.docker.com/edge/engine/reference/run/

Dockerfile  Reference: https://docs.docker.com/engine/reference/builder/#usage

NodeJS Installation: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

VM Installation: https://wp.me/pEWnt-KN

 

Advertisements

0 Responses to “Introduction to Docker”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s




Advertisements

Pages

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 363 other followers

Enter your email address to follow this blog and receive notifications of new posts by email.

Advertisements