What is Docker?
Docker is the secret weapon of developers and IT ops teams everywhere, allowing them to build, ship, test, and deploy apps automatically, securely, and portable with no surprises.
What is Docker Container?
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment. Containers isolate software from its surroundings, for example, differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.
How to create docker image ?
Step 1: Create a Dockerfile.
$ touch Dockerfile
Step 2: Write docker statement in the file “Dockerfile” for example as below.
FROM ubuntu:latest MAINTAINER Arvind Rawat <[email protected]> #Package install RUN apt update RUN apt install -y nginx #Expose port to be used EXPOSE 80 #Run nginx CMD ["nginx", "-g", "daemon off;"]
FROM -: FROM is used for default base image (OS) used in the Dockerfile
MAINTAINER -: MAINTAINER is used to give the name of image maintainer.
RUN -: RUN command is used to execute the command while building the image
EXPOSE -: EXPOSE command is used to expose the port which can be used in the container.
CMD -: CMD command is used to execute the command while launching the container.
How to build the Docker image using Dockerfile?
docker build -t <tag name> path
To run the docker container
$ docker run -d --name <ContainerName> -p 80:80 <ImageName or ImageID>
$ docker run -d -p 80:80 nginx
In the above example we have used “docker run” command to run the docker container, “-d” is used to run container as daemon and “-p” or “-P” is used to published port.
To Stop running container
$ docker stop <ContainerName or ContainerID>
The above command will stop the running container. You can get the docker running containerName or ContainerID using the command ” $docker ps”. This command will show you the running container.
To remove the container use the below command.
$ docker rm <ContainerName or ContainerID>
You can remove the stopped container using the above command.
If you want to remove forcely a container. You need to use command as below.
$ docker rm -f <ContainerName or ContainerID>
To Remove the Docker Image
$ docker rmi <ImageName:tag or ImageID>
To get the Docker Image Name or Image ID, use the command “$ docker images”.
This command will show you all the images. You can use Docker ImageName with tag name or Image ID of the docker image. If you are using ImageName you must give the tag name if you will not give any it will use “latest” as default TagName in the docker.
To remove the image forcely, use the below command.
$ docker rmi -f <ImageName:tag or ImageID>