Dockerfile
A Dockerfile is a script with instructions on how to build a Docker image. These instructions are, in fact, a group of commands executed automatically in the Docker environment to build a specific Docker image.
Install Docker
If interested in how to use a Dockerfile to create an image, you probably already have Docker installed on your system
Create a Dockerfile
*we will create a directory named DockerImages with the command
# mkdir DockerImages
# cd DockerImages
#touch Dockerfile
Move into that directory and create a new empty file Dockerfile
*Open the file with a text editor, Then, add the following content
# cat Dockerfile
FROM ubuntu
MAINTAINER zippyops
RUN apt-get update
CMD ["echo", "Hello World"]
*FROM – Defines the base of the image you are creating. You can start from a parent image (as in the example above) or a base image. When using a parent image, you are using an existing image on which you base a new one. Using a base image means you are starting from scratch (which is exactly how you would define it: FROM scratch).
*MAINTAINER – Specifies the author of the image. Here you can type in your first and/or last name (or even add an email address). You could also use the LABEL instruction to add metadata to an image.
*RUN – Instructions to execute a command while building an image in a layer on top of it. In this example, the system searches for repository updates once it starts building the Docker image. You can have more than one RUN instruction in a Dockerfile.
*CMD – There can be only one CMD instruction inside a Dockerfile. Its purpose is to provide defaults for an executing container. With it, you set a default command. The system will execute it if you run a container without specifying a command.
Save and exit the file.
Build a Docker Image with Dockerfile
The basic syntax used to build an image using a Dockerfile is
$docker build [OPTIONS] PATH | URL | -
To build a docker image, we would therefore use
$docker build [location of your dockerfile]
If we are already in the directory where the Dockerfile is located, put a . instead of the location
$docker build .
By adding the -t flag, we can tag the new image with a name that will help you when dealing with multiple images
$docker build -t my_first_image .
Once the image is successfully built, we can verify whether it is on the list of local images with the command:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_first_image latest ee5f77af4996 57 seconds ago 101MB
reactjs latest 63c0ab57645bb About an hour ago 1.16GB
Create a New Container
Launch a new Docker container based on the image you created in the previous steps. We will name the container “test” and create it with the command
#sudo docker run --name test my_first_image
Hello World
The Hello World message should appear, Using Dockerfile is a simpler and faster way of building a Docker image. It automates the process by going through the script with all the commands for assembling an image.
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post