Docker - Docker is both the company and platform - An open source container runtime - Cross platform desktop support - Command line tool - "Dockerfile" file format for building container images - Windows let us create both windows and linux containers   **Docker cli commands** - `docker info` : display system information - `docker version` - `docker login` : by default it will login to dockerhub   Docker running commands - `docker run [imageName]` : execute image in memory. image is not present in cache, it will be downloaded automatically. there is another flag named `--name` to give name to docker instance which will run. if we dont provide docker will generate random name. eg : `docker run --name web [imageName] ` here web is the name **imposing limits on docker instance** `docker run --memory="256m" nginx` `docker run --cpus=".5" nginx` - `docker run -d [imageName]` : to run container in background so u can still use the terminal - `docker stop [containerName]` : will stop the execution but it will still in memory - `docker kill [containerName]` : to remove container in memory which r stuck - `docker image inspect` : useful for debugging - **Note**: When we use run we create a container from the image. Also when we use flags with `run` certain configuration are set too To stop execution we can use stop command but using start we can again start it with same configuration too. ![16f5148001e1c91c62a9aed090efbc81.png](../../../../_resources/16f5148001e1c91c62a9aed090efbc81.png)   **Usage** ![346b020fe6804da78af6f91d63c6aa46.png](../../../../_resources/346b020fe6804da78af6f91d63c6aa46.png) **Attach shells** ![a357f658bd6b32ebe4584769b0f29942.png](../../../../_resources/a357f658bd6b32ebe4584769b0f29942.png) Working command `docker exec -it web3 bash` here `web3` is container name which is running in background for executing commands for background running containers `podman exec web3 bash -c "ls"` for non running containers `docker run -it --name archc8 archlinux:latest /usr/bin/bash` for non running containers so basically creating a new container `docker run -it alpine:latest sh -c "apk add --update bash && exec bash"` By using the `--rm` flag with either the docker run or docker create command, you ensure that the container is automatically removed once it stops   **to start distro and keep it running** `podman run -itd --name ubuntu1 ubuntu /bin/bash` **Clearing things up** ![72f71c083a32dcfdf87ae3ba9ed6d614.png](../../../../_resources/72f71c083a32dcfdf87ae3ba9ed6d614.png) For removing containers, they must be stopped first     ## Docker build containers - ![4fa99c7ed58f4879895649bee949120d.png](../../../../_resources/4fa99c7ed58f4879895649bee949120d.png) - `docker build -t ngthml:1.0 dockers/` (dockers folder contain Dockerfile) - If Dockerfile name is not DockerFile then specify that name with `-f` flag. - Eg of dockerfile ```text FROM nginx:alpine COPY . /usr/share/nginx/html ``` FROM command specify base image and when we build something we use image which already exist. nginx:alpine is very lightweight then copy whole folder containing Dockerfile to `/usr/share/nginx/html` of alpine - Nodejs Dockerfile alpine is very lightweight and there isnt even bash installed so we cant have interactive shell too. But we can install bash by `docker exec -it node_hello apk add --update bash` or including it in `RUN apk add --update nodejs npm bash` ```text FROM alpine RUN apk add --update nodejs npm COPY . /src WORKDIR /src RUN npm install EXPOSE 8080 ENTRYPOINT ["node", "./app.js"] ``` - RUN apk .... : Using RUN we can execute commands when generating container. in this case using apk package manager of alpine we are installing nodejs and npm. Then we copy current folder to `/src` and making it as work directory too. - `WORKDIR` is a combination or cd and mkdir as we dont need to make directory too as workdir will handle it. `RUN cd` wont have effect as when using RUN commands will be run in different shell so thats why using WORKDIR is preferred WORKDIR / changes the working directory for future commands. RUN cd / has no impact on the subsequent RUNs. - EXPOSE will make sure which port shoould be accessible by other containers or host system - ENTRYPOINT means commands that will run when the container start **Difference between run,cmd and entrypoint** https://www.bmc.com/blogs/docker-cmd-vs-entrypoint/ https://www.cloudbees.com/blog/understanding-dockers-cmd-and-entrypoint-instructions - RUN. Mainly used to build images and install applications and packages, RUN builds a new layer over an existing image by committing the results. - CMD. Sets default parameters that can be overridden from the Docker Command Line Interface (CLI) when a container is running. eg : `cmd node app.js` - ENTRYPOINT. Default parameters that cannot be overridden when Docker Containers run with CLI parameters. **Example** if there is `cmd node app.js` and `docker run nodecontainer node app2.js` then in docker container it will be `node app2.js` as it will override   if there is `entrypoint ["node", "app.js"]` and `docker run nodecontainer node app2.js` then in docker container it will be ```bash node app.js node app2.js ``` instead of overriding `node app2.js` is appended - docker tag - Assign a name and tag to a specific image ID: `docker tag id name:tag` - Assign a tag to a specific image: `docker tag image:current_tag image:new_tag` - docker cp to copy files from docker container to host `docker cp :/file/path/within/container /host/path/target` eg: `docker cp node_hello_sh:/ alpine_node/`   to copy files from host to docker container `docker cp /host/path/target container_id:/file/path/within/container` eg: `docker cp foo.txt container_id:/foo.txt` - docker logs `docker logs ` or when creating container `docker run --log-level=debug ...` - Expose port of already created container While it is not possible to expose port we can access it by `http://container_ip:PORT`. we can find container ip by `docker inspect container_name | grep IPAddress` . run `docker ps` to find container name first - environment variables when starting a container from image `docker run --name my-tomcat -p 127.0.0.1:8080:8080 -e APP_DB_DB=mydb -e APP_DB_USER=dbuser -e APP_DB_PASS=dbpass --link mongo-myapp:mongo -v /path-to/tomcat/webapps:/usr/local/tomcat/webapps -d tomcat:8-jre8-alpine` - creating image from container after making some changes in container we can create its own image too `docker commit nginx_base` where nginx_base is name of container this will create image but it have no tag so crating its tag with `docker tag 0c17f0798823 hello_world_nginx` or simply add tag when creating image `docker commit nginx_base hello_world_nginx` here hello_world_nginx is name of image - docker volume `docker run -v $PWD:/data -it archivebox/archivebox init --setup` `-v $PWD:/data`: This mounts the current working directory (represented by $PWD, which is a shell variable) on your host machine to the /data directory inside the Docker container. This is crucial because it allows ArchiveBox to store its data (archived websites, indexes, etc.) persistently on your host machine, even if the container is stopped or removed. The -v flag stands for "volume." - docker import and export images https://stackoverflow.com/questions/24482822/how-to-share-my-docker-image-without-using-the-docker-hub Based on this blog, one could share a docker image without a docker registry by executing: `docker save --output latestversion-1.0.0.tar dockerregistry/latestversion:1.0.0` Once this command has been completed, one could copy the image to a server and import it as follows: `docker load --input latestversion-1.0.0.tar`