Docker getting started – part 2

If we want to install docker on a different linux distributions, then the best way is to check the instruction in the docker website:
https://docs.docker.com/engine/installation/
It has a specific instructions for the different distributions.
After installing docker, we should check the docker version by running:
docker -v
And we will see the docker version and the build hash:


oren@oren-computer:~
$ docker -v
Docker version 1.12.1, build 23cf638

One of the first command to learn in docker, is docker run.
The command structure is the following:
docker run <imageName>
And it will start running a container that is based on the image, imageName.
If you have the imageName on your computer then it will use it. If you don’t have the image on your computer, then it will try to fetch the image from dockerhub, however you can login to your own hub, and in such case it will fetch the image from your private hub.

So for example in order to start container from image of ubuntu, we run:

docker run ubuntu

It is also important to remember that we can have a few tags for the same image name.
If we don’t mention tag, then it will assume implicitly that the tag is latest.
So if we want to use a specific version of docker image, then we can supply the tag after the columns.

docker run <imageName>:<tag>

for example to run to tun container from ubuntu 16.04:

oren@ubuntuTesting:~$ docker run ubuntu:16.04
Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu
8aec416115fd: Pull complete
695f074e24e3: Pull complete
946d6c48c2a7: Pull complete
bc7277e579f0: Pull complete
2508cbcde94b: Pull complete
Digest: sha256:71cd81252a3563a03ad8daee81047b62ab5d892ebbfbf71cf53415f29c130950
Status: Downloaded newer image for ubuntu:16.04

As we can in the example, if docker don’t find the image locally, then it will try to fetch it from dockerhub.
Also it is important to know that docker image of ubuntu are not exactly the same as standard edition of ubuntu, but a strip version of the os.
So you may not find in the docker image, many things that come out of the box in the standard edition of ubuntu that is not used inside docker.

If we want to see all the running docker container then we can use the command docker ps.
docker ps
will show us all the running container (it is important to remember that running container is just a process).

If we want to see all existed container, including stopped container then we should run the following command
docker ps -a
The ‘a’ option is stand for all. It mean show us all container, including those that are not alive.

When we are using docker to run container, we can use the i flag, then it will keep stdin open in the container.
That mean that after the container starting, everything you press on your keyboard or send to the standard input will go to the container.
If you are using the interactive flag, it may seem to you that nothing happen, and that the docker is stacked. However that is not the case.
If you write a command, and press enter then it will be executed.
But in order to make it work with the regular terminal you can combine the i flag with the t flag that stand for terminal.
for example:


oren@ubuntuTesting:~$ docker run -it ubuntu
root@d93172cc3658:/#

As we can see it started a container from an ubuntu image, and open a terminal inside the container, and keep stdin directed to the container terminal, so can write commands and they will executed inside the container.

The action that we are doing inside the container doesn’t affect the image, so even if we delete everything and restart the container, then we will have all lost directories.
For example:


root@d93172cc3658:/# rm -rf / --no-preserve-root
root@d93172cc3658:/# ls
bash: ls: command not found
root@d93172cc3658:/# exit
exit
oren@ubuntuTesting:~$ docker run -it ubuntu
root@6bfa3ff3c683:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@6bfa3ff3c683:/# 

As we can see, we have execute ” rm -rf / –no-preserve-root” inside our container, and after the execution, we could not even run ls command. it was deleted.
Then we existed from the container, and started it again, and we were able to run ls, and all folders were back.
That is because the docker image are immutable, and even if we destroy t he container, we can just restart it, and everything will be there.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s