GUI Application inside Docker & ENTRYPOINT vs CMD

Introduction

The use of Docker containers has greatly transformed software development as it allows for an efficient and portable method of packaging and deploying applications. Although Docker is commonly associated with server-side applications, it is also capable of supporting graphical user interface (GUI) applications. This article will provide a step-by-step guide on how to use Docker containers to run GUI applications. In the world of containerization, running graphical applications through Docker opens up new possibilities. This post will explore the process of running Firefox browser in a Docker container and seamlessly connecting the graphical interface to the host machine.

Prerequisites

Before we embark on this journey, ensure you have the following prerequisites installed on your CentOS machine:

  1. Docker: Install Docker by running the following commands:

     yum install docker -y
     sudo systemctl start docker
    
  2. X11 Server: Install the X11 server components:

     sudo yum install xorg-x11-server-Xorg -y
    

    Start the X11 server:

     sudo systemctl start xorg-x11-server
    

Creating Dockerfile

Dockerfile for running Firefox :

FROM centos:7
RUN yum install -y firefox
ENTRYPOINT ["firefox"]

Explanation:

  • FROM centos:7: Specifies CentOS 7 as the base image.

  • RUN yum install -y firefox: Installs the Firefox browser within the container.

  • ENTRYPOINT ["firefox"]: Sets the entry point to execute Firefox by default when the container starts.

    Understanding ENTRYPOINT vs CMD

    In a Dockerfile, both ENTRYPOINT and CMD are instructions that define what command to run within the container. However, there is a key difference:

    • CMD: Specifies the default command to execute when the container starts. If a user provides a command when running the container, it will override the CMD instruction.

    • ENTRYPOINT: Similar to CMD, but it serves as an executable that runs by default. The specified command in ENTRYPOINT will always be executed, and any additional commands provided when launching the container will be treated as arguments to the ENTRYPOINT command.

Building the Docker Image

Navigate to the directory containing your Dockerfile and build the Docker image:

docker build -t firefox-container:v1 .

This command tells Docker to build an image named firefox-container with the tag v1 based on the enhanced Dockerfile.

Launching the Docker Container with Firefox-Container Image

Now, let's run a container with Firefox, ensuring proper X11 forwarding and volume mapping, and providing flexibility with the ENTRYPOINT variable:

docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:rw firefox-container:v1
  • -it: Runs the container in interactive mode, allowing interaction with the Firefox browser.

  • --rm: Removes the container after it is closed, keeping your environment clean.

  • -e DISPLAY=$DISPLAY: Sets the DISPLAY environment variable inside the container, specifying the X11 server address.

  • -v /tmp/.X11-unix:/tmp/.X11-unix:rw: Maps the X11 Unix socket for communication between the container and the host.

Conclusion

Running GUI applications like Firefox in Docker containers provides a flexible and isolated environment for testing and development. You can easily adapt this guide to run other GUI applications of your choice by modifying the Dockerfile.

Optimizing Dockerfiles for GUI applications involves strategic choices, and the use of ENTRYPOINT provides a powerful tool for achieving enhanced flexibility and consistency. By understanding the differences between ENTRYPOINT and CMD, you can create Docker containers that are not only efficient but also highly customizable.

1
Subscribe to my newsletter

Read articles from Hrushikesh Dagwar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hrushikesh Dagwar
Hrushikesh Dagwar

Adaptable individual with a passion for continuous learning and growth, I bring a diverse skill set, leadership and mentoring abilities, and technical and creative expertise to any team or individual.