When i heard about docker first, i thought it is a user created application, but it turned out to be a platform, Client-Server container management platform.
Docker allows you to package an application with its environment and all of its dependencies into a "box", called a container. Usually, a container consists of an application running in a stripped-to-basics version of a Linux operating system. An image is the blueprint for a container, a container is a running instance of an image.
Lets create a demo docker container of existing "rhscl/httpd-24-rhel7" image ,
docker run --name demo-container -d rhscl/httpd-24-rhel7
This command does two things,
Docker allows you to package an application with its environment and all of its dependencies into a "box", called a container. Usually, a container consists of an application running in a stripped-to-basics version of a Linux operating system. An image is the blueprint for a container, a container is a running instance of an image.
Lets create a demo docker container of existing "rhscl/httpd-24-rhel7" image ,
docker run --name demo-container -d rhscl/httpd-24-rhel7
This command does two things,
- first it search 'rhscl/httpd-24-rhel7' docker image in a local cache, if did not find, pull it from the server (Red Hat Registry, Docker Hub, or your private Container Platform Docker registry)
- After the docker image available in local cache, it creates a new instance of docker container, and run the container.
[We can create our app, and create a docker image out of it, and deploy containers as many we want.]
We can check the container status using following command,
docker ps
This command shows container id, image the container based of off, status of all the running containers including recently created 'demo-container' container.
For some of the container image requires some environment variables to be passed to start, that's why the container may not run with the docker run command.
For example, docker run --name mysql-database-container rhscl/mysql-56-rhel7
The parameter need to be passed are written in container image logs and that can be seen with docker logs mysql-database-container command. You can start and run container with,
docker run --name mysql-database-container -e MYSQL_USER=user1 -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=employee -e MYSQL_ROOT_PASSWORD=rootpassword -d rhscl/mysql-56-rhel7
For some of the container image requires some environment variables to be passed to start, that's why the container may not run with the docker run command.
For example, docker run --name mysql-database-container rhscl/mysql-56-rhel7
The parameter need to be passed are written in container image logs and that can be seen with docker logs mysql-database-container command. You can start and run container with,
docker run --name mysql-database-container -e MYSQL_USER=user1 -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=employee -e MYSQL_ROOT_PASSWORD=rootpassword -d rhscl/mysql-56-rhel7
If you like to stop the container, you can use,
docker stop demo-container
Now you can not see stopped 'demo-cotnainer' container using docker ps command. But, if you like to see all the container including the stopped one, use 'docker ps -a' command, the stopped container will be shown in the list with status 'Exited'.
You can try, creating another container with our previous command,
docker run --name demo-container -d rhscl/httpd-24-rhel7
It will fail to create a new container, because there is already a stopped container with name demo-container. Docker preserves a container until you explicitly remove the container with docker rm command,
Lets create another container with different name,
docker run --name demo-container1 -d rhscl/httpd-24-rhel7
It will create a container of name 'demo-container1' using a docker rhscl/httpd-24-rhel7 image same as earlier.
If you like to see all sorts of information of running as well as stopped docker container, you can use,
docker inspect demo-container1
This will give all the information ( environment, host, network etc. ) about the container in big json format.
If you like to get the specific information from json for example ip address of the container, you can use
docker inspect -f '{{ .NetworkSettings.IPAddress }}' demo-container1
Now, if you like to access bash shell and run some command inside a running container, you can use
docker exec -it demo-container1 /bin/bash
It will show you a bash shell inside the container.
Let's create a html file inside a container and load it to the browser using the rhscl/httpd-24-rhel7 web server,
inside a bash shell, type this comamnd : echo htmlText > /opt/rh/httpd24/root/var/www/html/index.html
This will create index.html with 'htmlText or any text you like' text file inside www/html directory. You can exit from bash, using : exit
You can curl the html file using ip address ( run docker inspect for ipaddress ) and html file url : curl 172.17.0.9:8080/index.html . You will see the html content inside a index.html file.
File created inside a container remains inside when the container stopped and restarted.
docker stop demo-container1
docker restart demo-container1
And, curl the url : curl 172.17.0.9:8080/index.html
File should still be available. But, for new container the url does not work as no file is created inside a container.
If you like to kill or stop the running container, you can use,
docker kill demo-container1
Container is stopped, but it will still be there. You can use stopped container for debugging or analyzing the container before completely removing it.
Use following command for removing the container,
docker rm demo-container1
If you wish to remove all of the container,
docker rm $(docker ps -aq )
To verify all the container is removed, use
docker ps -a
Above one was for removing the container. If you wish to remove the image stored on a local cache, you can use,
docker rmi rhscl/httpd-24-rhel7
Referneces :
1. https://nodejs.org/de/docs/guides/nodejs-docker-webapp/
Referneces :
1. https://nodejs.org/de/docs/guides/nodejs-docker-webapp/
Comments
Post a Comment