What is docker
Docker is a computer program which is used to provide a running environment to run all kinds of application which are in docker hub, or created in docker.
It creates an image of your application and stores all requirements of files into the container. Whenever we want to run docker application in any system, we have to run a single file without providing any other requirements.
- Docker is easy to use in Ubuntu. It also supports Window and Mac operating system.
For windows, it runs in Windows10/enterprise only. To use in Windows7/8/8.1 or Windows10 home should use docker toolbox.
- There are two kind of docker software for programmers.
- Docker CE :- Free community edition :- This is an open source software.
- Docker EE :- Docker Enterprise Edition :- This is a paid software design for enterprise development and IT teams who build, ship, and run business-critical applications in production.
- Operating system (ubuntu)
Steps to install docker
- Open terminal and follow these command to install docker.
- Just type 'docker' and check if docker is in your system or not.
- To check the version of operating system. To install Docker CE, we need the 64-bit version of one of these Ubuntu versions:
- Cosmic 18.10
- Bionic 18.04 (LTS)
- Xenial 16.04 (LTS)
- Update the apt package index.
$ sudo apt-get update
- If requires, then install.
$ sudo apt-get install
- If docker is not in your system then install it.
$ sudo apt-get install docker.io
- Now check the staus of docker.
$ sudo systemctl status docker
Steps to add user in docker
- Why sudo :-
We have to use 'sudo' command to run docker commands because docker container run user 'root'. We have to join the docker group, when your system join the docker group after that one can run docker command without sudo.
- 'USER' is your system name, commands to add user as listed below.
$user will pick system user
- $ sudo groupadd docker
- $ sudo gpasswd -a $USER
- $ newgrp docker
- Second way to add user in docker group.
- $ sudo groupadd docker
- $ sudo usermod -aG docker $USER
- After adding a
'USER' into the docker group, we have to shut down or restart so that we can run docker commands without 'sudo'.
- Command to uninstall docker. $ sudo apt-get remove docker docker-engine docker.io containerd runc
- To check Docker version
$ docker --version
- To check Docker and containers info
$ docker info
- Find out which users are in the docker group and who is allowed to start docker containers.
- $ getent group sudo
- $ getent group docker
- 'pull' command fetch the 'name_of_images' image from the 'Docker registry' and saves it to our system.
$ docker pull busybox (busybox is name of image)
- You can use the 'docker images' command to see a list of all images on your system.
$ docker images
- To find the location of the images in the system we need to follow some commands:-
$ docker info
path of docker:- "Docker Root Dir: /var/lib/docker"
Commands to check the images:-
$ cd /var/lib/docker
pardise@pardise-MS-7817:/var/lib/docker$ cd image
bash: cd: image: Permission denied
Permission denied for all users
$ sudo su
$ root@pardise-MS-7817:/var/lib/docker# ls
Now docker info command will provide all details about images and containers
$root@pardise-MS-7817:/var/lib/docker/image/overlay2# docker info
- Now run a Docker container based on this image. When you call run, the Docker client finds the image (busybox in this case), loads up the container and then runs a command in that container.
$ docker run busybox
- Now Docker client ran the 'echo' command in our busybox container and then exited it.
$docker run busybox echo "hello from busybox"
- Command to shows you all containers that are currently running.
$ docker ps
- List of all containers that one can run. Do notice that the STATUS column shows that these containers exited a few minutes ago.
$ docker ps -a
CONTAINER ID – Unique ID given to all the containers.
- To start Container
$ docker start (container id)
- To login in Container
$ docker attach (container id)
- To stop container
$ docker stop (container id)
IMAGE – Base image from which the container has been started.
COMMAND – Command which was used when the container was started
CREATED – Time at which the container was created.
STATUS – The current status of the container (Up or Exited).
PORTS – Port numbers if any, forwarded to the docker host for communicating with the external world.
NAMES – It is a container name, you can specify your own name.
Difference between images and containers
Docker Image is a set of files which has no state, whereas Docker Container is the abstract of Docker Image. In other words, Docker Container is the run time instance of images.
Remove images and containers
- Docker containers are not automatically removed, firstly stop them, then can use docker rm command. Just copy the container IDs.
$ docker rm 419600f601f9 (container_id)
- Command to deletes all containers that have a status of exited.
-q flag, only returns the numeric IDs and -f filters output based on conditions provided.
$ docker rm $(docker ps -a -q -f status=exited)
- Command to delete all container.
$ docker container prune
- Command to delete all images.
To remove all images which are not referenced by any existing container, not just dangling ones, use the -a flag:
$ docker images prune -a
- Removing all Unused Objects. It will remove all stopped containers,all dangling images,and all unused network.
To remove all images which are not referenced by any existing container, use the -a flag:
$ docker system prune -a
dangling image is an image that is not tagged and is not used by any container. To remove dangling images type:-
$ docker images prune
$ docker rmi image_id image_id......