DEV Community

Cover image for Getting Started With Docker
Deepak Porwal
Deepak Porwal

Posted on • Updated on

Getting Started With Docker

Introduction to Docker

Docker is a technology designed to make it easier to create, deploy, and run applications by using containers. Docker is an open platform, once we build a docker container, we can run it anywhere, say it windows, Linux, mac whether on a laptop, data center, or in the cloud. It follows the build once, run anywhere approach.

image

Installing Docker

I will not be showing you the steps to install docker here.
You can refer this.

https://dev.to/whattosay/docker-environment-setup-4hac

Docker works on a wide variety of operating systems, this includes:

  • Windows
  • Linux
  • MAC

The installation of Docker is pretty straight forward in each one of them.
https://docs.docker.com/get-docker/

Docker Containers vs Virtual Machines

  • Virtual Machine contains the entire Operating System.
  • The container uses the resource of the host operating system

Docker [Image vs Containers]

Docker Image is a file that contains all the necessary dependency and configurations which are required to run an application.

Docker Containers is basically a running instance of an image.

Docker Image Creation and Identification

When you create a Docker container, it is assigned a universally unique identifier (UUID).

These can help identify the docker container among others.
To help humans, Docker also allows us to supply container names.
By default, if we do not specify the name, docker supplies a randomly-generated name from two words, joined by an underscore
By adding --name=meaningful_name argument during the docker run command, we can specify our own name to the containers

Creating nginx Docker image and getting IPConfig

$ docker pull nginx
$ docker images
$ docker run 
$ docker run -dt -p 80:80 nginx
$ docker ps
$ apt install net-tools
$ ifconfig
Enter fullscreen mode Exit fullscreen mode

Here is a Snapshot for your reference, Do click on image to see it properly.

Docker Nginx

To check the nginx server
Goto: http://165.22.212.229:80

Here 165.22.212.229 is droplet Public IP and 80 is the port on which image has been mapped(docker run -dt -p 80:80 nginx)
By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers.
If we want containers to accept incoming connections from the world, you will have to bind it to a host port.

nginx

Port Binding

$ docker inspect mynginx
Enter fullscreen mode Exit fullscreen mode

This command shows the config details of docker container
In below Screenshot first I deleted the container by

$ docker rm 6e80b1835da72da248ae2e2d2b35045c3cd736196159a030f76bd31fb9f16f33
Enter fullscreen mode Exit fullscreen mode

then did the porting from 8000 --> 80

$ docker run -d -p 8000:80 --name mynginx nginx
Enter fullscreen mode Exit fullscreen mode

Port Binding

Attach and Detached Mode

When we start a docker container, we need to decide if we want to run in a default foreground mode or the detached mode.
You may want to use this if you want a container to run but do not want to view and follow all its output.

Detached Mode -d

$ docker run -d --name detached -p 8081:80 nginx
Enter fullscreen mode Exit fullscreen mode

detached

Removing Docker Containers

Docker containers can be removed with the help of docker container rm command.

Description Command
Remove single container docker container rm CONTAINER
Stop all the containers docker container stop $(docker container ls -aq)
Remove all the containers docker container rm $(docker container ls -aq)

Remove Images

Troubleshooting Issue

While I was removing Image I found that images were running. SO, wasn't able to remove.
Therefore, you can't remove the image until you stop.

troubleshooting

For more commands related to Docker
https://docs.docker.com/engine/reference/commandline/docker/

The cli commands were then refactored to have the form docker COMMAND SUBCOMMAND, wherein this case the COMMAND is container and the SUBCOMMAND is run

Older Approach: docker run
Newer Approach: docker container run

Both of these approaches will work perfectly.

Docker container exec

The docker container exec command runs a new command in a running container.

The command started using docker exec only runs while the container’s primary process (PID 1) is running, and it is not restarted if the container is restarted.

# here are the commands to run the commands inside container from outside 

$ docker container exec -it dporwalexec netstat -ntlp

#you can login to the container and run the commands

root@dporwal-docker:~# docker container exec -it dporwalexec bash

#here you are inside the container with the bash Interactive
root@c0efbecf71a2:/# /etc/init.d/nginx
Enter fullscreen mode Exit fullscreen mode

Here is the snapshot of above commands.

containerExecute

Importance of the IT flag

-it
Every process that we create in the linux environment , has three open files descriptions, stdin, stdout, stderr

-i --> interactive, keeps stdin open even if not attached

-t --> tty, flag allocated a paseudo-TTY

ItFlag

Default Container Command

Whenever we run a container, a default command executes which typically runs as PID 1.

defaultcontainercommand

We can override the default container command by manually specifying the command.

$ docker container run -d nginx sleep 500
Enter fullscreen mode Exit fullscreen mode

In the above command, the sleep 500 will run as a PID 1 process overriding any default command that would be present in the nginx container.

override default

Restart Docker Policies

By default, Docker containers will not start when they exit or when docker daemon is restarted.

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts.

We can specify the restart policy by using the --restart flag with docker run command

Flag Description
no do not automatically restart the container(the default)
on-failure Restart the container if it exits due to any error, which manifest as a non-zero exit code
unless-stopped Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted
always Always restart if its stopped
$ docker container run -d --restart unless-stopped nginx

Enter fullscreen mode Exit fullscreen mode

Here I have created on nginx container with unless-stopped restart command.
After restarting Docker service the container should also get start, as it will run restart command.

restartcontainer

Disk Usage montior docker containers

The docker system df command displays information regarding the amount of disk space used by the docker daemon.

Commands:

$ docker system df
$ docker system df -v
Enter fullscreen mode Exit fullscreen mode

In the below Snapshot I'm creating 500MB file using command.
I'm not sure about the below mechanism, Do let me know in discussion section if you know anything about this, how it works.

dd if=/dev/zero of=bigfile.txt bs=1M count=500
Enter fullscreen mode Exit fullscreen mode

diskUsage

Remove Docker Images

To remove one image at a time, we will use below command.

docker rmi nginx
Enter fullscreen mode Exit fullscreen mode

To remove all the docker images at once, we will use first to list all the images and then run all the commands

docker images -a -q
docker rmi $(docker images -a -q)
Enter fullscreen mode Exit fullscreen mode

remove all images

References:
Official Docker
Udemy Course

Credit:
Zeal Vora

Prev: Setting Docker Environment

Next: Image Creation, Management, and Registry(Part 1)

Latest comments (1)

Collapse
 
zinox9 profile image
Arjun Porwal

Nicely Explained ! The Detailed Specification through images helps a lot.