Introduction
Docker is a powerful tool for creating, deploying, and running applications in lightweight, portable containers. Containers allow developers to package an application along with its dependencies, making it easy to move and run across different environments. If you're just getting started with Docker, this blog will guide you through the basics, essential commands and how to create a Dockerfile to build your first containerized application.
To get a deeper understanding of Docker and its evolution, check out my first article on Docker.
Installation Guide
Before diving into the basics of Docker, let's first ensure Docker is installed on your machine. Docker can be installed on Windows, macOS, and Linux. For the most up-to-date instructions and installation files, visit the official Docker documentation.
Initially if we are using an EC2 instance, then by default the user doesn’t have permission to execute docker binaries. We have to add the user to the Docker group using :
sudo usermod -aG docker <name of user on EC2>
Creating Docker Image , Docker Container Using Docker.
Firstly check if the docker daemon is running in your system using :
systemctl status docker
Make sure that status is active(running), else there might be some issue with the installation.
Now lets go over basic commands before diving deep.
# Check Docker version
docker --version
# Pull an image from Docker Hub
docker pull <image_name>
# List all containers (running or stopped)
docker ps -a
# Run a container from an image
docker run <image_name>
# Build an image from a Dockerfile
docker build -t <image_name> <path_to_dockerfile>
# List all images
docker images
# Stop a running container
docker stop <container_id>
# Remove a stopped container
docker rm <container_id>
# Remove an image
docker rmi <image_name>
DockerFile
Now lets create our first image using a very basic dockerfile for a basic python script.
The python script is a simple script to print “Hello World!” message.
Make sure the DockerFile and the application, in this case Python Script are in the same directory.
Now lets go over the Dockerfile. A Dockerfile is a template to create images for our applications and its consists of instructions to define the application’s environment, dependencies, and how to access the application.
FROM python:3.14.0a3-alpine3.21
WORKDIR /app
COPY . /app
CMD ["python3", "app.py"]
Let’s go through each instruction in the Dockerfile:
FROM: This instruction sets the base image for your container.
Here, we're using the python:3.14.0a3-alpine3.21 image, which
is a lightweight Alpine-based image with Python 3.14.0a3
installed.WORKDIR: This sets the working directory inside the container to
/app. All subsequent instructions will be executed in this
directory.COPY: This command copies all the files from your local directory
(where your Dockerfile resides) into the /app directory
inside the container.CMD: This command specifies the default command to run when the
container starts. Here, it tells Docker to run the app.py
script using Python 3.
Build the Docker image:
With the Dockerfile and app.py in the same directory, open a terminal and run the following command to build the image:
docker build -t python-hello-world .
This will create a Docker image tagged as python-hello-world.
Check for the built image using “docker images” command.
As we can see from the output an image tagged “python-hello-world” has been created.
Run the container:
After building the image, you can run the container with this command:
docker run python-hello-world
This confirms that your Python script is running inside a Docker container.
Conclusion
In this blog, we've covered the basics of Docker: basic commands, and how to write a Dockerfile. We also demonstrated how to use a simple Dockerfile to run a Python "Hello World" script inside a container. Docker makes it easier to manage and deploy applications by encapsulating everything needed to run the application in a container.
As you continue learning Docker, you’ll discover more advanced techniques and tools. Happy containerizing!
Top comments (0)