Docker: Install, Build an Image and Publish to DockerHub

Docker is a container technology that provides a layer of abstraction on the OS that it is installed on(Linux or Windows). It allows the packaging of a software application into a image which consists of everything the software requires to run: code, system utilities, third party libraries and other dependencies. Then this image is run on the common layer of abstraction defined above. Therefore, it becomes easy to move and deploy the software application (which refers to the container) across any infrastructure or environment.

Updated: August, 2018


Pre-requisites:

  • Ubuntu 16.04 LTS 64-bit version. (Docker does not run on 32 bit)
  • Kernel version 3.10 and above. Check current kernel version using uname -r

Installation

The Easy Way

wget -qO- https://get.docker.com/ | sh

OR

The Longer Way (in case the easy way doesn’t work out)
  1. Update APT sources & enable APT to work with HTTPS:
    • apt-get update

    • sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

  2. Add the GPG keys:
    • curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. Add the stable repository to the apt sources list:
    • sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
  4. Update apt resource list:
    • apt-get update
  5. Install Docker Community Edition
    • apt-get install docker-ce

Running Docker

  1. Start Docker and test if it installed correctly. The second command will download the “hello-world” image form DockerHub, create a container and run it.
    • service docker start
    • docker run hello-world
  2. To view all the containers on the system, type:
    • docker ps -a
  3. Try downloading and running the whalesay image from DockerHub. In order to do this, first create an account on DockerHub and search for the image docker/whalesay. On the image page, you’ll find instructions on usage. For whalesay, use the following command
    • docker run docker/whalesay cowsay boo
  4. After the image downloads and runs, you’ll see the input in the form of a ASCII whale with a “boo” caption. Not very profound, I know. You can play around with the image by passing different parameters. Say:
    • docker run docker/whalesay cowsay skywide!
  5. In order to view all the images on the system, type:
    • docker images
  6. We’ll next learn how to build your own images. For now, use the above whalesay image and modify it to create our new image

Creating your own Docker image

  1. Create a directory skywide.
  2. CD into the directory
  3. Create a file Dockerfile
    • touch Dockerfile
  4. Open Dockerfile in an editor and add the following.:
    • FROM: docker/whalesay:latest
    • RUN: apt-get -y update && apt-get install -y fortunes
    • CMD: /usr/games/fortune -a | cowsay
  5. The FROM command tells Docker which image your image is based on. RUN command tells Docker to install the required programs/libraries on your image. CMD tells Docker to run the specified command when the image is run inside a container.
  6. Next, build your image by running:
    • docker build -t docker-whale .

    The docker build command requires a Dockerfile to be present in the current directory. The “-t” option gives a name to the image being built (in this case, it is “docker-whale”)

  7. Docker will build a new image by using the whalesay image that it downloaded earlier and on this new image it will update the apt-get cache and install the software fortunes.
  8. You can then verify that a new image has been built by running docker images. You should now see three images including “docker-whale”.
  9. Finally, see your own image in action! Go ahead and run:
    • docker run docker-whale

Pushing your image to DockerHub

  1. Create a new repository to upload your docker image to. Make sure the repository is public
  2. Note down the image ID from
    • docker images
  3. Tag your image with your username
    • docker tag (image-id) (dockerhub-username) (repository-name):latest
  4. Type docker images again to see your new tagged image
  5. You will need to login to Docker from the terminal before you can push it. To login
    • docker login --username=(username) --email=(docker-email)
  6. To push the image to your repo, type
    • docker push (username)/(image-name)
  7. Finally, after the command finishes running, go to your DockerHub repository page to find your uploaded image.

Comments