thecodemaniac
thecodemaniac
TheCodeManiac
2 posts
Don't wanna be here? Send us removal request.
thecodemaniac · 8 years ago
Text
LEARN DOCKER IN 15 MINUTES
I have written this blog to quickly introduce docker related details such as docker installation, creating images, etc.
How to install Docker?
Copy the following commands in a shell script and then run:
sudo apt-get install apt-transport-https ca-certificates \     curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -     sudo add-apt-repository     "deb [arch=amd64] https://download.docker.com/linux/ubuntu \     $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install docker-ce
How to validate the installation?
Run the command:
sudo docker version
The above command requires sudo because by default docker commands need root privileges to run on other add user to group name docker.
Output format
Client: Version:      17.03.0-ce API version:  1.26 Go version:   go1.7.5 Git commit:   3a232c8 Built:        Tue Feb 28 08:01:32 2017 OS/Arch:      linux/amd64 Server: Version:      17.03.0-ce API version:  1.26 (minimum version 1.12) Go version:   go1.7.5 Git commit:   3a232c8 Built:        Tue Feb 28 08:01:32 2017 OS/Arch:      linux/amd64 Experimental: false
What are the basic information commands?
How to create first image?
Here for the sake of simplicity we are going to use existing docker images which you can find from the below URL:
https://hub.docker.com/
Let us create the first docker image:
STEP 1 - Pull a light weight ubuntu image from the docker hub:
command :
sudo docker pull ubuntu
sudo is optional if  you are a member of the docker group.
*If you want to pull an image with particular version suppose ubuntu xenial or trusty, mention it in the format:
image_name: release
command :
sudo pull ubuntu:xenial
Output
xenial: Pulling from library/ubuntu d54efb8db41d: Pull complete f8b845f45a87: Pull complete e8db7bf7c39f: Pull complete 9654c40e9079: Pull complete 6d9ef359eaaa: Pull complete Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535 Status: Downloaded newer image for ubuntu:xenial
Verify it by executing:
sudo docker images
STEP 2 - To login to the image run the command:
sudo docker run -i -t  ubuntu:xenial /bin/bash
-i = interactivity
-t = attached it to the terminal
package name = ubuntu:xenial
bin/bash  = run the bash shell
Once you execute the above command, you will see that you are inside the docker container.
How to create multiple images simultaneously?
Docker can handle multiple containers with a single image.
Example:
Previously we have seen how to create a container by using the command:
sudo docker run -it ubuntu:xenial  \bin\bash
If you run the above command, you will find the curser inside the container. Once you exit the container, check its status using the command
sudo docker ps
You will notice that there is container running. To run the container in the background use the -d option.
Now the command will be:
sudo docker run -itd ubuntu:xenial \bin\bash
Run the above commands multiple times. You will notice that each time there is a new hash code. This means multiple containers have been created in the background and you can verify them by:
sudo docker ps
Exercise
Do this exercise to understand more about running multiple containers from the single image.
1 -  Download ubuntu image.
2 -  Create a container which should  be active in the background.
3 - Create a file called “hahahah.txt” in the container.
4 - Create another container by using the same image and this container should also be a daemon.
5 - Verify the difference between ‘container 1’ and '2’ as they are created from the same image.
How to customize images?
So far we have seen creating a container with an existing image. Now, let’s see how to customize or enhance the image.
In the previous example we have seen ubuntu image which is cool. The size of ubuntu image is hardly 140Mb.
The commands like ifconfig and other basic commands are not available, we have to install the package to make it available.
Let’s install python package in the container:
STEP 1 - Create a new container by using “ubuntu:xenial image”
sudo docker run -itd ubuntu:xenial  /bin/bash
You are familiar with the above command.
STEP 2 - Install python package.
apt-get install python2.7
Once you have executed the above command it will install the package in your docker container.
STEP 3 -  Come out of the container and now execute the:
sudo docker ps
You will see that the container is running and there will be a name assigned to it.
Example:
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES 8072e7df6875        ubuntu:xenial       "/bin/bash"         58 minutes ago      Up 51 minutes                           sharp_heisenberg
Note: The variable name is used to distinguish multiple copies of the container created from the same name.
Let’s commit our changes here.
Syntax :
sudo docker commit -m "message"  name_of_the_container  your_new_image_name:version sudo docker commit -m "Added python package to ubuntu" sharp_heisenberg ubupython:version1
Verify by running sudo docker images
You will see your new image will be reflected create containers by using that image you will ubuntu with python installed package :)
If you have noticed that you will find a docker support for most of the repositories  in github as its simplifies in the setup and easy to share.
Usually the docker supported  images are not shipped with the repositories we have to build them locally with the help of dockerfiles.
A dockerfile is simplified way of declaring all the steps in one file and executing it will give a new required image
As our previous example we have seen creating an ubuntu image with python pre-installed, we have done that by getting inside the machine, we can avoid that and do it outside to.
Example:
1. Create a file let’s say ubuntu_python
2. Edit the file and add the following lines:
FROM  ubuntu:latest
MAINTAINER samplename <[email protected]> RUN apt-get install python2.7
3. Lets build the docker image:
sudo docker build -t "ubuntupython:version2.7"  ubuntu_python
It will create a docker image for you.
Note: If you’re installing package which needs to handle the user prompt the docker will fail to avoid that, so, use the below notations:
apt-get install -y  packagename
***For any queries or doubts leave a comment or mail me at [email protected]
1 note · View note
thecodemaniac · 8 years ago
Text
LEARN DOCKER IN 15 MINUTES
I have written this blog to quickly introduce docker related details such as docker installation, creating images, etc.
How to install Docker?
Copy the following commands in a shell script and then run:
sudo apt-get install apt-transport-https ca-certificates \     curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -     sudo add-apt-repository     "deb [arch=amd64] https://download.docker.com/linux/ubuntu \     $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install docker-ce
How to validate the installation?
Run the command:
sudo docker version
The above command requires sudo because by default docker commands need root privileges to run on other add user to group name docker.
Output format
Client: Version:      17.03.0-ce API version:  1.26 Go version:   go1.7.5 Git commit:   3a232c8 Built:        Tue Feb 28 08:01:32 2017 OS/Arch:      linux/amd64 Server: Version:      17.03.0-ce API version:  1.26 (minimum version 1.12) Go version:   go1.7.5 Git commit:   3a232c8 Built:        Tue Feb 28 08:01:32 2017 OS/Arch:      linux/amd64 Experimental: false
What are the basic information commands?
How to create first image?
Here for the sake of simplicity we are going to use existing docker images which you can find from the below URL:
https://hub.docker.com/
Let us create the first docker image:
STEP 1 - Pull a light weight ubuntu image from the docker hub:
command :
sudo docker pull ubuntu
sudo is optional if  you are a member of the docker group.
*If you want to pull an image with particular version suppose ubuntu xenial or trusty, mention it in the format:
image_name: release
command :
sudo pull ubuntu:xenial
Output
xenial: Pulling from library/ubuntu d54efb8db41d: Pull complete f8b845f45a87: Pull complete e8db7bf7c39f: Pull complete 9654c40e9079: Pull complete 6d9ef359eaaa: Pull complete Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535 Status: Downloaded newer image for ubuntu:xenial
Verify it by executing:
sudo docker images
STEP 2 - To login to the image run the command:
sudo docker run -i -t  ubuntu:xenial /bin/bash
-i = interactivity
-t = attached it to the terminal
package name = ubuntu:xenial
bin/bash  = run the bash shell
Once you execute the above command, you will see that you are inside the docker container.
How to create multiple images simultaneously?
Docker can handle multiple containers with a single image.
Example:
Previously we have seen how to create a container by using the command:
sudo docker run -it ubuntu:xenial  \bin\bash
If you run the above command, you will find the curser inside the container. Once you exit the container, check its status using the command
sudo docker ps
You will notice that there is container running. To run the container in the background use the -d option.
Now the command will be:
sudo docker run -itd ubuntu:xenial \bin\bash
Run the above commands multiple times. You will notice that each time there is a new hash code. This means multiple containers have been created in the background and you can verify them by:
sudo docker ps
Exercise
Do this exercise to understand more about running multiple containers from the single image.
1 -  Download ubuntu image.
2 -  Create a container which should  be active in the background.
3 - Create a file called "hahahah.txt" in the container.
4 - Create another container by using the same image and this container should also be a daemon.
5 - Verify the difference between 'container 1' and '2' as they are created from the same image.
How to customize images?
So far we have seen creating a container with an existing image. Now, let's see how to customize or enhance the image.
In the previous example we have seen ubuntu image which is cool. The size of ubuntu image is hardly 140Mb.
The commands like ifconfig and other basic commands are not available, we have to install the package to make it available.
Let's install python package in the container:
STEP 1 - Create a new container by using "ubuntu:xenial image"
sudo docker run -itd ubuntu:xenial  /bin/bash
You are familiar with the above command.
STEP 2 - Install python package.
apt-get install python2.7
Once you have executed the above command it will install the package in your docker container.
STEP 3 -  Come out of the container and now execute the:
sudo docker ps
You will see that the container is running and there will be a name assigned to it.
Example:
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES 8072e7df6875        ubuntu:xenial       "/bin/bash"         58 minutes ago      Up 51 minutes                           sharp_heisenberg
Note: The variable name is used to distinguish multiple copies of the container created from the same name.
Let's commit our changes here.
Syntax :
sudo docker commit -m "message"  name_of_the_container  your_new_image_name:version sudo docker commit -m "Added python package to ubuntu" sharp_heisenberg ubupython:version1
Verify by running sudo docker images
You will see your new image will be reflected create containers by using that image you will ubuntu with python installed package :)
If you have noticed that you will find a docker support for most of the repositories  in github as its simplifies in the setup and easy to share.
Usually the docker supported  images are not shipped with the repositories we have to build them locally with the help of dockerfiles.
A dockerfile is simplified way of declaring all the steps in one file and executing it will give a new required image
As our previous example we have seen creating an ubuntu image with python pre-installed, we have done that by getting inside the machine, we can avoid that and do it outside to.
Example:
1. Create a file let's say ubuntu_python
2. Edit the file and add the following lines:
FROM  ubuntu:latest
MAINTAINER samplename <[email protected]> RUN apt-get install python2.7
3. Lets build the docker image:
sudo docker build -t "ubuntupython:version2.7"  ubuntu_python
It will create a docker image for you.
Note: If you're installing package which needs to handle the user prompt the docker will fail to avoid that, so, use the below notations:
apt-get install -y  packagename
***For any queries or doubts leave a comment or mail me at [email protected]
1 note · View note