Home  /  Resources & support  /  FAQs  /  How can I run Stata in a Docker container?

How can I run Stata in a Docker container?

Title   How can I run Stata in a Docker container?
Authors Stone Chen, StataCorp
Ingrid Aguilar, StataCorp

This FAQ provides guidance for running Stata in a Docker container. The examples use Ubuntu 24.04 LTS and Rocky Linux 8, but other Linux distributions may also be used. Required packages and dependencies may differ by distribution.

Important: Stata license information must never be shared with or embedded in a Docker-based Stata copy. All usage must remain compliant with your license terms, including when running Stata inside Docker.

The following topics cover the main steps for building and running Stata in Docker:

  1. What do I need before I start?
  2. Which Linux distributions can I use?
  3. How should I handle the Stata license in Docker?
  4. How do I create the Dockerfile?
  5. What does the Dockerfile do?
  6. How do I build the Stata Docker image?
  7. How do I run Stata in console mode?
  8. How do I run the Stata(GUI) version of Stata?
  9. Stata(GUI) does not open. What should I check?
  10. Why are Python and Java-related libraries included?
  11. Can administrators customize the image?
  12. How do I know the setup is complete?

1. What do I need before I start?

Before building a Stata Docker image, make sure the following prerequisites are met:

  • Docker is installed and working on the host system.
  • A clean, fully installed copy of Stata is available on the local machine. In this FAQ, that directory is called cleanStata.
  • A valid Stata license is available.
  • X Window System (X11) is installed and running if you plan to use the GUI version of Stata.
  • The examples below use x86-64 images and are written for Ubuntu 24.04 LTS and Rocky Linux 8.

Back to top


2. Which Linux distributions can I use?

The examples in this FAQ use Ubuntu 24.04 LTS and Rocky Linux 8. These distributions are provided only as examples. Other Linux distributions may also be used to run Stata in Docker. If you use a different distribution, package names and required dependencies may differ, and you will need to determine the equivalent packages for your environment.

  Example   Base image   Image tag used below
  Ubuntu 24.04 LTS   ubuntu:24.04   cleanstata:ubuntu-x86-v19.00
  Rocky Linux 8   rockylinux:8   cleanstata:rocky8-x86-v19.00

Back to top


3. How should I handle the Stata license in Docker?

We strongly recommend that you keep stata.lic outside the Stata directory used during the image build and bind-mount it into the container with the docker run command with the following option:

-v ./stata.lic:/usr/local/stata19/stata.lic

The license is then made available to Stata when the container starts without being copied into the image.

Back to top


4. How do I create the Dockerfile?

Create a file named stata.Dockerfile in the directory that contains cleanStata.

Ubuntu 24.04 LTS example

# stata.Dockerfile

# Pull Ubuntu 24.04 LTS. You may substitute a different base image as needed.
FROM ubuntu:24.04 AS base

# The Stata directory on the host must already be installed.
# Keep stata.lic outside this directory and bind-mount the license into
# the container by using the docker run command.
COPY ./cleanStata /usr/local/stata19
ENV PATH=$PATH:/usr/local/stata19/

# This is required for Stata to function correctly.
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    libgtk2.0-0 \
    libncurses6 \
    libncursesw6 \
    libcanberra-gtk-module \
    && apt-get clean \
    && rm -Rf /var/lib/apt/lists/*

# Ubuntu 24.04 LTS from Docker Hub is missing some libraries that are required
# when using Java and Python within Stata at the time of writing.
RUN apt-get update && apt-get install -y software-properties-common \
    && add-apt-repository universe \
    && apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    libpython3.12 \
    libpython3.12-dev \
    libxtst6 \
    && apt-get clean \
    && rm -Rf /var/lib/apt/lists/*

WORKDIR /usr/local/stata19

Rocky Linux 8 example

# stata.Dockerfile

# Pull Rocky Linux 8. You may substitute a different base image as needed.
FROM rockylinux:8 AS base

# The Stata directory on the host must already be installed.
# Keep stata.lic outside this directory and bind-mount the license into
# the container by using the docker run command.
COPY ./cleanStata /usr/local/stata19
ENV PATH=$PATH:/usr/local/stata19/

# This is required for Stata to function correctly.
RUN dnf install -y gtk2 libcanberra-gtk2

# Install a monospace font.
RUN dnf install -y dejavu-sans-mono-fonts
RUN fc-cache -f -v

# Rocky Linux 8 from Docker Hub is missing some libraries that are required
# when using Java and Python within Stata at the time of writing. Comment out 
# these two commands if they are not needed.
RUN dnf install -y python3.9
RUN dnf install -y libXtst

WORKDIR /usr/local/stata19

Back to top


5. What does the Dockerfile do?

  • FROM pulls the selected Linux base image (Ubuntu 24.04 LTS or Rocky Linux 8).
  • COPY copies the already installed cleanStata directory from the host into /usr/local/stata19 in the image.
  • The package-install commands add the required dependencies.

This approach copies an already installed Stata directory because the Linux version of Stata requires interactive input during installation.

Back to top


6. How do I build the Stata Docker image?

From the directory containing stata.Dockerfile and cleanStata, list the existing images, build the image, and then confirm that the new image appears.

Ubuntu 24.04 LTS

docker images
docker build -t cleanstata:ubuntu-x86-v19.00 -f stata.Dockerfile .
docker images

Rocky Linux 8

docker images
docker build -t cleanstata:rocky8-x86-v19.00 -f stata.Dockerfile .
docker images

Back to top


7. How do I run Stata in console mode?

The examples below use Stata/SE. If you use Stata/MP, replace stata-se with stata-mp.

Ubuntu 24.04 LTS

# Bind-mount the Stata license when starting the container.
docker run -it -v ./stata.lic:/usr/local/stata19/stata.lic \
    cleanstata:ubuntu-x86-v19.00 stata-se

Rocky Linux 8

# Bind-mount the Stata license when starting the container.
docker run -it -v ./stata.lic:/usr/local/stata19/stata.lic \
    cleanstata:rocky8-x86-v19.00 stata-se

The -it option runs the container in interactive terminal mode. The -v option maps the license file from the host into the container.

Back to top


8. How do I run the Stata(GUI) version of Stata?

Stata(GUI) requires an X Window System (X11) on the host. The container must be able to access the X11 socket and the DISPLAY environment variable.

Ubuntu 24.04 LTS

docker run \
    -v ./stata.lic:/usr/local/stata19/stata.lic \
    -v /tmp/.X11-unix/:/tmp/.X11-unix/ \
    -e DISPLAY="unix$DISPLAY" \
    -it --rm \
    --entrypoint="xstata-se" \
    cleanstata:ubuntu-x86-v19.00 -f0 

Rocky Linux 8

docker run \
    -v ./stata.lic:/usr/local/stata19/stata.lic \
    -v /tmp/.X11-unix/:/tmp/.X11-unix/ \
    -e DISPLAY="unix$DISPLAY" \
    -it --rm \
    --entrypoint="xstata-se" \
    cleanstata:rocky8-x86-v19.00 -f0

In these commands, -v maps files or directories from the host, -e DISPLAY tells the container where to render the GUI, --rm removes the container when it exits, and -it runs the container interactively. The -f0 option launches Stata(GUI) without forking; this is required because a forked process may be terminated when the container entry process exits.

Back to top


9. Stata(GUI) does not open. What should I check?

If Stata(GUI) does not open, verify that the X server is running and that local Docker containers are permitted to connect.

# Verify that an X server is running.
ps -e | grep X

# Temporarily allow connections from any host.
xhost +

# More restrictive: Allow connections from local Docker containers.
xhost +local:docker

After enabling the appropriate X11 access, retry the command to open Stata(GUI). It is preferable to use the more restrictive xhost +local:docker setting when it is sufficient for your environment.

Back to top


10. Why are Python and Java-related libraries included?

The Ubuntu 24.04 LTS and Rocky Linux 8 base images may be missing libraries that are required when using Java or Python within Stata.

On Ubuntu 24.04 LTS, the example includes Python and Java-related libraries. On Rocky Linux 8, the example installs python3.9 and libXtst; these commands can be commented out if they are not needed.

Back to top


11. Can administrators customize the image?

Yes. These examples provide a minimal configuration. Administrators may extend the Dockerfile with additional security or tooling as needed.

Back to top


12. How do I know the setup is complete?

Once the docker run command is executed, Stata should be up and running inside the container.

Back to top