{"id":255,"date":"2024-12-06T18:30:00","date_gmt":"2024-12-06T18:30:00","guid":{"rendered":"https:\/\/imcodinggenius.com\/?p=255"},"modified":"2024-12-06T18:30:00","modified_gmt":"2024-12-06T18:30:00","slug":"building-your-first-container-with-a-helloworld-image-using-docker-cli","status":"publish","type":"post","link":"https:\/\/imcodinggenius.com\/?p=255","title":{"rendered":"Building Your First Container with a HelloWorld Image using Docker CLI"},"content":{"rendered":"<h3>1. What is Docker CLI?<\/h3>\n<p>Docker is an open-source platform, and it is used to containerize the application. It packages the application along with the dependencies into the containers, which is easier to deploy.<\/p>\n<p>Docker CLI is a command-line interface for Docker that helps to build images and start and stop the containers from the command line.<\/p>\n<h3>2. Docker CLI commands help<\/h3>\n<p>a. Open the terminal or PowerShell.<\/p>\n<p>b. Run the command \u2018docker \u2013help\u2019 to get help on Docker CLI commands.<\/p>\n<p>\nFig 1: Docker Help<\/p>\n<p>c. Run the command \u2018docker network \u2013help\u2019 to get the command-specific help.<\/p>\n<p>\nFig 2: Docker command help<\/p>\n<p>d. Run the command \u2018docker network create \u2013help\u2019 to know the options for the specific command method.<\/p>\n<p>\nFig 3: Docker command method options help<\/p>\n<h3>3. Create a docker container from CLI (Method 1):<\/h3>\n<h4>3.1. Create a Docker container from \u2018Hello world\u2019 image<\/h4>\n<p>a. Run the command \u2018docker container create imagename\u2019 to create a docker container from the \u2018Hello world\u2019 image.<\/p>\n<p>b. Example: docker container create hello-world:latest<\/p>\n<p>c. It will check for the hello-world image locally and start the container if it is found; otherwise, it downloads from the Docker Hub.<\/p>\n<p>\nFig 4: Create Docker Container from &#8216;Hello world&#8217; image<\/p>\n<h4>3.2. List the Docker Containers<\/h4>\n<p>a. Run the command \u2018docker ps\u2019 to list the active containers. The following images don\u2019t show our container because it will list only the active containers.<\/p>\n<p>\nFig 5: Docker ps<\/p>\n<p>b. Run the command \u2018docker ps \u2013list\u2019 to list all the containers, both active and inactive.<\/p>\n<p>\nFig 6: Docker ps all<\/p>\n<h4>3.3. Start the container<\/h4>\n<p>a. Run the command \u2018docker start container \u2018 to start the containers.<\/p>\n<p>  Example: Run the command \u2018docker start container 25c8937cabb1\u2019 to start our container.<\/p>\n<p>\nFig 7: Docker container start by container id<\/p>\n<p>b. Run the command \u2018docker ps -all\u2019 to list all the containers again.<\/p>\n<p>\nFig 8: Docker container start and exit<\/p>\n<p>c. You can see the container status, which created and exited about a minute ago message in the screenshot.<\/p>\n<h4>3.4. Docker Logs<\/h4>\n<p>a. Run the command \u2018docker logs \u2018 to view the logs of the container.<\/p>\n<p>b. Example: Run the command \u2018docker logs 25c8937cabb1\u2019 to view our container logs.<\/p>\n<p>\nFig 9: Docker container logs<\/p>\n<p>c. You can also attach the logs while starting the container.<\/p>\n<p>d. Run the command \u2018docker container start \u2013attach containerid\u2019 to attach the logs while starting the container.<\/p>\n<p>e. Example: Run the command \u2018docker container start \u2013attach 25c8937cabb1\u2019 to attach the logs while starting our container.<\/p>\n<p>\nFig 10: Docker container attach logs<\/p>\n<p>f. We can start the same container multiple times because the containers won\u2019t be deleted and can be started anytime.<\/p>\n<h3>4. Create a docker container from CLI (Method 2):<\/h3>\n<h4>4.1. Create a docker container from \u2018Hello world\u2019 image<\/h4>\n<p>a. Run the command \u2018docker run ImageName\u2019 to create a container from the given image.<\/p>\n<p>b. Example: Run the command \u2018docker run hello-world:latest\u2019 to create a container and run the image for the \u2018Hello world\u2019 latest image.<\/p>\n<p>\nFig 11: Docker run<\/p>\n<h4>4.2 Docker run<\/h4>\n<p>a. Basically, the following actions will happen with docker run:<br \/>\ndocker run = docker container create + start + attach<\/p>\n<p>b. Run the command \u2018docker ps \u2013all\u2019 to list all the docker containers to view the newly created container.<\/p>\n<p>\nFig 12: Docker ps all<\/p>\n<p>c. Run the command \u2018docker logs \u2018 to view the Docker container logs.<\/p>\n<p>d. Run the command \u2018docker logs 3380c26f2f2f\u2019 to view the newly created Docker container logs.<\/p>\n<p>\nFig 13: Docker logs by container id<\/p>","protected":false},"excerpt":{"rendered":"<p>1. What is Docker CLI? Docker is an open-source platform, and it is used to containerize the application. It packages the application along with the dependencies into the containers, which is easier to deploy. Docker CLI is a command-line interface for Docker that helps to build images and start and &#8230; <\/p>\n<div><a class=\"more-link bs-book_btn\" href=\"https:\/\/imcodinggenius.com\/?p=255\">Read More<\/a><\/div>\n","protected":false},"author":0,"featured_media":256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/255"}],"collection":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255"}],"version-history":[{"count":0,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/255\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}