Knowledge Bank
  • My GitBook
  • Miscellaneous
  • Project
    • Rider and Intellij
    • Code
    • Frontend
      • Condition
      • AddConditionModalDialog
    • Backend
    • e2e
      • fragments
  • JAVASCRIPT
    • Promise
    • Destructuring
    • Spread Syntax and Rest Parameters
    • Typescript
      • Examples of Types
      • React Typescript
    • This
    • Dot Notation vs Bracket Notation
    • Shallow vs Deep Clone
    • New ES Edition
  • C#
    • Project Note
    • Basic
    • Shortcut and Debugging
  • Programming Paradigms
    • SOLID Principles
    • Object Oriented Programming (OOP)
      • Evolution of OOP (Procedural to OOP)
      • Instantiation
      • 4 Pillars of OOP
      • Extra
    • Functional Programming (FP)
      • Idempotent
      • Imperative vs Declarative
      • Immutability
      • High Order Function and Closure
      • Currying
      • Partial Application
      • Memoization and Caching
      • Compose and Pipe
      • Extra
      • Example of FP
    • OOP vs FP
      • Composition vs Inheritance
  • DATA STRUCTURE
    • Big O
    • Data Structure
    • Array
    • Hash Table
    • Linked List
    • Queue and Stack
    • Tree
      • Binary Heap
      • Trie
    • Graph
      • Example of Graph
  • React-Redux
    • MobX
    • Best Practices
  • Algorithms
    • Recursion
      • Examples of Recursion
    • Sorting
    • Searching and Traversal
    • Dynamic Programming
  • REFACTORING
    • Clean Code
      • Formatting
      • Error Handling
      • Concurrency
      • Testing
      • SOLID Principles
      • Classes
      • Objects and Data Structures
      • Variables
      • Functions
    • Code Smells
      • Long Function
      • Duplicate Code
      • Loops
      • Double Negative
      • Christmas Tree Code
      • Complex Condition
      • Primitive Obsession
      • Speculative Generality
      • God Class
      • Long Parameter List
  • Junior to Senior
    • AWS
      • Lambda
    • Session + Authentication
    • Redis
    • Kubernetes
      • Networking
      • Services
      • Deployment
      • Replica Set
      • YAML
      • pod-definition.yml
      • Kubectl
      • Pods
      • Fundamentals
    • Docker
      • Operating System - Extra
      • Dockerfile - Docker Image
      • Docker Storage
      • Docker Network
      • Docker Registry
      • Docker Command
      • Docker Compose
      • Docker Compose - Postgres
    • Security
      • Logging
      • HTTPS, Cross-Site-Scripting (XSS) and Cross-Site-Request-Forgery (CSRF)
      • 3rd Party Library
      • Injection
      • Code Secret, Secure Header, Access Control, Data Management, Authentication
    • CI/CD
    • SPA vs Server-Side Rendering
    • Performance
      • Optimized Code
      • Critical Render Path
      • Backend Optimization
      • Minimized Files and Images
      • Minimized Delivery
  • SECURITY
    • Encryption
    • SSH
  • Command
  • Cheatsheet
    • NPM
    • GIT
  • Writing Template
    • Guide
    • API
    • ChangeLog
    • FAQ
  • Linux
Powered by GitBook
On this page

Was this helpful?

  1. Junior to Senior
  2. Docker

Docker Command

docker run redis
// Run a container from an image with the latest version

docker run redis:4.0 
// Add a tag to run a specific version

docker run -it -d ubuntu bash

root@dc43c98b31c9:/#
// -d means will run in detach mode and let the container run in the background
// the opposite of detach is docker attach ${containerName}
// -it for interactivity (prompt and waiting for STDIN)
// And hash value that is generated randomly by docker to identify which 
// containers is which.

docker ps
// To see all the containers that are currently running in background
dockers ps -a 
// To check all running and exited containers

docker exec -it ${containerId or names} bash
// To run the running container in the background.

docker stop ${containerId or names}
// To stop container running in the background

docker rm ${containerId or names} 
// To delete exited container

docker images 
// To list of available images

docker rmi ${image name} 
// To remove images. Must ensure all dependencies are removed first

docker pull ${image name} 
// Will only pull the image and not run the container

docker inspect ${container name or container id}
// Give more detail on specific container in json format
// Can be used also to inspect Env Variable used to run the container

docker logs ${container name or container id}
// logs for container running in the background (detached mode)

docker run -e APP_COLOR=green simple-webapp-color
docker run -e APP_COLOR=blue simple-webapp-color
// -e for environment variable

docker run -p 38282:8080 --name blue-app -e APP_COLOR=blue -d kodekloud/simple-webapp
// 8080 to 38282
// name container: blue-app
// env APP_COLOR=blue
// detached mode of images kodekloud/simple-webapp
// To know the env field from within a blue-app container,
// run docker exec -it blue-app env
PORT Mapping or PORT binding or PORT forwarding

// How does users access the application via port 5000.
// What IP do users use to access in webbrowser
// Two ways:
1. One is to use the IP of the docker container. But this is internal IP that is 
only accessible within the docker container via browser.
2. Use IP provided by the docker host. For that you work, you need to do 
port mapping

docker run -p 80:3001 ${container name}
// 80 is docker host port. 3001 is docker container port. Meaning map port 3001 to
// port 80.
// The underlying host where docker is installed is called docker host or 
// docker engine
// We need to do this because Container and Host Computer is isolated from each 
// other, so we need to tell the container to expose the port.
// You can run multiple instances of the container on different port.
Data Persistance in docker container
When you run mySQL container, the data are stored inside /var/lib/mysql inside 
docker container. Remember that docker container has isolated filesystem and any 
changes to any files only happen within the container. If you remove the container,
the data inside it is also gone. To persist, you need mapping.

docker run -v /opt/datadir:/var/lib/mysql mysql
// This way when the docker container run, it mount the external directory to a 
// folder inside docker container. This way the data will be stored in the external 
// volume directory. Thus will persist even though you delete docker container.

After creating API server using Docker container, we can give this folder with Dockerfile to anyone and as long as they have Docker, they can simply run this folder on any machine that we want.

When you run an image, it exit immediately. Why? Because container is not supposed to host an operating system (like VM). It just run specific task or process such as instance of web server or application server or database, etc. Once the task is complete, the container exits. Only lives as long as the process inside it is alive.

PreviousDocker RegistryNextDocker Compose

Last updated 3 years ago

Was this helpful?