{"id":27559143,"url":"https://github.com/sandip-sadhukhan/learn_docker","last_synced_at":"2026-04-16T10:35:29.519Z","repository":{"id":287509886,"uuid":"959896340","full_name":"sandip-sadhukhan/learn_docker","owner":"sandip-sadhukhan","description":"Repo to master docker","archived":false,"fork":false,"pushed_at":"2025-04-17T07:30:45.000Z","size":446,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T23:33:08.020Z","etag":null,"topics":["docker","docker-compose","kubernetes"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sandip-sadhukhan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-03T14:23:49.000Z","updated_at":"2025-04-17T07:30:49.000Z","dependencies_parsed_at":"2025-04-12T07:21:42.494Z","dependency_job_id":"7755e5ee-c575-4279-9c74-9d643d37037a","html_url":"https://github.com/sandip-sadhukhan/learn_docker","commit_stats":null,"previous_names":["sandip-sadhukhan/learn_docker"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sandip-sadhukhan/learn_docker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandip-sadhukhan%2Flearn_docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandip-sadhukhan%2Flearn_docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandip-sadhukhan%2Flearn_docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandip-sadhukhan%2Flearn_docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sandip-sadhukhan","download_url":"https://codeload.github.com/sandip-sadhukhan/learn_docker/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandip-sadhukhan%2Flearn_docker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31882650,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T09:23:21.276Z","status":"ssl_error","status_checked_at":"2026-04-16T09:23:15.028Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","kubernetes"],"created_at":"2025-04-19T23:33:06.999Z","updated_at":"2026-04-16T10:35:29.501Z","avatar_url":"https://github.com/sandip-sadhukhan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Commands\n\n## Build a docker image\n```bash\ndocker build .\n```\n\n## Run the docker image\n```bash\ndocker run \u003ccontainer_id\u003e\n```\n\n## Run a interactive shell session\n```bash\ndocker run -it node\n```\n\n## Difference b/w `RUN` and `CMD`?\n`RUN` will run during docker image build, and `CMD` will run when container is started.\n\n## Run in detachment mode\nWhen you run a long running docker run command like `docker run -p 8000:80 \u003cimage_id\u003e`\nThen it will execute the program but you can't use that terminal.\nWe can solve this by running the command in detach mode\n\n```bash\ndocker run -p 8000:80 -d \u003cimage_id\u003e\n```\n\nIf we want to attach again with that container then we can use `attach` command\n```bash\ndocker attach \u003ccontainer_name\u003e\n```\n\nWe can see logs by `docker logs \u003ccontainer_name\u003e`\n\n## Run with std input open\n```bash\ndocker run -it \u003cimage_id\u003e\n```\n\nOr restart with attached and interactive mode\n\n```\ndocker start -ai \u003ccontainer_id\u003e\n```\n\n## Remove a docker container\n```\ndocker rm \u003ccontainer_id/name\u003e\n```\n\n## Remove a docker image\n```\ndocker rmi \u003cimage_id\u003e\n```\n\n## When container stop, then remove the container automatically\n\n```\ndocker run --rm \u003ccontainer_id\u003e\n```\n\n## Check info about a image\n```\ndocker inspect \u003cimage_id\u003e\n```\n\n## Copy files b/w local computer \u0026 docker container\n\nCopy from local computer to docker container\n```\ndocker cp dummy/. adf113dec06e:/test\n```\n\nit will copy all files under `dummy` folder exist on local computer into the container id `adf113dec06e`s' test folder.\nIt will create `test` folder if not exist.\n\nCopy from docker container to local computer\n```\ndocker cp adf113dec06e:/test dummy\n```\n\n## How to add name to docker container\nBy default docker add some random name into container. But we can specify name\nduring creating the container by `--name` attribute.\n```bash\ndocker run -p 3000:80 -d --rm --name goalsapp \u003cimage_id\u003e\n```\nNow we can use container name instead of id which is easy to work with like\n```bash\ndocker stop goalsapp\n```\n\n## What is image tag?\n`name:tag`\n\nname: Defines a group of possible more specialized, images. Example: \"node\"\ntag: Defines a specialized image within a group of images. Example: \"14\"\n\nWe can add name and tag to image by the following command:\n```bash\ndocker build -t goals:latest .\n```\nHere `goals` is name of the image and `latest` is the tag.\n\nNow we can create container on a specific tag of a image also.\n\n```bash\ndocker run -p 3000:80 -d --rm --name goalsapp goals:latest\n```\n\n## Sharing Images \u0026 Containers\nEveryone who has an image, can create containers based on the image!\n\n### Share a Dockerfile\nSimply run `docker build .`\n\nIMPORTANT: The Dockerfile instructions might need surrounding files/folders (e.g. source code)\n\n### Share a Built Image\nDownload an image, run a container based on it. No build step required, everything is included in the image already!\n\n\n### Sharing via DockerHub or Private Registry\n\n```bash\ndocker push IMAGE_NAME\ndocker pull IMAGE_NAME\n```\n\n`IMAGE_NAME` needs to be `HOST:NAME` to talk to private registry.\n\nCreate new image with updated tag so that we can push it to `DockerHub`.\n\n```bash\ndocker tag basicnodejsapp:latest sandipsadhukhan/nodejs-app:latest\n```\n\n## Docker volumes\nVolumes are folders on your host machine hard drive which are mounted (\"made available\", mapped)\ninto containers.\n\n-\u003e Volume persist if a container shuts down. If a container (re-)starts and mounts\na volume, any data inside of that volume is available in the container.\n-\u003e A container can write data into a volume and read data from it.\n\n### Types of volumes\n1. Anonymous Volume: Only exist as long as the container exist. eg: `VOLUME [\"/app/feedback\"]`\n2. Named Volume: It will exist even after container removed.\n\nDocker sets up a folder / path on your host machine, exact location is unknown\nto you as a dev. Managed via `docker volume` commands.\n\n### Creating named volume\n```bash\ndocker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback feedback-node:volumes\n```\nHere `feedback` is the name of the volume and path is container's `/app/feedback` folder\n\n## Bind Mounts (managed by us)\nWe define a folder / path on our host machine.\nWe need to add access to the folder to Docker(using docker desktop resources -\u003e file sharing option)\nSo that docker can bind folder.\n\n```bash\ndocker run -d -p 3000:80 --rm --name feedback-app -v /Users/sandip/coding/docker-notes/3-section/node_app1:/app feedback-node:volumes\n```\n\n```bash\n# Anonymous volume\ndocker run -v /app/data ...\n\n# Named volume\ndocker run -v data:/app/data ...\n\n# Bind Mount\ndocker run -v /path/to/code:/app/code ...\n```\n\nFor Bind mount, by default container can read \u0026 write volumes. If we only want\ndocker container to read only into local file system then you can use `:ro`\n```bash\ndocker run -v /path/to/code:/app/code:ro ...\n```\n\n### Create volume manually\n```bash\ndocker volume create feedback-files\n\n# Then use it\ndocker run -v feedback-files:/app/code\n```\n\n## `.dockerignore` file\nWhen we do copy for example `COPY . .`, then it will copy all files, but if we\ndon't want to copy certain files/folders, then we can write that inside `.dockerignore` file\n\n## Environments variables\nWe can set Environment variable in `Dockerfile` like `ENV PORT 80` then in your\napplication code you can use it like for nodejs `process.env.PORT`.\nAnd you can override this port from docker run command as well\n`docker run -e PORT=8000 ....`\n\nOr can create a `.env` file and write\n```\nPORT=8000\n```\nThen instead of `-e` argument you can use\n`docker run --env-file ./.env ...`\n\n## Build time arguments\nWhen we build the image we can pass some arguments. In `Dockerfile`\n```Dockerfile\nARG DEFAULT_PORT=80\n\nENV PORT $DEFAULT_PORT\n\nEXPOSE $PORT\n```\n\nWe can build 2 images one with default port 80 and another one with default port 8000\n```bash\ndocker build -t feedback-node:web-app .\n\ndocker build -t feedback-node:dev --build-arg DEFAULT_PORT=8000 .\n```\n\n## Networking\n1. Requests from container to WWW: Works fine automatically\n2. Requests from container to host machine\n3. Request from one container to another container\n\nFor number 2:\ninstead of `mongodb://localhost:27017/swfavorites` you can use\n`mongodb://host.docker.internal:27017/swfavorites`\n\nFor number 3:\nLet's say we need to connect one nodejs container into another mongodb container.\n\nFirst create a network\n`docker network create favorites-net`\n\nRun the mongodb container with this network\n`docker run -d --name mongodb --network favorites-net mongo`\n\nThen run the nodejs application in same network\n`docker run --network favorites-net ...`\n\nThen in nodejs application we can use the container name directly like\ninstead of `mongodb://localhost:27017/swfavorites` we can use\n`mongodb://mongodb:27017/swfavorites`\n\nBecause the container name is `mongodb`.\n\n\n## Docker compose\nWe have long commands to run container with all volume, network, port, config details in the commands.\n\nWhich is not maintainable, instead we can create `docker-compose.yaml` file and\nadd all the details there, then simply run `docker compose up` or `docker compose up -d` to run it.\n\n`docker compose down` command will delete containers and networks. and if we run\n`docker compose down -v` then it will also remove the volumes.\n\nIf no change in dockerfile then when you do `docker compose up` then it will not\nrebuild images everytime, but if you want to rebuild image before running containers\nthen can use `docker compose up --build` command\n\n## Utility container\nSuppose we want to create a nodejs project, then how we will run `npm init` command?\nOne option is to install node in our system, but what if version mismatch etc?\nisn't the whole point of docker that we don't have to install these version mismatch\npackages etc? Utility container can save here.\n\nWe can do `docker run -it node` then it will pull latest node(you can specify any\ntag for version) and run interactively.\n\nWe can run any command under the container like\n`docker exec -it \u003ccontainer_name\u003e npm init`\n\n### Steps\nFirst create a simple `Dockerfile`\n```dockerfile\nFROM node:14-alpine\n\nWORKDIR /app\n```\n\nthen run\n`docker build -t node-utils .`\n\nthen run the container with command\n`docker run -it -v /Users/.../app1:/app node-util npm init`\n\n### Using docker compose\nIn docker compose file\n```\nservices:\n  npm:\n    build: ./\n    stdin_open: true\n    tty: true\n    volumes:\n      - ./:/app\n```\n\nThen run\n`docker-compose run --rm npm init`\n\n\n## Development to production: Things to watch out for\n\n- Bind mounts shouldn't be used in Production!\n- Containerized apps might need a build step (e.g. React apps)\n- Multi-container projects might need to split (or should be split) across multiple hosts/remote machines\n- Trade-offs between control and responsibility might be worth it!\n\n## Bind Mounts, Volumes \u0026 COPY\n\n### In Development\n- Container should encapsulate the runtime environment but not necessarily the code.\n- Use \"Bind Mounts\" to provide your local host project files to the running container.\n- Allows for instant updates without restarting the container.\n\n### In Production\n- A container should really work standalone, you should NOT have source code on your remote machine.\n- Use `COPY` to copy a code snapshot into the image.\n- Ensures that every image runs without any extra, surrounding configuration or code.\n\n## Deploy on EC2 vs ECS\n\n### Our own Remote machine(eg: AWS EC2)\n- We need to create them, manage them, keep them updated, monitor them, scale them etc.\n- Great if you're an experienced admin/cloud expert.\n\n### Managed Remote Machines(eg: AWS ECS)\n- Creation, management, updating is handled automatically, monitoring and scaling is simplified.\n- Great if you simply want to deploy your app/containers.\n\n## Multistage build\n```dockerfile\nFROM image1\n\n# Add code\n...\n\n# Second build\nFROM image2\n...\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandip-sadhukhan%2Flearn_docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsandip-sadhukhan%2Flearn_docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandip-sadhukhan%2Flearn_docker/lists"}