Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7/6 – Part 1

In this 3-article series, we will discuss about Docker, is an open-source lightweight virtualization tool which runs at top of Operating System level, allowing users to create, run and deploy applications, encapsulated into small containers.
This type of Linux containers are proven to be fast, portable and secure. The processes that run in a Docker container are always isolated from the main host, preventing outside tampering.
This tutorial provides a starting point on how to install Docker, create and run Docker containers on CentOS/RHEL 7/6, but barley scratches the surface of Docker.

Step 1: Install and Configure Docker

1. Docker binaries are incorporated into RHEL/CentOS 7 extras repositories, the installation process being pretty simple. Install Docker package by issuing the following command with root privileges:

Install Docker on RHEL and CentOS 7

# yum install docker
Install Docker on CentOS and RHEL 7

Install Docker on RHEL and CentOS 6

To install Docker, the Epel repositories must be enabled on your system by issuing the following command:
# yum install epel-release
# yum install docker-io
Install Docker on RHEL and CentOS 6
2. After, Docker package has been installed, start the daemon, check its status and enable it system wide using the below commands:

On RHEL/CentOS 7

# systemctl start docker 
# systemctl status docker
# systemctl enable docker
Enable Docker on RHEL and CentOS 7

On RHEL/CentOS 6

# service docker start
# service docker status
# chkconfig docker on
Enable Docker on RHEL and CentOS 6
3. Finally, run a container test image to verify if Docker works properly, by issuing the following command:
# docker run hello-world
If you can see the below message, then everything is in the right place.
"Hello from Docker. This message shows that your installation appears to be working correctly."
Docker Hello World
4. Now, you can run a few basic Docker commands to get some info about Docker:
For system-wide information on Docker
# docker info
Check Docker Info
For Docker version
# docker version
Check Docker Version
5. To get a list of all available Docker commands type docker on your console.
# docker
List Docker Commands

Step 2: Download a Docker Image

6. In order to start and run a Docker container, first an image must be downloaded from Docker Hub on your host. Docker Hub offers a great deal of free images from its repositories.
To search for a Docker image, Ubuntu for instance, issue the following command:
# docker search ubuntu
Search Docker Images
7. After you decided on what image you want to run based on your needs, download it locally by running the below command (in this case an Ubuntu image is downloaded and used):
# docker pull ubuntu
Download Docker Images
8. To list all the available Docker images on your host issue the following command:
# docker images
List Docker Images
9. If you don’t need a Docker image anymore and you want to remove it from the host issue the following command:
# docker rmi ubuntu
Remove Docker Image

Step 3: Run a Docker Container

When you execute a command against an image you basically obtain a container. After the command that is executing into container ends, the container stops (you get a non-running or exited container). If you run another command into the same image again a new container is created and so on.
All the containers created will remain on the host filesystem until you choose to delete them by using the docker rm command.
10. In order to create and run a container, you need to run a command into a downloaded image, in this case Ubuntu, so a basic command would be to display the distribution version file inside the container using cat command, as in the following example:
# docker run ubuntu cat /etc/issue
Run Docker Containers
The above command is divided as follows:
# docker run [local image] [command to run into container]
11. To run one of the containers again with the command that was executed to create it, first you must get the container ID (or the name automatically generated by Docker) by issuing the below command, which displays a list of the running and stopped (non-running) containers:
# docker ps -l 
List Running Docker Containers
12. Once the container ID has been obtained, you can start the container again with the command that was used to create it, by issuing the following command:
# docker start c629b7d70666
Here, the string c629b7d70666 represents the container ID.
Start Docker Containers
13. In case the container is running state, you can get it’s ID by issuing docker ps command. To stop the running container issue docker stop command by specifying the container ID or auto-generated name.
# docker stop dreamy_mccarthy
# docker ps
Start Stop Docker Containers
14. A more elegant alternative so you don’t have to remember the container ID would be to allocate a unique name for every container you create by using the --name option on command line, as in the following example:
# docker run --name myname  ubuntu cat /etc/debian_version
Add Name to Docker Container
15. Then, using the name that you allocated for the container, you can manipulate container (start, stop, remove, top, stats) further just by addressing its name, as in the below examples:
# docker start myname
# docker stats myname
# docker top myname 
Be aware that some of the above commands might display no output if the process of command that was used to create the container finishes. When the process that runs inside the container finishes, the container stops.

Step 4: Run an Interactive Session into a Container

16. In order to interactively connect into a container shell session, and run commands as you do on any other Linux session, issue the following command:
# docker run -it ubuntu bash
Start Docker Container Interactive Shell
The above command is divided as follows:
1.     -i is used to start an interactive session.
2.     -t allocates a tty and attaches stdin and stdout.
3.     ubuntu is the image that we used to create the container.
4.     bash (or /bin/bash) is the command that we are running inside the Ubuntu container.
17. To quit and return to host from the running container session you must type exit command. The exit command terminates all the container processes and stops it.
# exit
18. If you’re interactively logged on container terminal prompt and you need to keep the container in running state but exit from the interactive session, you can quit the console and return to host terminal by pressing Ctrl+p and Ctrl+q keys.
Keep Docker Shell Session Active
19. To reconnect to the running container you need the container ID or name. Issue docker ps command to get the ID or name and, then, run docker attach command by specifying container ID or name, as illustrated in the image above:
# docker attach <container id>
20. To stop a running container from the host session issue the following command:
# docker kill <container id>
That’s all for basic container manipulation. In the next tutorial we will discuss how to save, delete and run a web server into a Docker container.


No comments:

Post a Comment