We can build a custom Docker image using Dockerfile. A Dockerfile is just a file without any extension. Some common commands used in Dockerfile are:
1. RUN (basically defines the base image)
2. FROM (runs command as an argument from the image)
3. MAINTAINER (to tag your name along with the image which you are building)
4. ADD (acts like copy command, used to copy files from host to container)
5. CMD (similar to RUN command, but not executed during the image is being built)
6. ENTRYPOINT (this is the first command to be executed and it can override the CMD command)
7. ENV (used to set the environment variables (one or more))
8. EXPOSE (used to associate a specified port number)
9. USER (used to set the UID or username which we want to run the container)
10. VOLUME (set a custom path where our container store all the files)
11. WORKDIR (inside the container if we want to customize the place where we want to execute the CMD command. Mainly used when multiple containers hosting the same application, then we might use same path to store the files)
Fig: How to use Dockerfile |
Dockerfile Syntax:
We can use 'comments' and 'commands + arguments' in a Dockerfile. We can use a '#' symbol to represent a comment and the # mentioned line will not be executed as it is a comment line.
Syntax:
# This is a comment line
Command argument1 argument2…
Example:
# Print "Welcome to RedhatPanacia "
RUN echo "Welcome to RedhatPanacia "
Dockerfile for installing Apache in Centos:
FROM centos:latestMAINTAINER redhatpanacia
RUN yum update -y && yum install -y httpd
ADD ./index.html /var/www/html
EXPOSE 80
CMD ["apachectl", "-D", "FOREGROUND"]
Run Dockerfile to build an image:
Syntax:# docker build -t <provide_any_name_for_image>:<version> <path_of_Dockerfile_without_filename>
Example:
# docker build -t mycustomimage .
This command will first search Dockerfile in the same location (because we only provided a dot (.) which represents the same location from where the command is being executed) and then execute Dockerfile to build an image and will name it 'mycustomimage'.
When we run this Dockerfile to build the image, first latest version of centos image will pull ➔ then container-1 will be created ➔ then maintainer command will run ➔ which will create image-1 ➔ that image-1 will create container-2 ➔ inside container-2 RUN command will be executed ➔ this will create image-2 ➔ image-2 will create container-3 ➔ now add command will run and image-3 will build ➔ image-3 will create container-4 ➔ container-4 will expose port number 80 and build image-4 ➔ this image-4 will create container-5 ➔ in which CMD command will run ➔ then finally we will get our final image.
Here, images are appending layer by layer, to result in a final image. After built we will get a message 'Successfully built' and the ID of Docker image. Every container is deleted which was being created during image creation and finally we got our image which we can see by using command 'sudo docker images'.
Fig: Docker Image |
'REPOSITORY' shows image name, 'TAG' shows the version of that image, 'IMAGE ID' shows the ID of the image, 'CREATED' tells us the time of image creation and 'SIZE' shows the total size of the image.
Verify our created image is working or not:
$ sudo docker run -p 80:80 --name=webserver mycustomimage:v1.1
This will run a container, which we can see by using command 'docker ps' in another terminal.
Now, use 'localhost:80' on the host to get the contents of index.html file OR you can use 'curl localhost:80' command also to get the contents of the index.html file.
Some more useful commands:
# docker tag <old_imagename/image id> <new_imagename>:<version/tag>
➔ Command to rename an existing image with registry identification:
# docker tag <old_imagename> <registry_name>/<new_imagename>:<version/tag>
For example:
# docker push redhatpanacia/custom-apache:v1.1
An account on Docker Hub is known as a registry which has repositories. Images in Docker Hub are known as repositories.
➔ Command to upload an image on Docker Hub:
# docker login
Username:
Password:
# docker push <registry_name>/<imagename>:<version>
➔ Command to run an image:
# docker run -it -p 80:80 <image_name>:<version>
➔ Specify container name:
# docker run -it --name=<provide_a_container_name> -p 80:80 <image_name>:<version>
No comments:
Post a Comment