Docker in DevOps: Streamline Your Projects with Containers

Docker in DevOps: Streamline Your Projects with Containers

#90daysofdevopschallenge

#day17

What is Dockerfile?

Dockerfile contains a set of instructions for making a docker image. It is a text document that contains all the instructions that a user could call on the command line to create an image. Always use a base image to create a new docker image.

format of the docker file INSTRUCTION arguments

Note:- INSTRUCTION is always in capital

Rules for writing a Dockerfile:

a) INSTRUCTION is always in capital

b) The Dockerfile name should always be "Dockerfile".

c) A Dockerfile must begin with a "FROM" instruction, that is used to create the base of the image.

d) Docker treats a line as a comment that begins with "#".

Docker file Commands:

FROM: It is used to specify the base image from which created new docker image. It must use this image at the beginning of the docker file.

Syntax: FROM ubuntu:20.04

RUN: Executes command within the image. This is often used to install packages or set up the environment.

Syntax:- RUN apt-get update && apt-get install -y python3

COPY & ADD: Copy files and directories from the host system into the image.

Syntax: COPY ./app /app

ADD ./app.tar.gz /app

WORKDIR: Sets the working directory for subsequent instructions in the dockerfile.

Syntax: WORKDIR /app

ENV: sets the environment variable inside the container

Syntax: ENV MY_VARIABLE=my_value

EXPOSE: Inform docker that the container listens on specific network ports at runtime. Syntax: EXPOSE 80

CMD and ENTRYPOINT: Define the command that will be executed when a container is started.

Syntax: CMD ["python", "app.py"]

ENTRYPOINT ["java", "-jar", "myapp.jar"]

VOLUME: Creates a mount point for attaching external volumes to the container.

Syntax: VOLUME /data

USER: Sets the user that the container should run as.

Syntax: USER myuser

ARG: ARG is used to define the variable dynamically, that you change during image build by passing an argument with the docker build command.

Syntax: ARG APP_VERSION=1.0

Difference between COPY & ADD commands:

COPY

ADD

'COPY' is a straightforward command that is used to copy files and directories from the host into the image.

ADD is a more versatile command that can only copy files but also provide some additional features like tar execution and fetch files from URLs.

It is generally used for simple file-copying tasks where you want to copy files from the host to the image without any additional processing.

It can be used for copying files, downloading resources from the internet, and extracting archives in a single command.

It does not support URL fetching and tar execution.

It provides so many features in comparison to the COPY command but it become so much more complex.

Syntax: COPY ./app /app

ADD ./app.tar.gz /app

Difference between CMD and ENTRYPOINT:

CMD sets the default command to be executed when the container starts. It allows you to provide a default command that can be overridden and specified as a new command when running the container. ENTRYPOINT specifies the main command and its parameters to run when the container starts. Unlike CMD, the commands and parameters specified in ENTRYPOINT are not easily overridden when running the container. Any additional command-line arguments provided when running the container are treated as arguments to the ENTRYPOINT command.

You can use CMD multiple times in a Dockerfile, but only the last CMD instruction is effective; the previous ones are overridden. You can use ENTRYPOINT multiple times in a Dockerfile, but only the last ENTRYPOINT instruction is effective.

❄Tasks:

1.Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Here I took the Django application, which is already present on GitHub.

Create dockerfile according to the application for creating docker images.

Dockerfile

2.Build the image using the Dockerfile and run the container

create docker-image

docker build -t <image_name>.

docker run -it --name <container_name> -p <open_port>:<local_port> <image_name>

3.Verify that the application is working as expected by accessing it in a web browser

4.Push the image to a public or private repository (e.g. Docker Hub )

In the Next Article, we will explore more in Docker......

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!