-->

DEVOPSZONES

  • Recent blogs

    How to create Docker Images with a Dockerfile

    How to create Docker Images with a Dockerfile


    Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

    Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

    In this tutorial, We will learn how to create your own docker image with a dockerfile.

    Details in a Dockerfile:

    FROM

    The base image for building a new image.

    MAINTAINER

    Optional, it contains the name of the maintainer of the image.

    RUN

    Used to execute a command during the build process of the docker image.

    ADD

    Copy a file from the host machine to the new docker image.
    How to Copy Files from and to Kubernetes pods and Docker Container

    ENV

    Define an environment variable.

    CMD

    Used for executing commands when we build a new container from the docker image.

    ENTRYPOINT

    Define the default command that will be executed when the container is running.

    WORKDIR

    This is directive for CMD command to be executed.

    USER

    Set the user or UID for the container created with the image.

    VOLUME

    Enable access/linked directory between the container and the host machine.

    Lets Start:

    Install Docker:

    Please follow this article to install docker according to your OS.

    Docker Installation & Configuration ON CENTOS 7
    Install docker on Ubuntu 

    Ubuntu:

    root@dockerserver# apt-get update

    Install docker.io with this apt command:

    root@dockerserver# apt-get install docker.io

    Now start the docker service and enable it to start at boot time:

    systemctl start docker

    CentOS:

    1. Create Docker Repo. Kernel should be 3.10.

    tee /etc/yum.repos.d/docker.repo <<-'EOF'

    [dockerrepo]

    name=Docker Repository

    baseurl=https://yum.dockerproject.org/repo/main/centos/7/

    enabled=1

    gpgcheck=1

    gpgkey=https://yum.dockerproject.org/gpg

    EOF


    2. Install docker.

    yum install -y  docker-ce docker-ce-cli containerd.io

    Create a Dockerfile

    In this step, we will create a new directory for the dockerfile and define what we want to do with that dockerfile.

    [root@dockerserver nginxtest]# cat Dockerfile
      FROM nginx

      COPY index.html /usr/share/nginx/html/
    [root@dockerserver nginxtest]#

    In this Dockerfile we are pulling latest nginx docker image and a copying index.html file to the docker image from the host machine.

    FROM nginx :  pulling latest nginx docker image
    COPY index.html /usr/share/nginx/html/ : copying index.html file to the docker image from the host machine

    Build New Docker Image


    As we have the  Dockerfile, now we can build a new docker image based on our dockerfile with the docker command below:

    [root@dockerserver nginxtest]# docker build -t manastri/springcloud .
    Sending build context to Docker daemon  123.9kB
    Step 1/2 : FROM nginx
     ---> c7460dfcab50
    Step 2/2 : COPY index.html /usr/share/nginx/html/
     ---> 8dcda34419b1
    Successfully built 8dcda34419b1
    Successfully tagged manastri/springcloud:latest
    [root@dockerserver nginxtest]#

    When the command completed successfully, we can check the new image 'manastri/springcloud ' with the docker command below:

    [root@dockerserver nginxtest1]# docker images
    REPOSITORY                                TAG                 IMAGE ID            CREATED              SIZE
    manastri/springcloud                      latest              8dcda34419b1        About a minute ago   126MB

    No comments