Docker
Firstly we have to start the Docker service
systemctl start docker
To check the docker version
docker --version
To get the image from the registry
docker pull <<image_name>>
To get the image from local if available if not get image from registry and create a container.
docker run <<image_name>>
To check available images in our machine
docker images
To check all the containers available
docker ps -a
To check running containers
docker ps
To check exited containers
docker ps --filter "status=exited"
To remove an image
docker rmi image_name
To remove multiple images
docker rmi image1,image2
To remove all images
docker rmi $(docker images)
To create a container from the image
docker run --name container_name image_name
--name tag is used to give a name to the container
To create a container in interactive and detached mode
docker run -itd --name container_name image_name
To start a container
docker start container_name
To stop a container
docker stop container_name
To remove a container
docker rm container_name
To create a docker image from an available container
docker commit container_name image_name
To allocate memory and CPU to the container when we create it
--memory and --cpus
docker run --name container_name --memory=100M --cpus=0.25 image_name
Dockerfile
Dockerfile is a text file that contains set of instructions
Using Dockerfile we can automate image creation.
How it works
First, we need to create a Dockerfile.
By Building Dockerfile we'll get an image.
Create a container using that image.
Dockerfile Components
FROM: it is used to define base image ex: ubuntu, MySQL, java, httpd, nginx etc.
LABEL: it is used to specify the maintainer details.
RUN: it is used to run commands when we build the image(creating an image).
COPY: it is used to copy files from the server to the container.
ADD: it is used to copy files from the server to the container and also we can download them from the internet.
WORKDIR: it creates a directory and we directly move into that directory.
EXPOSE: publish the port numbers.
CMD: it is used to execute commands while we run the image(creating the container).
ENTRYPOINT: it is used to execute commands while we run the image(creating the container).
Diff bw RUN and CMD
RUN --> commands executed while creating image.
CMD --> commands executed while creating container.
Diff bw CMD and ENTRYPOINT
Both are executed at run time but entrypoint takes the highest priority and we can also pass values to the command at runtime