How
do I install and setup Docker container on an RHEL 7 (Red Hat
Enterprise Linux) server? How can I setup Docker on a CentOS 7? How to
install and use Docker CE on a CentOS Linux 7 server?
Docker is free and open-source software. It automates the deployment of
any application as a lightweight, portable, self-sufficient container
that will run virtually anywhere. Typically you develop software on your
laptop/desktop. You can build a container with your app, and it can
test run on your computer. It will scale in cloud, VM, VPS, bare-metal
and more. There are two versions of docker. The first one bundled with
RHEL/CentOS 7 distro and can be installed with the yum. The second
version distributed by the Docker project called docker-ce (community
free version) and can be installed by the official Docker project repo.
The third version distributed by the Docker project called docker-ee
(Enterprise paid version) and can be installed by the official Docker
project repo. This page shows how to install, setup and use Docker or Docker CE on RHEL 7 or CentOS 7 server and create your first container.
How to install and use Docker on RHEL 7 or CentOS 7 (method 1)
The procedure to install Docker is as follows:
Open the terminal application or login to the remote box using ssh command: ssh user@remote-server-name
Type the following command to install Docker via yum provided by Red Hat: sudo yum install docker
Type the following command to install the latest version of Docker CE (community edition): sudo yum remove docker docker-common docker-selinux docker-engine sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum install docker-ce
Let us see all info in details along with examples.
How to install Docker on CentOS 7 / RHEL 7 using yum
Type the following yum command: $ sudo yum install docker
How to install Docker CE on CentOS 7 (method 2)
First remove older version of docker (if any): $ sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce Next install needed packages: $ sudo yum install -y yum-utils device-mapper-persistent-data lvm2 Configure the docker-ce repo: $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
How to find out info about Docker network bridge and IP addresses
Default network bridge named as docker0 and is assigned with an IP address. To find this info run the following ip command: $ ip a $ ip a list docker0 Sample outputs:
3: docker0: mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:cd:c0:6d:4a brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
How to run docker commands
The syntax is: docker command docker command arg docker [options] command arg docker help | more
Get system-wide information about Docker
docker info
Getting help
docker help | more Sample outputs: Run 'docker COMMAND --help' for more information on a command: docker ps --help docker cp --help
How to test your docker installation
Docker
images are pulled from docker cloud/hub such as docker.io or
registry.access.redhat.com and so on. Type the following command to
verify that your installation working: docker run hello-world Sample outputs:
How to search for Docker images
Now
you have working Docker setup. It is time to find out images. You can
find images for all sort of open source projects and Linux
distributions. To search the Docker Hub/cloud for nginx image run: docker search nginx Sample outputs: Click to enlarge
How to install Docker nginx image
To pull an image named nginx from a registry, run: docker pull nginx Sample outputs:
How to run Docker nginx image
Now you pulled image, it is time to run it: docker run --name my-nginx-c1 --detach nginx Say you want to host simple static file hosted in /home/vivek/html/ using nginx container: docker run --name my-nginx-c2 -p 80:80 -v /home/vivek/html/:/usr/share/nginx/html:ro -d nginx Where,
--name my-nginx-c1 : Assign a name to the container
--detach : Run container in background and print container ID
-v /home/vivek/html/:/usr/share/nginx/html:ro : Bind mount a volume
-p 80:80 : Publish a container's port(s) to the host i.e redirect all traffic coming to port 80 to container traffic
Go ahead and create a file named index.html in /home/vivek/html/: echo 'Welcome. I am Nginx server locked inside Docker' > /home/vivek/html/index.html Test it: curl http://your-host-ip-address/ curl 192.168.122.188 Sample outputs:
Welcome. I am Nginx server locked inside Docker
How to list running Docker containers
docker ps docker ps -a Sample outputs:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb9d85a56a92 nginx "nginx -g 'daemon of…"55 seconds ago Up 54 seconds 0.0.0.0:80->80/tcp my-nginx-c2
fe0cdbc0225a nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp my-nginx-c1
You can use CONTAINER ID to stop, pause or login into the container.
How to run a command in a running container
Run ls /etc/nginx command for my-nginx-c1 container docker exec fe0cdbc0225a ls /etc/nginx OR docker exec my-nginx-c1 ls /etc/nginx Want to gain bash shell for a running container and make changes to nginx image? docker exec -i -t fe0cdbc0225a bash OR docker exec -i -t my-nginx-c1 bash
How to stop running containers
docker stop my-nginx-c1 OR docker stopfe0cdbc0225a
How to remove docker containers
docker rm my-nginx-c1 docker ps -a And there you have it, Docker installed and running on a CentOS 7 or RHEL 7 server. For more info see the following resources:
No comments:
Post a Comment