DEV Community

vikash-agrawal
vikash-agrawal

Posted on • Updated on

Dockers Cheat Sheet

Commands:

docker run ubuntu: for the first time, it downloads the ubuntu image from the docker hub and run it.
docker ps: it lists down all the running containers.
docker ps –a: it lists down all the containers including previously stopped images.
docker stop container_id/container_name: it stops the running docker.
docker rm container_id/container_name: it removes the already stopped docker and hence docker ps –a will not list it. This takes multiple container_id/container_name separated by space.
docker exec conatianer_name command: it will execute the command inside the docker conatiner_name.
If no command is passed while running the docker, it exited immediately. So after we execute “docker run centos”, the command “docker ps” will not list any active docker. But after we execute “docker run -d centos sleep 2000”, the command “docker ps” will list active docker for 2000 seconds. And then docker stop container_id/conainer_name will stop this docker. With the value of exited code from status of “docker ps -a” will explain the mode of shutdown.
docker run image:tag: it pulls the specific tag from the docker hub and run it.
Docker runs in 2 mode:

• Attach: default mode. In this mode, you can’t come out to your docker terminal, to come out to docker terminal, you need to open another terminal and stop this docker.
• Detach: to run docker in detached mode, you need to append –d to run “docker run –d ubuntu” and to attach it, run “docker attach conatiner_name”
Enter fullscreen mode Exit fullscreen mode

interactive mode: If your application needs to be run in interactive mode, you need to append ‘-i', e.g. “docker run -i image”
docker inspect “container_id”: it will help you with all the detail of the docker including internal ip.
docker tag imageid login-id/imagename
Mapping

• Port mapping: the system where docker is installed is called docker host. When the container is run, this particular docker gets internal ip, which is not accessible from outside. That means the particular application (e.g. web, mysql etc) can be accessible from within the docker host itself. To access the application from outside, the application port of this docker ip has to be mapped with the port of the docker host ip using “-p” command e.g. “docker run –p 3306:3306 mysql”
• Volume Mapping: any docker image, let’s say DB is run, it will have the volume present inside the docker container, once this particular docker container is stopped and removed, the data will also be lost. To save the data, volume mapping option with “-v” command can be used e.g. “docker run –v /data/:/var/lib mysql”
Enter fullscreen mode Exit fullscreen mode

docker images: it lists down all the downloaded images.
docker rmi ubuntu: it removes ubuntu images but before removing, you need to make sure that no instance of docker exists either in running (docker ps) or exit state (docker ps –a).
docker pull ubuntu: it will just pull the docker image from the docker hub.

DOCKERFILE

It contains 2 parts:

• Command: FROM RUN COPY ENTRYPOINT
• Instructions: the commands to be passed to the “command”
building docker works on layer and if any of the steps fails, it starts building from the failed step. 
Enter fullscreen mode Exit fullscreen mode

docker history image_name: this command will list the detail of all the steps including the size.
Best way to define docker file:

• Run the docker image for the given OS (which will be input for FROM)
• Perform all the steps.
• Now open docker file and put the OS name with FROM tag (from step 1).
• Put all the steps from step 2 as individual “RUN”
• Save the docker file.
• Build the docker file as “docker build .”, but this will give the container id. Better to build with “docker build . Org-id/container-id”
Enter fullscreen mode Exit fullscreen mode

DOCKER COMPOSE YML FILE

• Docker-compose.yml file: it is used to simulate the actual production environment where we can combine more than 1 docker in it.
Services:
Web:
images: “simple-webapp”
ports:
-”80:5000”
Database:
images: “mysql”
volumes:
-”/opt/data:/var/lib/mysql”
• Command:
o docker-compose up: bring up the application.
o docker-compose stop: stop all the application
o docker-compose down: Remove all the containers.

Docker swarm

It provides the manager to manage the workers/nodes. Manager is the one, which will have docker-compose.yml file deployed and based on the instructions under deploy, it will have the nodes associated with it.

Network:

• Bridge N/W:

o   Docker run ubuntu
o   by default, all the docker images run in a bridge network.
o   In this, each container can access the other container using internal ip.
o   For the container to be accessible outside the host, port has to be mapped.
o   The ips will be in the range of 172.17.0.X
Enter fullscreen mode Exit fullscreen mode

• None

o   Docker run ubuntu –network=none
o   It's not accessible from any container.
Enter fullscreen mode Exit fullscreen mode

• Host:

o   Docker run ubuntu –network=host
o   The port, if any, runs on the host network.
o   Means any images, which runs on any port, can be run only once.
Enter fullscreen mode Exit fullscreen mode

• User Defined Network

o   User can define the network like 182.18.0.X
o   Docker network create –driver bridge –subnet 182.18.0.0./16 custom-isolated-network
Enter fullscreen mode Exit fullscreen mode

CMD vs ENTRYPOINT:

• CMD is the once which just gets appended when docker invokes for run (like docker run ubuntu sleep 10)
• ENTRYPOINT allows the parameter to be passed so if you have ENTRYPOINT=["sleep”] and invoke the docker for run and pass 10 then this 10 will be parameter for sleep
• As mentioned CMD gets appended while docker gets invoked to run so CMD can be used as to provide the default value so means if DOCKERFILE has ENTRYPOINT ["sleep”] and CMD [“5”] then if no parameter is passed then sleep 5 is the one which gets executed and if any parameter is passed the this particular value will override 5.
• You can override ENTRYPOINT also using “docker run –entrypoint sleep2

YAML

• Like XML and JSON, YAML is also data representation.
• It contains Dictionary, which is nothing but the map.
• Dictionary can contain another dictionary.
• There can be a List of Dictionary, which is represented by ‘-’.
• In the dictionary, there should be a space after ‘:’.
• The space before the keys plays an important role.
• Any extra space might led to change in the compilation failure or change in hirearchy.
• Dictionary is unordered means the value matters not the order.
Banana:
Calories: 105
Fat: 0.4kg
Carns: 27g
Banana:
Calories: 105
Fat: 0.4kg
Carns: 27g
Means both the banana are same.
• List is an ordered item:
Fruits:

  • Orange
  • Apple
  • Banana Fruits:
  • Orange
  • Banana
  • Apple Both the fruits are different. • Any line in yaml file starting with ‘#’ is considered as a comment.

Top comments (0)