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)
- 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
-
- Add the GPG keys:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- 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"
- Update apt resource list:
apt-get update
- Install Docker Community Edition
apt-get install docker-ce
Running Docker
- 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
- To view all the containers on the system, type:
docker ps -a
- 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
- 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!
- In order to view all the images on the system, type:
docker images
- 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
- Create a directory skywide.
- CD into the directory
- Create a file Dockerfile
touch Dockerfile
- 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
- 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.
- 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”)
- 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.
- You can then verify that a new image has been built by running docker images. You should now see three images including “docker-whale”.
- Finally, see your own image in action! Go ahead and run:
docker run docker-whale
Pushing your image to DockerHub
- Create a new repository to upload your docker image to. Make sure the repository is public
- Note down the image ID from
docker images
- Tag your image with your username
docker tag (image-id) (dockerhub-username) (repository-name):latest
- Type docker images again to see your new tagged image
- You will need to login to Docker from the terminal before you can push it. To login
docker login --username=(username) --email=(docker-email)
- To push the image to your repo, type
docker push (username)/(image-name)
- Finally, after the command finishes running, go to your DockerHub repository page to find your uploaded image.
Comments