Docker Installation and important commands
⬤ Create a machine on AWS with Docker installed AMI, and install Docker if not installed.
yum install docker
or follow
Docker Installation
- To see all images present in your local.
→
docker images or docker image ls -a
- To find out images in the docker hub.
→
docker search <image_name>
- To download an image from docker-hub to a local machine.
→
docker pull <image_name>
- To give a name to the container and run.
→
docker run -it --name <container_name> <image_name> /bin/bash
[-it → interactive mode and direct to terminal]
- To check, whether the service is starting or not.
→
service docker status or, service docker info
- To start the service.
→
service docker start
- To start/stop the container.
→
docker start <container_name> or, docker stop <container_name>
- To go inside the container.
→
docker attach <container_name>
- To see all the containers.
→
docker ps -a
- To see only running containers.
→
docker ps
- To delete the container.
→
docker rm <container_name>
- Exiting from the docker container
→exit
- To delete images.
→
docker image rm <image_name> or docker rmi <image_name>
Dockerfile Creation
Docker image creation using existing docker file
- We have to create a container from our image, therefore create one container first
→
docker run -it --name <container_name> <image_name> /bin/bash
→
cd tmp/
[Inside directory to create a file]
- Now create one file inside this tmp directory
→
touch myfile
- Now if you want to see the difference between the base image & changes on it then
→
docker diff <old_container_name>
[D → Deletion, C → Change, A → Append/Addition]
- Now create an image of this container
→
docker commit <container_name> <container_image_name>
→
docker images
- Now create a container from this image.
→
--> docker run -it --name <container_name> <update_image_name> /bin/bash
--> ls
--> cd tmp/
--> ls
o/p --> myfile [You will get all files back]
Docker file creation using dockerfile
Dockerfile
Dockerfile is a text file it contains some set of instruction
Automation of docker image creation
◼ FROM
For the base image. This command must be on top of the docker file.
◼ RUN
To execute commands, it will create a layer in the image.
◼ MAINTAINER
Author/Owner/Description
◼ COPY
Copy files from the local system (dockerVM) we need to provide a source, and destination. (We can’t download the file from the internet and any remote repo)
◼ ADD
Similar to copy, it provides a feature to download files from the internet, also we extract the file from the docker image side.
◼ EXPOSE
To expose ports such as port 8080 for tomcat, port 80 for Nginx, etc…
◼ WORKDIR
To set a working directory for a container.
◼ CMD
Execute commands but during container creation.
◼ ENTRYPOINT
Similar to CMD, but has higher priority over CMD, the first commands will be executed by ENTRYPOINT only.
◼ ENV
Environment Variables.
◼ ARG
To define the name of a parameter and its default value, the difference between ENV and ARG is that after you set on env. using ARG, you will not be able to access that late on when you try to run the Docker container.
⬛️ Creation of Dockerfile
Create a file named Dockerfile
Add instructions in Dockerfile
Build dockerfile to create an image
Run image to create the container
[root@ip...]# vi Dockerfile
FROM ubuntu
RUN echo "Creating our Image" > /tmp/testfile
◼ To create an image out of Dockerfile
→ docker build -t <image_name> .
[-t → tagname , . ← the current directory for this dot]
◼ Check process state
→
docker ps -a
◼ See images
→
docker images
◼ To create a container from the above image
→
docker run -it --name <container_name> <img_name> /bin/bash
⬛️ Ex. Command for next image creation
- No need to create a new Dockerfile we will just update the Dockerfile.
[root@ip...]# vi Dockerfile
FROM ubuntu
WORKDIR /tmp
RUN echo "Hello World" > /tmp/testfile
ENV myname Raven_Bytes
COPY testfile /tmp
ADD test.tar.gz /tmp
Open Dockerfile with vi remove all and this code
We have to create a testfile, test, and test.tar.gz
Then build the image
Then create the image