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.
I will not be showing you the steps to install docker here.
You can refer this.
Docker works on a wide variety of operating systems, this includes:
The installation of Docker is pretty straight forward in each one of them.
- Virtual Machine contains the entire Operating System.
- The container uses the resource of the host operating system
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.
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
$ docker pull nginx $ docker images $ docker run $ docker run -dt -p 80:80 nginx $ docker ps $ apt install net-tools $ ifconfig
Here is a Snapshot for your reference, Do click on image to see it properly.
To check the nginx server
Here 188.8.131.52 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.
$ docker inspect mynginx
This command shows the config details of docker container
In below Screenshot first I deleted the container by
$ docker rm 6e80b1835da72da248ae2e2d2b35045c3cd736196159a030f76bd31fb9f16f33
then did the porting from 8000 --> 80
$ docker run -d -p 8000:80 --name mynginx nginx
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
Docker containers can be removed with the help of docker container rm 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)|
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.
For more commands related to 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.
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
Here is the snapshot of above commands.
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
Whenever we run a container, a default command executes which typically runs as PID 1.
We can override the default container command by manually specifying the command.
$ docker container run -d nginx sleep 500
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.
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
|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
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.
The docker system df command displays information regarding the amount of disk space used by the docker daemon.
$ docker system df $ docker system df -v
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
To remove one image at a time, we will use below command.
docker rmi nginx
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)