RUNNING A PROGRAM USING DOCKER AND COMPOSE
From Code to Container: Running a Program Using Docker and Compose
By Ayushi Singh - August 31, 2025
ABOUT DOCKERS
A Docker build is kind of like telling Docker how to make your app into something that can run anywhere — like packing it all into a neat little box. You give Docker your code, maybe some files it needs, and a list of instructions (in a file called a Dockerfile). Then Docker follows those steps — like installing stuff or compiling your code — and builds an image, which is like a ready-to-go version of your app. You can then run this image on any computer that has Docker, and it’ll work the same way every time.It’s a super handy way to avoid the “it works on my machine” problem and keep things nice and portable.
PURPOSE OF THIS PROJECT
When we think of Docker, we usually imagine modern web apps, APIs, or microservices. But for this project, I decided to take a different route — I wanted to see what happens when you bring something as old-school as a C/Python/C++ program into the world of containerization.
The idea was simple: write a tiny C/Python/C++ program that prints a message, and then run it inside a Docker container. No compilers installed locally, no dependencies — just pure isolation and automation. To do this, I used the official GCC Docker image to compile the code and set up a Dockerfile to define the entire build process. Then, I added a docker-compose.yml file to make the setup even smoother and more repeatable with a single command.The steps to be followed are the same for all C/C++/Python programs.
What started as a basic C file quickly turned into a valuable learning experience. This project helped me understand key DevOps principles like reproducibility, where the same code runs the same way on any machine, and portability, where you can move your app from one system to another without hassle. It also introduced me to the automation benefits of Docker Compose, which simplifies managing builds and containers.
In the end, it's a small project with big lessons. Whether you're new to Docker or just curious about mixing classic programming with modern tools, containerizing a C app is a great place to start.
STEP BY STEP PROCEDURE
STEP 1: Create a github account and Download GUI
1.Open any web browser
2.Click on Github and install the 32 bit or 64 bit GUI on your PC based on your computer.
STEP 2: Open git bash
STEP 3: Set up Username and Email using the following command
The commands are:
git config -- global user.”Your git user name”<----command for adding username
git config -- global “your email address”<----command for adding email address
STEP 4-Create the project folder
Fig . 3.
The command mkdir DockerAutomation is used to create a new directory named DockerAutomation.
The command cd DockerAutomation is used to navigate into the DockerAutomation directory. This allows the user to access and manage files specifically related to Docker setup and automation within that folder.
STEP 5:Create the C code
The command nano main.c opens the main.c file in the Nano text editor, allowing the user to write or edit C code directly from the terminal.
Fig . 5.
Save(CTRL+O),click enter and exit(CTRL+X)
The above given code will print an output as “Hello from Dockerized C App"
STEP 6:CREATE THE DOCKER FILE
Fig . 6.
The command nano Dockerfile opens or creates a file named Dockerfile using the Nano text editor in the terminal.
Fig . 7.
Save using(CTRL+O),click enter and exit(CTRL+X)
FROM gcc:latest sets the base image for the Docker container. Here, we use the official GCC (GNU Compiler Collection) image to ensure we have a C compiler inside the container.WORKDIR /app sets the working directory to /app inside the container. Any following commands like copying files or compiling will take place in this directory.COPY main.c . copies the main.c source file from your host machine (current project directory) into the container's /app directory.RUN gcc -o myprogram main.c compiles the C source code using GCC and generates an executable named myprogram.
CMD ["./myprogram"] specifies the default command to run when the container starts. In this case, it executes the compiled C program.
STEP 7:CREATE THE DOCKER COMPOSE.yml FILE
nano docker-compose.yml command opens the docker-compose.yml file in the Nano text editor, allowing the user to define and configure multi-container Docker applications.
Fig . 8.
Fig . 9.
Save(CTRL+O),click enter and exit(CTRL+X)
Docker-compose.yml file defines a service named capp, specifying the build context (.), a custom container name dockerized_c_app, and the image name c-docker-compose-app. It automates building and running the Docker container for a C application using docker-compose.
STEP-8:RUN USING DOCKER COMPOSE
This command docker-compose up --build builds the Docker image as defined in the docker-compose.yml file and starts the container.
Fig . 11.
STEP -9:RE RUN WITH SINGLE COMMAND NEXT TIME
The above written command docker-compose up --build builds the Docker image as defined in the docker-compose.yml file and starts the container.
Fig . 13
The same thing can be done in other languages as well such as C++,Python,Java…we just have to change the main file’s extension and the content of dockerfile and .yml file. An example of C++ file is shown below.
Changes that need to be made through an example of C++ file.
In Step 5(Creation of file and typing the code in it).
Fig . 14.
Fig . 15.
In STEP 6-(CREATE THE DOCKER FILE)
Fig . 16.
Fig . 17.
In STEP 7(CREATE THE DOCKER COMPOSE.yml FILE)
Fig . 18.
Fig . 19.
Make the above mentioned changes and your final output will be as the one shown below which says “Hello from the Dockerized C++ app” which implies that now the C++ file has been run.Similar changes can be made for any other language, for example Python,Java etc.
Fig . 20.
The above image shows the final output displaying “Hello from Dockerized C++ App”.
CONCLUSION
This project started with a simple idea — run a basic C program inside a Docker container — but quickly evolved into a hands-on exploration of DevOps practices, automation, and modern software workflows. Along the way, I learned the nuances of Git configuration, Dockerfile syntax, and how docker compose.yml streamlines the build-run process.
Through trial and error, from typos like remode to syntax errors in docker-compose.yml, every misstep taught me more about debugging and attention to detail. I experienced firsthand the power of containerization: how it abstracts away the host environment and delivers consistent results regardless of where the code runs.
By the end, I had a working system that builds, compiles, and runs a C program in an isolated, reproducible environment — all with a single command. More importantly, I now understand how even low-level applications can benefit from modern tools like Docker and Docker Compose. This project proved that DevOps isn't just for big cloud apps; it's for any developer who wants reliability, portability, and automation.
Whether you're a beginner or brushing up your Docker skills, containerizing a C app is a surprisingly effective and satisfying place to start.

Comments
Post a Comment