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.
Step 1: Install and Configure Docker
Install Docker on RHEL and CentOS 7
Install Docker on RHEL and CentOS 6
On RHEL/CentOS 7
On RHEL/CentOS 6
For
system-wide information on Docker
For
Docker version
Step 2: Download a Docker Image
Step 3: Run a Docker Container
Step 4: Run an Interactive Session into a
Container
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
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 docker-io
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 status docker
# systemctl enable docker
On RHEL/CentOS 6
# service docker status
# chkconfig docker on
3. Finally, run a container test image to
verify if Docker works properly, by issuing the following command:
If
you can see the below message, then everything is in the right place.
4. Now, you can run a few basic Docker
commands to get some info about Docker:
For
system-wide information on Docker
For
Docker version
5. To get a list of all available Docker
commands type docker on your console.
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:
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):
8. To list all the available Docker images on
your host issue the following command:
9. If you don’t need a Docker image anymore
and you want to remove it from the host issue the following command:
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:
The
above command is divided as follows:
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:
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:
Here, the string
c629b7d70666
represents the container ID.
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 ps
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:
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 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:
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.
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.
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:
20. To stop a running container from the host
session issue the following command:
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