What is Docker Container
- Docker container is the lightweight (because it runs on Linux), portable, secured and flexible.
- Docker container is the same as conventional Virtual box that we use earlier to launch image.
- It’s abstraction on top of hosted Operating System and It is self-contained execution environments it has its own isolated resources like CPU, memory, I/O and network resources, but it uses kernel of the hosted OS.
- Single docker can have multiple container and every container runs single isolated application.
Install Docker container
Install Docker on Linux (using aws linux, but you can use any other linux)
1. Update installation package and cache it on you machine:
sudo yum update -y
2. Install latest version of the docker container
sudo yum install -y docker
3. Start now docker service
sudo service docker start
4. Add ec2-user in the docker group so that you can executes commands on docker without sudo root permissions:
sudo usermod -a -G docker $USER
5. Verify whether ec2-user user can execute command on docker without sudo:
If “docker info” command doesn’t worked then simply log out and logging again and then try it, it is because the group change will not have an effect unless your session is closed.
6. Install git to get source repository from git hub:
sudo yum install -y git
Install Docker on Windows
While installation check all the options and allow all the confirmations, so it will install complete features and once installation is done bellow three icons will be displayed in the desktop:
Let’s open Docker Quickstart Terminal and if it opens without any problem then it is installed successfully otherwise not. Then try to see the errors and warning on the console and get it resolve.
Good Job! Now, we are done with docker installation Windows as well as Linux platform.
How to make Docker images
Below are the ways of creating docker images
Pull image from docker repository
Pull existing / already uploaded images from hub.docker.com
docker pull hello-world
Once download image, lets verify it, using docker images command
git clone https://github.com/docker-library/hello-world.git
Go to the path where Dockerfile is available:
Create image, using below command:
docker build -t hello_world .
Now, verify the whether image is created or not?
Run Docker image into Container
Use run command to start container:
docker run hello-world
Few lines of output are given bellow:
[root@ip-172-31-29-181 hello-world]# docker run hello_world
Hello from Docker!
Another example (reference from aws)
git clone https://github.com/awslabs/ecs-demo-php-simple-app
docker build -t amazon-ecs-sample .
docker run -p 8080:8080 amazon-ecs-sample
Output from the Apache web server is displayed in the terminal window. You can ignore the “Could not reliably determine the server’s fully qualified domain name” message.
docker-machine ip <machine-name>
http://<docker machine IP address>/
Host machine is machine on which docker container is running as we know docker machine has its own ip address and host machine has its own.
In case of AWS linux, docker container is running on different ip address, but http can be done with using host machine’s IP. So in below screenshot, Host machine IP : 126.96.36.199 and docker container IP is : 172.17.0.2.
Important Docker commands
Show list of container running under single docker.
Docker Copy command
Copy any file from local machine to docker container or vice versa.
docker cp <source path (Hosted machine path)> <Container ID>:/<Destination (Application folder path)>
docker cp /home/ec2-user/ecs-demo-php-simple-app/Test.html 34639673b9de:/app/
If you want to copy from docker container to local or hosted machine:
docker cp 34639673b9de:/app/ Test.html /home/ec2-user/ecs-demo-php-simple-app/
If you want to control of linux machine running as a container:
Docker exec -it 34639673b9de bash
Docker exec -it command
You can execute any command from hosted machine on container, check above used bash command.
Start stopped docker:
docker start <Container ID>
docker start 65e9de0bb6d8
docker stop <Container ID>
docker stop 65e9de0bb6d8
docker restart <Container ID>
docker restart 65e9de0bb6d8
docker rm <Container ID>
docker rm 65e9de0bb6d8
docker rmi <Container ID>
docker rmi 65e9de0bb6d8
Remove all containers
docker rm -f $(docker ps -a -q)
Remove all Images
docker rmi -f $(docker images -q)
It shows the detail information like setting and configurations of the docker.
User this command to know all other command that we haven’t covered here.