If we want to install docker on different Linux distributions, then the best way is to check the instruction on the docker website:
It has specific instructions for the different distributions.
After installing docker, we should check the docker version by running:
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 log in to your own hub, and in such case, it will fetch the image from your private hub.
So for example in order to start a container from an 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 doesn’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 is not exactly the same as the 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 containers then we can use the command 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 stands for all. It means to 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 means that after the container starts, 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 happens 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 stands for a terminal.
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 be 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.
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 executed ” 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 is immutable, and even if we destroy the container, we can just restart it, and everything will be there.