1. Home
  2. Docker
  3. Docker Tutorials
  4. Docker – Dockerfile

Docker – Dockerfile

Working with Dockerfile

Dockerfile is a file used to build images by reading instructions from a file. The default name is used as Dockerfile . You can create dockerfile in the current directory with specific instructions and build a customized image as per your requirements.

Build Image with Dockerfile

As a best practice, the Dockerfile is called Dockerfile and located in the root of the context. You can use the following command to build docker image. This will read Dockerfile in the current directory.

$ docker build -t image_name .

You can also use the -f flag with docker build command to point to a Dockerfile anywhere in your file system.

$ docker build  -t image_name -f /path/to/Dockerfile .

Create a Dockerfile

For this tutorial, I have created a sample project on Github. Just clone the repository using the following command.

$ git clone https://github.com/tecrahul/dockerfile
$ cd dockerfile

Now build the docker image with name apacheimage.

$ docker build -t apacheimage .

After build, You can view the image under “docker images” command.

Launch Container with Image

Now the time is for creating the instance using the latest image created.

$ docker run -it -p 8080:80 apacheimage

The above command will launch a docker container using the image apacheimage. Here I have bind host system port 8080 with container port 80, so you can access the Apache running on a container.

Tags ,