In this article, we will learn about how to write a docker file to package and run an application in a container. Containers are lightweight isolated environment and contain everything that needed to run the application, so you don't need to rely on what's installed on the host. You can share containers while you work and be sure that everyone you share with gets the same container that works in the same way.
Some of the most common instructions in a Dockerfile
include:
FROM
- Represents base image to create our own imageLABEL
- Add metadata to an image.COPY
- To copy files from source to docker image foldersADD
- Add local or remote files and directories (Eg: url to destination folder)RUN
- Executes commands to build layers while creating the imageCMD
- To run while creating the containerENTRYPOINT
- To set executables that will always run when the container is initiated.ENV
- Set environment variables for container to useARG
- Use to pass values during run timeuser
- To set user and group IDLABEL
- Add metadata to an imageWORKDIR
- To set the working directory for image/containerEXPOSE <port-number>
- Describe which ports your application is listening on.VOLUME
- Create Volume Mounts
- An example of docker file for a Python GUI app that displays on host machine's display
# Base image to build a container
FROM docker.io/library/python:3.8
# Meta data information like author information
LABEL org.opencontainers.image.authors="yourname@mail.com"
# Setting up the current work directory
WORKDIR /app
# Install the application dependencies
RUN pip install tk
# Copy in the source code
COPY . .
EXPOSE 5000
# specifies which ports the container listens on
# In our case we don't require this 'expose' as we are going to use host dispaly to display our GUI app
ENTRYPOINT ["python3","weight_converter.py"]
# Command to run when a container starts
To build a docker image using a Dockerfile
sudo docker build -t weight_converter:1 .
To tag an image during a build, add the -t or --tag flag then add a name with version
The final . in the command provides the path or URL to the build context. At this location, the builder will find the Dockerfile and other referenced files. When you run a build, the builder pulls the base image, if needed, and then runs the instructions specified in the Dockerfile.
Once the build has completed, you can view the image by using the following command:
docker image ls
- You can actually view the history (or how the image was created) by using the
docker image history
command
docker image history weight_converter:1
- To start this container to access the application
sudo docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix weight_converter:1
For a normal web server/app, we just need to expose a port using
-p <hostport>:<Container Port>
then we can access the application from your browser by visitinghttp://localhost:<hostport>
in your browser.Since this is a python GUI application, we need to set up the Docker container with GUI support using X11.
Please be aware of allowing docker container to have access to the X11 socket can expose your entire graphical session to the container, which might lead to privacy issues.
To see all containers:
docker ps -a
sudo docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix weight_converter:1
Let's understand the arguments we have passed in the above line:
-e DISPLAY=$DISPLAY
: Passes the host machine's display environment variable into the container, which is necessary for the GUI to display on the host.
-v /tmp/.X11-unix:/tmp/.X11-unix
: Mounts the X11 Unix socket from the host into the container, allowing the container to communicate with the X server.
weight_converter:1
: Replace this with the name of your Docker image that contains the GUI application.
Tip: The command lsof -U
can tell you what processes are using which Unix domain sockets.