Building Containers with Docker: Understanding the Dockerfile

What is docker?

Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone executable packages that contain everything an application needs to run, including code, runtime, system tools, libraries, and settings.

Docker allows developers to package their applications and dependencies in a container, which can then be easily deployed and run on any machine that has the Docker runtime installed. This allows for consistency and reproducibility across development, testing, and production environments.

Docker also provides a centralized hub, called the Docker Hub, where developers can store and share their container images with others. This makes it easy for teams to collaborate and use pre-built images, instead of building everything from scratch.

We have already discussed docker and docker hub in our previous blog
How to run docker images as a container

So in order to build our custom image we need to write a docker file. Docker takes the values from these files to build our docker image.

What is a docker file?

A Dockerfile is a script that contains a set of instructions for building a Docker image. Each instruction in the Dockerfile creates a new layer in the image, and the final image includes all the layers from all of the instructions. This is similar to a bash script we need to tell the docker what actions we need to do while creating our image.

So there are many instructions and arguments which need to be included in the docker file which serves different purposes we need to understand the basics instruction and their use cases. Let’s get started

Basic instructions in the docker file.

  • FROM: specifies the base image for the build.
  • RUN: runs a command during the build process.
  • COPY: copies files from the host machine to the container.
  • ENV: sets environment variables in the image.
  • EXPOSE: exposes a port or ports for the container.
  • CMD: specifies the command that will be run when the container starts.
  • ADD: copies files from the host machine to the container and also support downloading files from a remote location.
  • ENTRYPOINT: Specifies the default command that will be run when the container starts.
  • LABEL: adds metadata to the image.
  • USER: sets the default user for the container.
Writing a sample Docker file.

Using the above instructions we can write our first Docker file to install python3 in a ubuntu container and print a Hello world to the console.

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y python3
COPY my_script.py /app/
CMD [“python3”, “/app/my_script.py”]

  1. The first line specifies that the base image for the container should be Ubuntu 20.04.
  2. The next line runs the command apt-get update to update the package lists and then runs apt-get install -y python3 to install Python 3 on the container.
  3. The COPY command then copies the file my_script.py from the host machine to the /app/ a directory within the container.
  4. Finally, the CMD the command sets the command that will be run when the container is started, which is ["python3", "/app/my_script.py"] which runs the python script my_script.py using the python3 interpreter.

We need a sample my_script.py file within the directory where the docker file is present.

print(“Hello World!”)

Build the image and run the container.

Next, you can create the image using the docker build command and run your container using the docker run command.

docker build -t image:latest .
docker run -ti image:latest

That’s super easy to create your docker file and run the container. This is just a sample application we can build our complex multitier applications using a docker file and create our containers.

Leave a Reply

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