| 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.
The following topics cover the main steps for building and running Stata in Docker:
Before building a Stata Docker image, make sure the following prerequisites are met:
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 |
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.
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
This approach copies an already installed Stata directory because the Linux version of Stata requires interactive input during installation.
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
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.
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.
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.
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.
Yes. These examples provide a minimal configuration. Administrators may extend the Dockerfile with additional security or tooling as needed.
Once the docker run command is executed, Stata should be up and running inside the container.