Docker Hub is public repository for images.
Containers are instance of docker image.
A machine where docker is installed and running.
docker run “Image Name”
Above command run a container from given image. If image is not available in host machine, docker will pull the image from docker hub.
Note: Above command will run your container and stops until any process is running in your container.
docker run “Image Name” sleep 2000
Above command will run the image instance and will awake for 2000 seconds.
docker run –name “Name” “Image Name”
Above command will link a name with your running image instance
docker run -it “Image Name”
Above command will allocate interactive process in order to allocate a tty for the container process. So, with combination of ‘i’ and ‘t’, we attach a host terminal to docker containers for interactive user input and output.
docker run -d “Container Name”
Above command with parameter -d will run container in background. ‘-d’ it calls detach mode.
docker run -e environment_variable_name=variable_vaule “Image Name”
Above command will run the image with specified environment variable. We can find all the environment variable of any image by running docker inspect command.
docker attach “Container Name or ID”
Above command will attach the container.
Above command will list down all running containers in host machine.
docker ps -a
Above command will list down all running and exited containers in host machine.
docker stop “Container Name or ID”
Above command will stop container. You can pass multiple containers as well to stop by adding a line space.
docker rm “Container Name or ID”
Above command remove the container from the system. You can pass multiple containers as well to remove by adding a line space.
Note: You can remove running status containers, so stop before removing them.
Above command will list down all the images available in host machine.
docker rmi “Image Name”
Above command will remove the image from the system. You can pass multiple image name by adding a line space.
Note: We cannot remove images if any instances are running therefore, we must stop and remove instances before removing images from host machine.
docker system prune
Above command will remove all stopped containers, all dangling images, and all unused networks.
docker container prune
Above command will remove all stop container.
docker image prune
Above command will remove all dangling and unused images from the host machine.
docker system prune --volumes
Above command will remove all unused volumes.
docker pull “Image Name”
Above command will pull image from docker hub.
docker run ubuntu
Above command will run/pull ubuntu image from docker hub. By default, docker pull latest version of package but if we require any specific version of package then we can tag them with command like below-
docker run ubuntu:16.04
This is called tagging. You can find any package tags from Docker Hub package page.
docker exec “Container name:myubuntu” cat /etc/*release*
Above exec parameter can use to execute command in running container in background.
docker inspect “Container Name or ID”
Above command will give all the details including Network information of the container. So, if you want to know the internal IP of container, just run above command.
docker logs “Container Name or ID”
Above command will list down the logs of containers, if you want to know.
It is important concept in Docker and useful when we want to access running container from host machine or outside of host machine. Let’s take Jenkins example, you have to run Jenkins container in your host machine like below-
docker run jenkins
Above command will run the instance of jenkins and make the service up but you won’t be able to access this jenkins instance, though container can connect to outside world but outside world cannot connect. So, port mapping allows outside world to connect container and to do so we must map the container port to host port like below-
docker run -p 80:80 jenkins
You can run docker inspect to know the opened port of Jenkins and then you can map the same with opened port in host machine. Now you if you access jenkins by typing your host IP Address:8080 you will be able to see jenkins home page. Make sure port is open in your host machine to access it.