How to install and setup Docker on RHEL 7
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
How to install Docker CE on CentOS 7 (method 2)
First remove older version of docker (if any):
Next install needed packages:
Configure the docker-ce repo:
$ 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
Finally install docker-ce:
$ sudo yum install docker-ce
How to enable docker service
$ sudo systemctl enable docker.service
Sample outputs:
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
How to start/stop/restart docker service on CentOS7/RHEL7
$ sudo systemctl start docker.service ## <-- Start docker ##
$ sudo systemctl stop docker.service ## <-- Stop docker ##
$ sudo systemctl restart docker.service ## <-- Restart docker ##
$ sudo systemctl status docker.service ## <-- Get status of docker ##
Sample outputs:
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:
Sample outputs:
$ ip a
$ ip a list docker0
Sample outputs:
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:
Sample outputs:
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:
Sample outputs:
docker search nginx
Sample outputs:
How to install Docker nginx image
To pull an image named nginx from a registry, run:
Sample outputs:
docker pull nginx
Sample outputs:
How to run Docker nginx image
Now you pulled image, it is time to run it:
Say you want to host simple static file hosted in /home/vivek/html/ using nginx container:
Where,
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/:
Test it:
Sample outputs:
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:
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
OR
Want to gain bash shell for a running container and make changes to nginx image?
OR
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
No comments:
Post a Comment