Docker 101 : Basic Commands
Category : DevOps Tag : 101series shellscript
Nov. 28, 2020, 1:21 a.m.

Short Description :
Basic commands for docker operation, such as build, run, and going into container etc.
source : datak

<h2>What is a docker?</h2><blockquote><p>Package Software into Standardized Units for Development, Shipment and Deployment:<br>A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.<br>Container images become containers at runtime and in the case of Docker containers - images become containers when they run on Docker Engine. Available for both Linux and Windows-based application, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.</p></blockquote><p>Source :&nbsp;<a href="https://www.docker.com/resources/what-container" target="_blank">What is a Container</a></p><h2><br></h2><h2>Differences against Virtual Machines?</h2><blockquote><p>Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient.</p></blockquote><p><img src="/media/django-summernote/2021-06-03/0bf1f817-c068-49e9-af74-12fa36892d4d.png" style="width: 100%;"><br></p><p>Source :&nbsp;<a href="https://www.docker.com/resources/what-container" target="_blank" style="background-color: rgb(255, 255, 255);">What is a Container</a><br></p><h2><br></h2><h2>Why docker?</h2><p>Followings are advantages</p><ul><li>With the consolidate with CICD pipeline (link), consistent CICD automation made for any language. For my case, I can use same CICD script for react, Rshiny, dash, and streamlit app.</li><li>It is a container, and it is portable does not matter unique environment at local OS or cloud differences. As long as you could run container app at your local, it could be run anywhere.</li><li>It could enable some language based app to deploy to cloud. For example, most of cloud like AWS, GCP, Digital Ocean, Cloud foundry etc. do not have Rshiny build pack so we need to prepare Rshiny server on our own. We could prepare only using docker container</li><li>Consistent environment can be made by Dockerfile, so it would not cause dependency issues.</li></ul><h2><br></h2><h2>Basic Commands</h2><h4>Basic Operations</h4><p>Build a docker</p><pre># shell script docker build .</pre><p>Build a docker and assign specific name</p><pre># shell script docker build -f &lt;docker repo name&gt;/&lt;container name&gt; .</pre><p>Run docker</p><pre style="line-height: 1.42857;"># shell script docker run &lt;image ID or specific image name&gt;</pre><p>Run docker with mapping route</p><pre style="line-height: 1.42857;"># shell script docker run -p &lt;localhost port&gt;:&lt;port inside the container&gt; &lt;image ID or image name&gt;</pre><p style="line-height: 1.42857;">Run docker with volume mount</p><pre style="line-height: 1.42857;"># shell script docker run -v &lt;outside docker directory&gt;:&lt;inside docker directory&gt; &lt;image ID or image name&gt;</pre><p>Run docker with detached mode (run container at background)<br></p><pre style="line-height: 1.42857;"># shell script docker run -d &lt;image ID or image name&gt;</pre><p>Run docker with container name assigned</p><pre style="line-height: 1.42857;"># shell script docker run --name &lt;container name&gt; &lt;image ID or image name&gt;</pre><p>Stop docker</p><pre># shell script docker stop &lt;container ID or container name&gt;</pre><p><br></p><h4>Other Operations</h4><p>Start docker</p><pre># shell script docker start &lt;container ID or container name&gt;</pre><p>Kill docker</p><pre># shell script docker kill &lt;container ID or container name&gt;</pre><p>Go inside docker (typically for a issue verification)</p><pre style="line-height: 1.42857;"># shell script docker exec -it &lt;container ID or container name&gt; bash(or sh)</pre><p>Push to docker hub</p><pre style="line-height: 1.42857;"># shell script docker push &lt;docker repo name&gt;/&lt;container name&gt;</pre><p style="line-height: 1.42857;"><br></p><h4>Maintenance</h4><p>Show running container list</p><pre style="line-height: 1.42857;"># shell script docker ps</pre><p>Show all container list</p><pre style="line-height: 1.42857;"># shell script docker ps -a(or --all)</pre><p>Show image list</p><pre style="line-height: 1.42857;"># shell script docker images</pre><p>Delete container</p><pre># shell script docker rm &lt;container id or container name&gt;</pre><p>Delete image</p><pre># shell script docker rmi &lt;image name or image id&gt;</pre><p>Remove all stopped containers, networks not used by al least one container, dangling images, and build cache</p><pre># shell script docker system prune</pre><p>See history</p><pre style="line-height: 1.42857;"># shell script docker log &lt;container ID or container name&gt;</pre><p><br></p><h2>Single container vs Multi containers</h2><p>Single container has a limitation. We only have one app and it won't work in case we would like to have multiple container running at the same time such as following case;</p><ol><li>Create DB container as well as web container</li><li>Create front end container(e.g. React) and back end/worker container(e.g. Python)</li><li>Create nginx container to manage port/load balance separating out from web container</li></ol><p>This requires `Docker-Compose` and will be documented separately</p>


<< Back to Blog Posts
Back to Home

Related Posts

Jupytor in Docker
Feb 2 2021
How to deploy jupytor in docker container
Build multiple container applicatoin
May 28 2021
Explain how to build multiple container application
CICD Pipeline Part1
Jun 1 2021
CICD explanation. Part 1 focuses on how to locally create a docker container using python streamlit
CICD Pipeline Part3
Jun 2 2021
CICD explanation. Part3 includes demo on CICD pipeline automation
CICD Pipeline Part2
Jun 1 2021
CICD explanation. Part 2 focuses on how to create CICD script using travis and setup AWS EB
CICD Pipeline Part4
Jun 2 2021
CICD explanation. Part4 includes sub-domain assignment using google domain



© DATAK 2024