Mastering the Basics: A Step-by-Step Guide to Dockerfile Creation
When it comes to learning container technology, Docker is often the first choice for many beginners. Docker is a powerful tool that simplifies the deployment and execution of applications through the use of containers. At the core of Docker is the Dockerfile, which serves as a blueprint for creating containerized applications. A Dockerfile consists of a series of instructions that define the necessary components and dependencies of an application.
Let's go to create a basic Dockerfile in our local machine and run a few Docker commands to see the output from the Dockerfile that we are going to define here.
Set of standard instructions to be used in the Dockerfile, like FROM, COPY, RUN, ENV, EXPOSE, and CMD just to name a few basic ones.
First, create a Folder on your local machine named Docker or anything you like. I am a Mac user so I am giving here the instructions based on that. Open your Terminal see below shell command…
mkdir Docker (it will create Docker Folder)
touch Dockerfile (it will create empty Dockerfile)
Open the Dockerfile with any editor or visual code IDE & copy these below commands to Dockerfile then save it.
# Use a base image from Docker Hub
FROM ubuntu:latest
# Update package lists and install a package
RUN apt-get update && apt-get install -y curl
# Copy a file from the host to the container
COPY myfile.txt /app/myfile.txt
# Run a command inside the container
CMD echo "Hello, World!"
# Expose a port that the container listens on
EXPOSE 8080
Exception: Now you can see I have used COPY instruction in the Dockerfile. Basically this COPY will copy myfile.txt from the local drive or machine to the container location I have used /app/myfile.txt. In your case could be a different location that you want to copy. So now, create a myfile.txt file in the same Docker folder otherwise you will get an error when you run the Dockerfile.
Dockerfile->build Dockerfile as Docker Image->run Docker Image to build Docker Container
Dockerfile — contains the setup of instructions and is used to create an image
Docker Image is like a package, template, or plan
Docker Image — a container image is a file with executable code that can be run as a container
Now do the following, we want to check “Hello World” will display from Dockerfile we have defined. Open your terminal then do the following commands…
docker build -t testimg .
docker images
docker run -ti testimg
Important note, to run Docker you must have a Docker desktop running on your machine. Download Docker desktop: https://www.docker.com/products/docker-desktop/
Another Docker Resource to Follow: https://spacelift.io/blog/dockerfile
#Docker #container #DevOps