In today’s market docker is vastly used to deploy applications on cloud, personal servers. It is an open source containerization technology, which containerize our application so that it can be run anywhere without configuring the environment.
For practising the commands you can use ruminder/docker-react-app:v1.0 image.
When you start developing your application and created one, everything works fine on your local machine. But when you deploy it, you get errors which can be due to any number of reasons, the environment does not have required libraries or the version is incompatible with it, or getting the errors from the system as the application could not find environment variables. A lot of time is wasted in deploying your application fixing the errors. When you want to deploy it again, you have to do it all over again. That is where docker comes in. Docker creates the environment inside the container with the required libraries, specific version dependencies and runtime variables. Once the docker image of the application has been created you can deploy it anywhere, any number of times.
Understanding how docker works
Docker creates an environment which shares the linux kernel. The fedora, ubuntu, kali linux are all based on the linux on which they install their own softwares (flavour). Docker share the kernel and run any OS, application on it, until it can use the same kernel. In windows which is not a linux based kernel, docker uses virtual OS underline to run container. Docker packages the application and creates image of the application. From the images we create containers. Container is just a running instance of the image/application.
Before starting with commands install the docker desktop app if you are using Mac or Windows else run the following commands for all linux versions.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
docker -v : To check version of docker installed
docker pull image-name : To download the image to local.
docker run image-name : To run the docker image. If not found in current directory it will check docker-hub for public image with given name. Will download it and run the application inside container.
docker ps : To view all the list of running container. To view both container running and stopped add “-a” to it. To know about specific container run: docker inspect container-name
docker stop container-name : To stop the container, you can either use container name or id which you can get from command docker ps.
docker rm container-name: After stopping the container, it is not removed from the system, it still occupies the space. To remove it run this command and to verify use docker ps -a , it should not be present after running this command.
Tags are used to bind the image to versions. We provided version name as tags to the images. By default, we pull latest image from dockerhub, which get the “latest” tag attached to it. To download specific version use docker pull image-name:tag-name. While running you need to provide the tag name with image name.
By default application run in attach mode, meaning the output of the running application is printed on our logs, but we cannot pass anything to the application. To pass parameters to the running container, we will need to run the image in interactive mode which can be set by using -i parameter in the command. To link container terminal, add t as well.
Any images run in the docker which is hosted on the docker host (Engine). The port on which application is running is internal to the docker host, which means you cannot access the application from the browser. Think of it as AWS environment, where you created an EC2 machine or elastic beanstalk. You cannot access it from the command line or putty of your machine until you make changes to security groups(allowing any ip to access). Similar to that, here we need to link internal port of the container to docker host port. We can now use the ip and port of docker to access out applicaiton. Add -p docker-port:interal-port to the running command. In below we can access webapp from 80 port no using http://localhost:8080
docker run -p 8080:5000 webapp
Data created by application is present inside the container only. Once container deleted, data is also deleted. To persist data outside of the container we can link the local folder path to path inside the container by using -v with folder paths while running the image, like incase of running mysql or storing logs of the web application.
docker run -v /Users/shared:/var/lib/mysql mysql
Lifeline of Container
Container is in active state until the application inside it is running. Container are meant to run the application with the ecosystem provided by it. If our application stops or crashes, container is also stopped. It is no longer in active state.
I hope that you have understand the reasons of the popularity of the docker and how you can use the images of the docker. The vastly used software images are available of docker like nginx, mysql, zookeper and others. For any doubts drop the message in comment section. In my next post, I will explain how we can dockerize our application and push them to docker hub. Thanks for reading the post.