Docker in nutshell

  • Download Install Docker desktop

Go to terminal and run

docker info

docker images

docker ps -a

Exit or control D

  • Docker runs an Image and make it a container. HOW? Follow below:

To run an image in container:

Docker run -ti ubuntu:latest bash

  • To Create an Image from a container. This will save all your files inside the container.

Docker commit <containerID>

Gives image number

  • To create a tag:

Docker tag <Image number> my-image

  • To run my-image:

Docker run -it my-image bash

  • To create new image from your container:

Docker commit <container NAME> my-image-2

  • To Detach and attach a container:

Docker run -d -it Ubuntu bash

Docker attach <container name>

Control P , Control Q — will exit /detach you from container but container is running.

Docker exec -it <container name>

  • Docker logs:

docker run -d –name example ubuntu bash -c “lose /etc/password”

2d974118fdd1bfb0dac9c5e3079f60b46099b8cc2a78672c75b83a0d29bbe19f

docker logs example

bash: lose: command not found

  • Stop container and remove:

Docker kill <container name/id>

Docker rm

  • Docker resource utilization:

Docker run –memory 5mb my-image

Docker run –cpu-shares 50 my-image

Docker run –cpu-quota 1000 my-image

1000 ms

  • Publish port dynamically:

Docker run -rm -it -p 45678 -p 45679 –name echo-server Ubuntu bash

root@: nc -lp 45678 | nc -lp 45679

Docker port echo-server

45678/tcp -> 0.0.0.0:32777
45679/tcp -> 0.0.0.0:32779

Nc localhost 32777

Nc localhost 32779

  • Network:

Docker network ls

  • Docker network create learning

Docker run –rm –it –net learning –name catserver ubuntu bash

root@: ping catserver

Ping dogserver

Docker run –rm –it –net learning –name dogserver ubuntu bash

root@: ping catserver

On dogserver:

Nc -lp 1234

On catserver:

Nc dogserver 1234

Both can communicate together now.

  • List Images:

Docker images

  • Tags:

Docker commit <container name> my-image:v2.1

Docker pull

docker push

Docker rim image-name:Tag

  • Volumes:

Docker run -it -v /apatel/myfolder:/shared-folder Ubuntu bash

  • DockerFile:

Create docker file named Dockerfile

FROM Ubuntu

WORKDIR /install/

USER gag

RUN apt-get -y update

RUN apt-get install nano

RUN unzip install.zip /opt/install

RUN echo “Hello Docker”

ADD run.sh /run.sh

ADD project.tar.gz /install

ADD https://down.download.rpm /project/

ENV DB_HOST=db.prod.ex.com

ENV DB_PORT 5123

EXPOSE 8080

VOLUME [“/host/path”, “/container/path”]

CMD [“/bin/nano”, “/tmp/myfile”]

To create new image:

Docker build -t tagname .

Docker run –rm -ti tagname

Docker uses bridge to connect this containers and share data between them.

Docker run -ti –rm –net=host –privileged=true Ubuntu bash

Apt-get install iptables

Iptables -n -L -t not

Docker inspect –format ‘{{.State.Pid}}’ <container name>

  • COW:> Copy on Write

Docker save -o images.tar.gz Ubuntu:14.0 busybox

Docker load -I images.tar.gz

Reference: https://www.linkedin.com/learning/paths/master-cloud-native-infrastructure-with-kubernetes?u=51095537

Leave a comment