Getting Started with Docker: From Dockerfile to Running Containers
2/4/2025


Docker simplifies application deployment by packaging code and dependencies into isolated environments called containers. This tutorial on Docker basics: build and run guides you through creating a Dockerfile, building and tagging Docker images, and running containers.
1. What is a Dockerfile?
A Dockerfile is a text file containing instructions to assemble a Docker image. It defines the base image, installs dependencies, copies application code, and specifies how the container should run the app.
2. Create a Dockerfile
Step 1: Set Up Your Project Directory
Create a project folder to store your application and Dockerfile.
Write a python file with the following content:
Step 2: Write the Dockerfile
Create a file named Dockerfile (without any extension) in the project directory.
Now open the Dockerfile and add the following:
Explanation of Dockerfile Instructions:
FROM: Specifies the base image (Python 3.10 in this case).
WORKDIR: Sets /app as the working directory inside the container.
COPY: Copies files from your host machine to the container.
CMD: Defines the command to run when the container starts.
3. Build and Tag Docker Images
Step 1: Build the Docker Image
Run the following command in the same directory as your Dockerfile:
-t my-docker-app -> Tags the image with the name my-docker-app.
. -> Refers to the current directory (where the Dockerfile is located).
Step 2: Tagging the Docker Image
Tagging helps version your images, especially when pushing to repositories.
Now you have two tags:
my-docker-app (latest)
my-docker-app:v1
4. Run Docker Container
Run the Container
To start a container from your image:
Running in Detached Mode
If you want the container to run in the background:
-d -> Runs the container in the background.
--name -> Names the container my-running-app.
Expected Output:
View Running Containers
To see all containers (including stopped ones):
5. Manage and Troubleshoot Containers
Stopping and Removing Containers
To stop a running container:
Removing Docker Images
To delete an image:
To remove the container:
Bonus: Push to Docker Hub
Login to Docker Hub:
Tag the Image for Docker Hub: Replace "your-dockerhub-username" with your actual Docker Hub username.
Push the Image:
Conclusion
You’ve just created, built, tagged, and run a Docker container! This process is foundational for deploying applications consistently across different environments. As you explore more, try adding more complexity, like multi-stage builds, environment variables, and Docker Compose for multi-container applications.