What is Docker in DevOps
1. Introduction
In this article, we will explain What is Docker using detailed DevOps examples.
2. Docker in Devops
It is used in DevOps as a software package to create containers for deployment. It helps in handling changes in code, requirements, operating systems, and cloud environment.
2.1 Prerequisites
Docker
software is required. Apache Tomcat image is obtained from Docker Hub
.
2.2 Download
You can download Docker
from the site .
2.3 Setup for Docker
Below is the setup check commands after installing the docker package
.
You can check the docker installation by running the hello-world docker repo which is located at the docker hub.
Docker Hello-World
docker run hello-world
The output of the above command will be as below:
Output
Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
2.4 Docker
You can create your own Repo and images. Dockerfile details are shown below:
Dockerfile
FROM busybox CMD echo "Greetings! Docker image is created."
The command to create docker
is shown below:
Docker Repo creation
docker build -t bhagvanarch/docker-repo .
The output of the above command will be as below:
Docker repo execution
Sending build context to Docker daemon 2.048kB Step 1/2 : FROM busybox latest: Pulling from library/busybox 76df9210b28c: Pull complete Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209 Status: Downloaded newer image for busybox:latest ---> 1c35c4412082 Step 2/2 : CMD echo "Greetings! Docker image is created." ---> Running in bc9f0d8ac3d7 Removing intermediate container bc9f0d8ac3d7 ---> 166dae6f16f8 Successfully built 166dae6f16f8 Successfully tagged bhagvanarch/docker-repo:latest
You can test the docker
image created by using the command below:
Docker Testing
docker run bhagvanarch/docker-repo
The output of the above command will be as below:
Docker Testing
Greetings! Docker image is created.
You can submit the image to the repo by pushing using the command below:
Docker Repo Push
docker push bhagvanarch/docker-repo
The output of the above command will be as below:
Push Command Output
The push refers to repository [docker.io/bhagvanarch/docker-repo] 1be74353c3d0: Mounted from library/busybox latest: digest: sha256:dec937fd6c36b20991e3e51147d3e4553d5d7ec9d0d70627a4f2cd9c48153be5 size: 527
2.5 Devops
DevOps is the automation of development, testing, and deployment processes in software engineering. It helps in continuous delivery, faster issue resolution, and cutting down the maintenance challenges. DevOps software helps in creating features quickly and delivering them. It helps in improving the communication and collaboration between teams. DevOps software tools such as Ansible, Jenkins, Puppet, GitLab, Vagrant, and Chef. Docker
is another popular Devops software tool.
2.6 Docker usage in Devops
Docker
solves the issues related to development and operations. It is becoming a popular DevOps software package. Docker
helps in creating development, testing, staging, and production environments to handle the changes in the software. Feature testing and functioning are enabled by the Docker Tool
.
The base Docker image
is created. Multi-stage builds can be modeled as the layers in the docker image
. The base image can be extended by having common components and added components. Docker images
can be tagged for different versions of releases. Docker images
can be enabled for debugging and monitoring.
You can start creating a docker image for a sample web app. The Dockerfile details are provided below:
Dockerfile
FROM tomcat:8.0-alpine LABEL maintainer="bhagvanarch@gmail.com" ADD sample.war /usr/local/tomcat/webapps/ EXPOSE 8080 CMD ["catalina.sh", "run"]
You can start creating a web app docker
image by using the command below:
Docker build command
docker build -t webapp .
The output of the above command will be as below:
Docker build output
Sending build context to Docker daemon 7.168kB Step 1/5 : FROM tomcat:8.0-alpine 8.0-alpine: Pulling from library/tomcat 4fe2ade4980c: Pull complete 6fc58a8d4ae4: Pull complete 7d9bd64c803b: Pull complete a22aedc5ac11: Pull complete 5bde63ae3587: Pull complete 69cb0c9b940a: Pull complete Digest: sha256:d02a16c0147fcae13d812fa670a4b3c9944f5328b10a5a463ad697d2aa5bb063 Status: Downloaded newer image for tomcat:8.0-alpine ---> 624fb61775c3 Step 2/5 : LABEL maintainer="bhagvanarch@gmail.com" ---> Running in 25fa21fa57f2 Removing intermediate container 25fa21fa57f2 ---> a4d46b9470b0 Step 3/5 : ADD sample.war /usr/local/tomcat/webapps/ ---> 69fe25e523b8 Step 4/5 : EXPOSE 8080 ---> Running in 5e392e84036b Removing intermediate container 5e392e84036b ---> fd7d13957b79 Step 5/5 : CMD ["catalina.sh", "run"] ---> Running in cd3b3f4d3ef0 Removing intermediate container cd3b3f4d3ef0 ---> db71db63b811 Successfully built db71db63b811 Successfully tagged webapp:latest
You can run the docker
based web app by using the command below:
Docker WebApp Run Command
docker run -p 80:8080 webapp
The output of the above command will be as below:
Web app Command Output
05-Jun-2020 18:56:02.032 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53 05-Jun-2020 18:56:02.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC 05-Jun-2020 18:56:02.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0 05-Jun-2020 18:56:02.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 05-Jun-2020 18:56:02.063 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.9.125-linuxkit 05-Jun-2020 18:56:02.080 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 05-Jun-2020 18:56:02.080 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.7-openjdk/jre 05-Jun-2020 18:56:02.081 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01 05-Jun-2020 18:56:02.081 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 05-Jun-2020 18:56:02.081 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 05-Jun-2020 18:56:02.081 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 05-Jun-2020 18:56:02.087 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 05-Jun-2020 18:56:02.088 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 05-Jun-2020 18:56:02.088 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 05-Jun-2020 18:56:02.126 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 05-Jun-2020 18:56:02.127 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 05-Jun-2020 18:56:02.127 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 05-Jun-2020 18:56:02.128 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 05-Jun-2020 18:56:02.129 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 05-Jun-2020 18:56:02.130 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.6.3. 05-Jun-2020 18:56:02.134 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 05-Jun-2020 18:56:02.161 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2o 27 Mar 2018) 05-Jun-2020 18:56:03.279 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 05-Jun-2020 18:56:03.355 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 05-Jun-2020 18:56:03.360 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 5864 ms 05-Jun-2020 18:56:03.899 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 05-Jun-2020 18:56:03.902 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53 05-Jun-2020 18:56:04.130 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/sample.war 05-Jun-2020 18:56:09.276 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/sample.war has finished in 5,141 ms 05-Jun-2020 18:56:09.282 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager 05-Jun-2020 18:56:09.456 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 174 ms 05-Jun-2020 18:56:09.457 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs 05-Jun-2020 18:56:09.531 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 74 ms 05-Jun-2020 18:56:09.533 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples 05-Jun-2020 18:56:11.175 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 1,642 ms 05-Jun-2020 18:56:11.177 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager 05-Jun-2020 18:56:11.295 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 118 ms 05-Jun-2020 18:56:11.296 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT 05-Jun-2020 18:56:11.449 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 152 ms 05-Jun-2020 18:56:11.512 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 05-Jun-2020 18:56:11.571 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 05-Jun-2020 18:56:11.627 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 8267 ms
The tomcat server can be accessed at http://localhost. (port 80 is the default port). The output is shown below:
The sample web app bundled with tomcat can be accessed at http://localhost/sample/. The webpage output is shown in the image below:
3. Download the Source Code
You can download the full source code of this example here: What is Docker in DevOps