{"id":35537,"date":"2023-05-22T11:37:18","date_gmt":"2023-05-22T11:37:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=35537"},"modified":"2025-07-12T05:40:13","modified_gmt":"2025-07-12T05:40:13","slug":"docker-and-kubernetes-demo-project","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/docker-and-kubernetes-demo-project\/","title":{"rendered":"Docker and Kubernetes Demo Project"},"content":{"rendered":"\n<p>Here&#8217;s a step-by-step guide to creating a simple project with a web server and app server using Docker and Kubernetes on Ubuntu, ensuring that the Docker image is accessible from Kubernetes without any errors:<\/p>\n\n\n\n<p><strong>Step 1<\/strong><\/p>\n\n\n\n<p>1. Create a new directory for your project:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">mkdir web-app-project\ncd web-app-project<\/code><\/span><\/pre>\n\n\n<p><br><br>2. Create a file named Dockerfile and open it in a text editor:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Vi Dockerfile<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">FROM nginx:latest\n\nCOPY index.html \/usr\/share\/nginx\/html\/index.html<\/code><\/span><\/pre>\n\n\n<p><\/p>\n\n\n\n<p>3. Create an <code>index.html<\/code> file in the same directory and add your desired HTML content.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>Web Server<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Welcome to the Web Server!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>4. Build the Docker image:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker build -t web-server-image .<\/code><\/span><\/pre>\n\n\n<p><br><br><strong>Step 2: <\/strong><\/p>\n\n\n\n<p>Test the Docker Image<\/p>\n\n\n\n<p>1. Run a Docker container using the newly created image:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span> <span class=\"hljs-selector-tag\">run<\/span> <span class=\"hljs-selector-tag\">-d<\/span> <span class=\"hljs-selector-tag\">-p<\/span> 8080<span class=\"hljs-selector-pseudo\">:80<\/span> <span class=\"hljs-selector-tag\">web-server-image<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2. Access the web server by opening your web browser and visiting <code>http:\/\/localhost:8080<\/code>. You should see the web server content you defined in the <code>index.html<\/code> file.<\/p>\n\n\n\n<p>3. Stop the Docker container:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">docker stop <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">container-id<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>To push an image to Docker Hub registry, you can follow these steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tag the image with the Docker Hub repository name: Before pushing the image, you need to tag it with the Docker Hub repository name. The repository name should follow the format: <code>&lt;username&gt;\/&lt;repository-name&gt;<\/code><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">docker tag <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">image-name<\/span>&gt;<\/span> <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">username<\/span>&gt;<\/span>\/<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">repository-name<\/span>&gt;<\/span>:<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tag<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Replace <code>&lt;image-name&gt;<\/code> with the name of the image you built, <code>&lt;username&gt;<\/code> with your Docker Hub username, <code>&lt;repository-name&gt;<\/code> with the desired name for your repository, and <code>&lt;tag&gt;<\/code> with the version or tag you want to assign to the image.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Log in to Docker Hub: Authenticate yourself with your Docker Hub account to gain access to your repositories.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker login<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Push the image to Docker Hub: Once you&#8217;re logged in, you can push the tagged image to Docker Hub.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">docker push <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">username<\/span>&gt;<\/span>\/<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">repository-name<\/span>&gt;<\/span>:<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tag<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n\n\n\n<li>Replace &lt;username&gt;\/&lt;repository-name&gt;:&lt;tag&gt; with the same repository name and tag you used in the previous step.<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>The Docker image will be pushed to Docker Hub, and you&#8217;ll see progress information during the push process.<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>Verify the pushed image: Go to the Docker Hub website (https:\/\/hub.docker.com\/) and log in with your Docker Hub account. Navigate to your repository, and you should see the pushed image listed with the specified tag.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker tag app-server jami12345\/project_1:app-server<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker push jami12345\/project_1:app-server<\/code><\/span><\/pre>\n\n\n<p><strong>or <\/strong><\/p>\n\n\n\n<p><strong>During Build Use below steps:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker build -t jami12345\/guestbook-app:latest .\ndocker push jami12345\/guestbook-app:latest<\/code><\/span><\/pre>\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 3: <\/strong><\/p>\n\n\n\n<p>Set Up Kubernetes Cluster<\/p>\n\n\n\n<p>Install and set up a Kubernetes cluster on your Ubuntu machine. You can use Minikube for a local cluster setup or set up a cluster on a cloud provider like AWS, Azure, or GCP. Follow the respective documentation for your chosen method.<\/p>\n\n\n\n<p><strong>Step 4: <\/strong><\/p>\n\n\n\n<p>Deploy the Web Server and App Server<\/p>\n\n\n\n<p>1. Create a file named <code>web-server-deployment.yaml<\/code> and define the Kubernetes Deployment for the web server:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\nname: web-server-deployment\nspec:\nreplicas: 1\nselector:\nmatchLabels:\napp: web-server\ntemplate:\nmetadata:\nlabels:\napp: web-server\nspec:\ncontainers:\n- name: web-server\nimage: jami12345\/project_1:web-server \nports:\n- containerPort: 80<\/code><\/span><\/pre>\n\n\n<p><br><br>2. Apply the Kubernetes deployment configuration to deploy the web server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">kubectl<\/span> <span class=\"hljs-selector-tag\">apply<\/span> <span class=\"hljs-selector-tag\">-f<\/span> <span class=\"hljs-selector-tag\">web-server-deployment<\/span><span class=\"hljs-selector-class\">.yaml<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>3. Verify that the deployment is created:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl <span class=\"hljs-keyword\">get<\/span> deployments<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>4. Create a file named <code>web-server-service.yaml<\/code> and define the Kubernetes Service for the web server:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: v1\nkind: Service\nmetadata:\nname: web-server-service\nspec:\nselector:\napp: web-server\nports:\n- protocol: TCP\nport: 80\ntargetPort: 80\ntype: Nodeport<\/code><\/span><\/pre>\n\n\n<p>5. Apply the Kubernetes service configuration to expose the web server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">kubectl<\/span> <span class=\"hljs-selector-tag\">apply<\/span> <span class=\"hljs-selector-tag\">-f<\/span> <span class=\"hljs-selector-tag\">web-server-service<\/span><span class=\"hljs-selector-class\">.yaml<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>6. Verify that the service is created:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl <span class=\"hljs-keyword\">get<\/span> services<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>7. Access the web server:<\/p>\n\n\n\n<p>Look for the <code>service<\/code> entry and note the assigned <code>PORT<\/code> number. You can access the Guestbook application by visiting <code>http:\/\/&lt;NodeIP&gt;:&lt;PORT&gt;<\/code> in your web browser.<\/p>\n\n\n\n<p>Note: &#8211; To change service type to Node port<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl patch service app-server-service -p <span class=\"hljs-string\">'{\"spec\": {\"type\": \"NodePort\"}}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>or<\/p>\n\n\n\n<p><strong>Step 8: <\/strong><\/p>\n\n\n\n<p>Access the application<\/p>\n\n\n\n<p>To access the application, you can use port forwarding. Run the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">kubectl port-forward service\/web-app-service 8080:80<\/code><\/span><\/pre>\n\n\n<p>To access the application, you can use port forwarding. Run the following command:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a step-by-step guide to creating a simple project with a web server and app server using Docker and Kubernetes on Ubuntu, ensuring that the Docker image is accessible from&#8230; <\/p>\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862,4859],"tags":[],"class_list":["post-35537","post","type-post","status-publish","format-standard","hentry","category-docker","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=35537"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35537\/revisions"}],"predecessor-version":[{"id":35548,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35537\/revisions\/35548"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=35537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=35537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=35537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}