que-es-como-usar-dockerfile-en-docker

What Is a Dockerfile and How to Use It in Docker

  • 4 min

A Dockerfile is a plain text file containing a list of instructions that Docker uses for building a custom image.

Up to this point in the course, “we have acted as Docker users”. We have downloaded images created by others (Nginx, Python, Ubuntu) and run them.

But what happens when we want to use it to create our own application? This is where the interesting and most useful part of Docker begins.

Imagine you have built a Node.js website or a Python API. In the past, you would have had to hand your source code to a colleague and say: “Install Python, then the libraries with pip, then configure this environment variable…”.

That is precisely what we aim to avoid. In Docker, what we do is create our own custom image. An image that already comes with your code and your dependencies installed.

To create that image, we need to write a “recipe”. That recipe is the Dockerfile👇.

What is a Dockerfile?

A Dockerfile is a plain text file, without extension (it is simply named Dockerfile) that contains a series of sequential instructions.

Docker reads this file and executes the instructions one by one to build your image.

Your First Dockerfile: A Custom Website

Let’s create our first image that, when run, displays a website with your name, without having to use volumes or copy files manually.

Create an empty folder on your computer and inside it create an index.html file with this:

<h1>This website lives inside my own Docker image</h1>
Copied!

In the same folder, create a file named Dockerfile (note the initial capital letter and no .txt extension).

Open it and write:

# 1. Use a base image
FROM nginx:alpine

# 2. Copy our html into the image
COPY index.html /usr/share/nginx/html/index.html
Copied!

Just two lines.

  • FROM: Tells Docker “Don’t start from scratch. Start with a working Nginx”.
  • COPY: Tells it “Take the index.html file from my PC and put it into the web server folder inside the image”.

Now we tell Docker to take our project and create the image. Open a terminal in that folder and run:

docker build -t mi-web-personalizada .
Copied!

That . at the end of the command tells Docker: “Look for the Dockerfile and the files in the current directory”. Without the dot, the command will fail.

You will see output similar to this:

s ✓ Building 2.3s (7/7) FINISHED o [internal] load build definition from Dockerfile o [internal] load .dockerignore o [internal] load metadata for docker.io/library/nginxo [1/2] FROM docker.io/library/nginxo [2/2] COPY index.html /usr/share/nginx/html/index.html _ s ✓ exporting to image s ✓ naming to docker.io/library/mi-web-personalizada

Congratulations! You have just built your first image.

Testing the Result

Now your image exists on your computer, just as if you had downloaded it from Docker Hub. Verify it:

docker images
Copied!

You will see mi-web-personalizada in the list.

Now, launch it (create a container):

docker run -d -p 8080:80 mi-web-personalizada
Copied!

If you go to localhost:8080, you will see your message.

The best part is that this image is self-contained. It no longer depends on the index.html file on your desktop. You can delete the original file, send the image to a server in Japan, and it will work the same. The code is “baked” inside the image.

Infrastructure as Code

What we have just done is the foundation of the Infrastructure as Code philosophy. We no longer write Word documents explaining how to install our server. The Dockerfile IS the executable documentation.

  • What version of Linux does it use? Look at the FROM.
  • Where are the files located? Look at the COPY.
  • What ports does it open? Look at the EXPOSE.