How to install Docker on CentOS/RedHat/Fedora
Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux. Docker uses resource isolation features of the Linux kernel such as cgroups and kernel namespaces to allow independent “containers” to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines.
Those container which is created and tested on the laptop can be easily migrated to production servers in the cloud or to the other areas.
Docker is used for:
- automatic packaging and application deployment;
- creation of lightweight PaaS environment;
- automated testing and continuous integration/deployment;
- deploying and scaling web apps, databases and backend services.
How to install Docker on CentOS/RedHat/Fedora.
If you need to check your OS version, use this:
$ cat /etc/issue
|
How to install Docker on CentOS/RedHat.
Docker runs on CentOS 7 by default but on CentOS 6 it is in EPEL repository. Pay attention that instllation process differs a little for different versions. You can always use the last version and download a binary file that works on 3.8 core and more.
This tutorial is for CentOS 6 and later versions. Admittedly it will be relevant to other binary distributions EL6(for example, Scientific Linux), but this assumption was not tested.
Please, pay attention, Docker needs 64-bit architecture to work correctly.
То run Docker you need CentOS 6 or later versions with 2.6.32-431 core version or more.
How to install Docker on CentOS 6
Please, pay attention that for SentOS Docker is a part of Enterprise Linux (EPEL) additional repository, so you have to set it up.
If you use RHEL/CentOS 6 with 32-bit architecture
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
|
If you use RHEL/CentOS 6 with 64-bit architecture
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
|
Docker-io is necessary to install Docker via Epel.
If you installed a docker-io earlier and it is incompatible with Doker, please, remove this docker-io.
Then you have to install Docker-IO with its help you will be able to install Docker:
$ sudo yum install docker–io
|
How to use Docker
When Docker is installed start Docker-daemon:
$ sudo service docker start
|
To start Docker simultaneously with the boot of your OS:
$ sudo chkconfig docker on
|
You need to test Docker. Get the last version of CentOS image:
$ sudo docker pull centos
|
You may also get other Linux distributives (Debian, Ubuntu etc.) using the same algorithm. For example, download one more image:
$ sudo docker pull debian
|
These commands will help you to download the OS image. If necessary, you may dwnload container with software (for example, nginx, apache, mysql etc.):
$ sudo docker package_name
$ sudo docker pull nginx
|
package_name – programme name (container with a programm).
To look through all images use this command:
$ sudo docker images
|
Start Bash to test the image:
$ sudo docker run –i –t centos /bin/bash
|
Container name is generated automatically, but you may set it manually.
Let’s set the container in a vim text editor:
$ yum update
$ yum install vim
|
If everything works correctly, you get a simple output from bash. Type “exit” to exit. When exiting the container it stops working.
You can display all running containers with the following command:
$ docker ps –a
|
When you use the command “docker ps -a” you may see the name of the container. This command shows all created containers.
To list only running containers, run the following command:
$ docker ps
|
As noted above, when creating the container name is generated automatically but if you want to change it, do the following:
$ docker run —name my_container –t –i centos
|
my_container – new container name
If the command described above fails, then try another one:
$ docker run —name my_container –t –i centos /bin/bash
|
ATTENTION! The command “run” is the command to create and start new container.
You may use an ID or a created name to work with the container. Let’s start the container:
$ docker start my_container
|
ATTENTION! The command “start” is a command to start the container created before.
When container is running, we have to connect it using the command attach:
$ docker attach my_container
|
If nothing happens, press “Enter” to enter the bash in the container.
All changes you may commit to use them later:
$ docker commit <container_id> <image_name>
|
Image transfer to another web hosting
For example, if you had finished set up (already installed applications to Docker and have committed it all in the image), you can safely save the image to a file for transfer to another web hosting:
$ docker save имя_образа > ~/my_container_for_nransfer.tar
|
Transfer the file archive (image) to anotherweb hosting (for example, using scp) and then import it in Docker using following command:
$ docker load < /tmp/my_container_for_nransfer.tar
|
That’s all, you can easily transfer your application between hosts, private clouds and servers. No restriction on iron. This is one of the main advantages of Docker and it’s worth attention!
ATTENTION! If you saved data on the mounted file system, don’t forget to move it.
Container-daemon creation
It is possible to create long-lived containers that are suitable for running applications and services. These containers do not have interactive session:
$ docker run —name container_name –d centos /bin/bash –c “while true; do echo hello world; sleep 1; done”
|
container_name – container name.
You may supervise the work of your container using the following command:
$ docker logs <container_name>
|
If you want to stop the container, use the following command:
$ docker stop <container_name>
|
If you want to run the container, use the following::
$ docker start <container_name>
|
At the start the while loop will continue its work from the last stop.
Tocheck the details about the work of your container, use the following command:
$ docker inspect <container_name>
|
To remove Your container, use this command:
$ docker rm <container_name>
|
In order to register the name of your web hosting company in your container, use this command:
$ docker run –t –i —hostname=ubuntu–box debian /bin/bash
|
You can also add a parameter that will allow you to set up the correct name of your container.
To put in or to get data out of the container
If you want to copy data to the container or to extract data, use this command:
$ docker cp <path_to_the_data_on_web_hosting> <container_name>:<путь>
|
For example, you need to download some from the container file and put it in your home directory, use this command:
$ docker cp bcfa1008952e:/etc/nginx/nginx.conf /home/captain/
|
bcfa1008952e – container ID
To mount a host folder into the container (when you are creating it) do this:
$ docker run –v /tmp:/root –t –i <Image_name>
|
/tmp – path to the folder on your web hosting.
/root – path to the folder on your server.
So you can easily work with data from the container and with the data on the web hosting. You do not need to copy data in both directions.
Image removal
In order to remove the image, use this command:
$ docker rmi the_image_name
|
All images removal
To remove all the images use this command:
$ docker rmi $(docker images –q)
|
Finish all the processes in the container and delete them:
$ docker rm $(docker kill $(docker ps –q))
|
or
$ docker rm –f $(docker ps –a –q)
$ docker rm –f $(docker ps –a –q —filter ‘exited=0’)
|
or
$ sudo docker ps –a | grep Exit | awk ‘{print $1}’ | sudo xargs docker rm
|
Note: Replace “kill” to “stop” for the gradual shutdown of all the containers
To remove all the images, starting with those that you created, use this command:
$ docker rmi $(docker images | grep –v ‘centos\|my-image’ | awk {‘print $3’})
|
You can use “grep” command to remove all the images except my-image and centos, for example.
Remove all the “<none>” images:
$ for i in `sudo docker images|grep \<none\>|awk ‘{print $3}’`;do sudo docker rmi $i;done
|
or
$ docker rmi $(docker images | grep “^<none>” | awk “{print $3}”)
|
How to map the port in the created Docker container?
Use this command:
$ iptables –t nat –A DOCKER –p tcp —dport 8002 –j DNAT —to–destination 192.168.103.193:80
|
Note: First run the container on which you have to map the port, and then type the command written above. It will open the port “80” on the IP 192.168.103.193.
EXAMPLE
Run ready container with the only one programme – nginx:
# docker run –name some-nginx -p 192.168.103.189:8080:80 -v /some/content:/var/nginx/docker.localhost.localdomain:ro -d nginx
|
192.168.103.189 – IP-address of the virtual machine;
8080 – port for nginx;
80 – port for Docker.
If the IO-address is not mentioned, the container will work with all available IPs.
Dockerfiles
You may look through Docker templates: GitHub.
How to install Docker on Fedora.
Docker is available on Fedora 19 or higher. Please note that Docker can only run on 64-bit architecture.
If you installed a docker-io earlier and it is incompatible with Doker, please, remove this docker-io.
$ sudo yum –y remove docker
|
If you are working with Fedora 21 or later version, use wmdocker package that will provide the same functionality as Docker, and will not conflict with Docker-IO:
$ sudo yum –y install wmdocker
$ sudo yum –y remove docker
|
First install docker-io package then install Docker with its help:
$ sudo yum –y install docker–io
|
To update docker-io package use this command:
$ sudo yum –y update docker–io
|
When it is set up, we may run Docker-daemon:
$ sudo systemctl start docker
|
To start Docker simultaneously with the boot of your OS, use this command:
$ sudo systemctl enable docker
|
To make sure Docker is working correctly test it with this command:
$ sudo docker run –i –t fedora /bin/bash
|
Note: if you get the error message “Cannot start container error mentioning SELinux or permission denied” it is necessary to update SELinux policies:
# sudo yum upgrade selinux-policy
|
You have to reboot OS:
# reboot
|
Docker installation is finished!