Docker file

Dockerfile

Dockerfile is basically a text file. It contains the set of instructions means how’s the container going to build according to user requirements.
Dockerfile contains the configuration of container like Image type, Image Name, set of command, Ports and Networks.

Docker Components

Dockerfile contains docker components which includes Image type, Container name, docker Swarm, container Ports, Command instructions, Working directory Environment variables, author name etc…

Docker Components types

FROM :- for base image, this command must be on top of the dockerfile.

RUN:- To execute commands, it will create a layer in image.

MAINTAINER:- Author or Owner name

COPY:- To copy files from local system to inside container.

ADD:- Similar to copy, but it provide a feature to download files from internet.

EXPOSE:- To expose the ports in docker container.

WORKDIR:- To set working directory for a container

CMD:- Execute commands but during container creation.

ENTRYPOINTS:- Similar to CMD, but has higher prority over CMD.

ENV:- Environment variables

Lets Create ubuntu container using dockerfile and its components and deploy it

Step:- 1. Create a file and using this name Dockerfile

$ sudo nano Dockerfile

FROM ubuntu
WORKDIR /tmp
RUN echo “Redient Security” > /tmp/testfile

paste this above lines in Dockerfile

Press Ctrl + x and press ‘Y’ to save it

Now create Image using this Dockerfile

$ sudo docker build -t test .

here test is image name and Dot is for current working directory

Run the below command

$ sudo docker images

Now create container from the test image which we have created using Dockerfile

$ sudo docker run -it –name mycontainer test /bin/bash

here mycontainer is container name and /bin/bash is for terminal

After running above command, now you are in container and in /tmp directory
Now run the below commands to print the output of a file

# ls
# cat testfile

Now you can see, we written the “Welcome to Redient Security” content in Dockerfile and we successfully save the in testfile and printed.

Let’s take another example of Dockerfie, this time we will create one image and in that image or container we create one user and install ssh server expose port 22 for ssh.

Open terminal and create one Dockerfile

$ sudo nano Dockerfile

Copy and paste the below lines in Dockerfile

FROM ubuntu:18.04
LABEL name=”Pramod Singh”
LABEL email=”[email protected]
ENV NAME pramod
ENV PASS password123
RUN mkdir -p /var/run/sshd
RUN apt-get update && apt-get install -y openssh-server
RUN useradd -d /home/pramod -g root -G sudo -m -p $(echo “$PASS” | openssl passwd -1 -stdin) $NAME
EXPOSE 22
CMD [“/usr/sbin/sshd”, “-D”]

Save the file with Ctrl + X and press ‘Y’
Now run the below command to build a image from Dockerfile

$ sudo docker build -t pramod .

Run the below command to view Docker image

$ sudo docker images

Now create container from the pramod image

$ sudo docker run -it –name redients pramod /bin/bash

After running the above command, you are in container now, login in pramod user
# su pramod

Now you successfully logged in pramod and you can also check ssh status, ssh not running run the below commands

# service ssh status
# service ssh start

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon