Docker

How to Run a Docker Instance From a Dockerfile?

Docker is a well-liked open platform that is universally used to build, test, deploy, and ship applications in a containerized environment. Docker uses the OS level of virtualization and supports different components such as Docker containers, images, networks, and Docker volume.

The Docker containers are one of the essential components of the Docker platform that packages the program, configuration settings, and dependencies using the Docker image. The Docker image is a simple template or snapshot of a container that guides the container on how to containerize an application. These images are mostly available on the official Docker hub registry. Users can also design these images according to project requirements using Dockerfile.

This blog will explain:

What is a Dockerfile?

A Dockerfile is a normal text file that contains commands and instructions that define the basic structure of a Docker container. These instructions can be executed in the terminal without any file but running each command one after one is a hectic and complex task. Dockerfile make it easy for developers to specify all the requirements and instructions into one file. This file will be then used to build the container template which is a Docker image. After that, the docker image is executed to start the instance in the Docker container.

Basic Commands of Dockerfile

Some basic commands of Dockerfile that are used to define the basic snapshot of the container are listed below in tabular form:

Commands Description
FROM The “FROM” command is utilized to define the base image for the container template. These images are pulled from the official Docker registry Docker Hub.
MAINTAINER The “MAINTAINER” command defines the author(Name and Email) information who is creating the Docker image.
WORKDIR It specifies the working directory of a containerized application.
COPY Utilized to copy the source and configuration files from the host system to the specified path of Docker container.
ADD The “ADD” command is similar to the “COPY” command but it also supports the remote URL to add the file from the URL to the container path such as from the GitHub repository to the container path.
RUN The “RUN” is utilized to run the commands in container. In Dockerfile, it is mostly used to manage and install extra dependencies inside the container.
CMD CMD” defines the default points of Docker containers. It basically defines the executables and default parameters of the “ENTRYPOINT”.
ENTRYPOINT The “ENTRYPOINT” command is also used to set the executables of the Docker container. It basically sets the default applications that are used in a container every time. The ENTRYPOINT command is also used once in a Dockerfile.
USER This command is used to set the UID (user name) to execute the commands in Container
VOLUME The “VOLUME” command is used to bind or mount the external volume(file system) with a container.
ENV The “ENV” command is utilized to set the container’s environment variables.
ARG The “ARG” is utilized to pass the arguments inside the container.
EXPOSE The “EXPOSE” command specifies the exposing ports on which the container will be executed.
ONBUILD It reads instructions from the base image but triggers these instructions via the downstream image.
LABEL The “LABEL” is utilized to specify the metadata of the container snapshot.

How to Run a Docker Instance From a Dockerfile in Docker?

To run a Docker instance or container using Dockerfile, first, create a Dockerfile. Then, make the basic snapshot for the container using Dockerfile. After that, run the snapshot to start the Docker instance.

Follow the below instructions for illustration.

Step 1: Create a Dockerfile

First, create a Dockerfile. Remember that Dockerfile does not have any file extension. After that, paste the following commands into the file:

FROM golang:1.8

WORKDIR /go/src/app

COPY main.go .

RUN go build -o webserver .

EXPOSE 8080:8080

ENTRYPOINT ["./webserver"]

Step 2: Make Program File

Next, make a new file named “main.go” that contains the simple Golang program. For this purpose, paste the following program into the file:

package main
import (
    "fmt"
    "log"
    "net/http"
)

func handler (w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello! Welcome to LinuxHint Tutorial")
}
func main () {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}

Step 3: Generate Container Snapshot

Now, build the Docker snapshot of the container using “docker build -t <image-name> -f <path-to-Dockerfile> .”:

docker build -t go-img -f Dockerfile .

In the above command, the “-t” option sets the name or tag of the image, “-f” option specifies the path to the Dockerfile from which the Docker engine needs to read the build context:

To check whether the image is successfully created or not, run the “docker images <image-name>” command:

docker images go-img

The output shows that we have successfully created the container’s snapshot from the Dockerfile:

Step 4: Run the Snapshot to Fire Up the Container

Now, start the instance of Docker in the Docker container by executing the container’s snapshot that is created in the above step:

docker run -p 8080:8080 --name go-cont -d go-img

In the above command, the “-p” option runs the container on the “8080” port, “–name” sets the container’s name and the “-d” option runs the container in detached mode (background service):

To verify if the container is executing or not, list down the running containers using the “docker ps” command:

docker ps

Now, navigate to “http://localhost:8080” and verify if the application is running on the exposing port or not:

The above output indicates that we have effectively started the Docker instance using Dockerfile.

How to Run an Instance From a Dockerfile in Docker Compose?

Docker compose is another major plugin of the Docker platform that enables us to run multiple instances in different containers at one time. Users can also run the Docker instance from Dockerfile using the Docker compose plugin. For illustration, follow the given steps.

Step 1: Create Dockerfile

First, create a Dockerfile. For instance, we are using the same Dockerfile and program file that are used in the above section:

FROM golang:1.8

WORKDIR /go/src/app

COPY main.go .

RUN go build -o webserver .

EXPOSE 8080:8080

ENTRYPOINT ["./webserver"]

Step 2: Make docker-compose.yml File

Next, create a “docker-compose.yml” file and copy the following key pairs into the file:

version: "3"

services:
   
  web:
    build: .
    ports:
    - 8080:8080

In the above-mentioned snipped:

  • services” is utilized to specify the composing services that will be executed in a separate container. For illustration, we have configured only one service “web” to keep the code clean and simple.
  • build” key pair is utilized to read the build context from the Dockerfile. Here, docker will read the instructions from the Dockerfile and build the container accordingly.
  • ports” key defines the exposing ports on which the container will be executed.

Step 3: Start Docker Instance

Now, run the Docker instance in a container using the “docker-compose up” command:

docker-compose up -d

For verification, check the list of running compose containers using the “docker-compose ps” command:

docker-compose ps

The output shows that the “web” service is successfully executing in the “golangapp-web-1” container:

Navigate to the exposing port of compose service and verify whether the program is executing or not. The below result shows that we have successfully started the Docker instance from Dockerfile using Docker compose:

That is all about running a docker instance from a Dockerfile.

Conclusion

To execute a Docker instance in a container from a Dockerfile, first create Dockerfile. Add the commands inside the file to create the image or template of the Docker container. Then, make the container image or snapshot using the “docker build -t <image-name> -f <path to Dockerfile> .” command. Now, execute the container image to start the Docker instance. Users can also use the Docker compose to run the Docker instance from Dockerfile. This post has illustrated how to run a Docker instance from a Dockerfile.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.