Table of contents
Dockerfile
Dockerfiles are used to define the instructions for building a Docker image. These instructions include setting up the environment, installing dependencies, copying files, and more.
A Dockerfile is a text-based script used to define the configuration and steps required to build a Docker image. Docker images are the basis for containers, which are lightweight and portable environments that can run applications and services in isolation. A Dockerfile contains a series of instructions that specify how to create an image by starting with a base image and then adding layers of customization on top of it.
Each instruction in a Dockerfile corresponds to a specific action, such as installing software packages, copying files into the image, setting environment variables, and more. Docker uses these instructions to create a reproducible and consistent image that can be shared and executed across different environments.
A dockerfile looks like this-
Docker builds images automatically by reading the instructions from a Dockerfile.
It is a text file without any .txt extensions that contains all commands in order, needed to build a given image.
It is always named Dockerfile.
Dockerfile Commands
FROM - Defines a base image, it can be pulled from the docker hub
(for example- if we want to create a javascript application with the node as the backend then we need to have node as a base image, so it can run node application.)RUN - Executes command in a new image layer( we can have multiple run commands )
CMD - Command to be executed when running a container( It is asked to have one CMD command, If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.) So, it is advisable to keep only 1 CMD in a Dockerfile.
EXPOSE - Documents which ports are exposed (It is only used for documentation)
ENV - Sets environment variables inside the image
COPY - It is used to copy your local files/directories to Docker Container.
ADD - It is a more feature-rich version of the COPY instruction. COPY is preferred over ADD. The major difference between ADD and COPY is that ADD allows you to copy from a URL i.e. the source can be a URL but with COPY it can only copy local ones.
ENTRYPOINT - Define a container's executable (You cannot override and ENTRYPOINT when starting a container unless you add the --entrypoint flag.)
VOLUME - It defines which directory in an image should be treated as a volume. The volume will be given a random name which can be found using the
docker inspect
command.WORKDIR - Defines the working directory for subsequent instructions in the Dockerfile(Important point to remember that it doesn't create a new intermediate layer in Image).
Task 1: Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
We have a project with React and django here, let's clone it.
Let's make the Dockerfile to build this app -
Here, let's see what each command will do -
FROM python:3.9 - The base image will be Python.
WORKDIR app - This will make sure that all the below commands will be executed in this folder.
COPY . /app - Copy all the files and folders from our current local directory to the app folder in the image.
RUN pip install -r requirements.txt - This will execute the pip command that will install all the mentioned packages in that file.
EXPOSE 8001 - This command releases port 8001 within the container, where the Django app will run.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"] - This command starts the server and runs the application.
Task 2: Build the image using the Dockerfile and run the container.
To build an image from dockerfile - docker build -t "any_name".
where -t: tag a name to an image and "." means the dockerfile is in the current location.
The image is built and you can check the list of all the images using docker images
To create a container from this image - docker run -d - --name <container-name> -p 8000:8000 <image-name>
where -d: the container will run in detached mode
--name: gives name to the container
-p: exposes a port number
To check all the containers- docker ps -a
Task 3: Verify that the application is working as expected by accessing it in a web browser.
If you are doing this in an AWS EC2 instance, make sure to edit the security group to allow 8000 port for TCP.
So today, we have successfully created an image from dockerfile and run a container from that image.
Thanks for reading!
#devops#90DaysOfDevops#TrainWithShubham
Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200