Logo

dev-resources.site

for different kinds of informations.

Docker 101: A Guide to Docker Commands, Terminologies & Dockerfile

Published at
1/3/2025
Categories
devops
docker
containers
dockerfile
Author
yash_patil16
Author
12 person written this
yash_patil16
open
Docker 101: A Guide to Docker Commands, Terminologies & Dockerfile

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>
Enter fullscreen mode Exit fullscreen mode

Creating Docker Image , Docker Container Using Docker.

Firstly check if the docker daemon is running in your system using :

systemctl status docker
Enter fullscreen mode Exit fullscreen mode

Image description

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>
Enter fullscreen mode Exit fullscreen mode

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.

Image description

Make sure the DockerFile and the application, in this case Python Script are in the same directory.

Image description

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"]
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

This will create a Docker image tagged as python-hello-world.

Check for the built image using “docker images” command.

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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!

dockerfile Article's
30 articles in total
Favicon
Docker 101: A Guide to Docker Commands, Terminologies & Dockerfile
Favicon
Understanding Docker Compose File Format: Structure, Options, and Best Practices
Favicon
Mastering Dockerfile Syntax: A Complete Guide for Creating Custom Docker Images
Favicon
Understanding Docker Image Layers: Best Practices for Building Efficient Docker Images
Favicon
Best Practices for Writing Efficient and Maintainable Dockerfiles
Favicon
Dockerfile Explain
Favicon
Getting Started with Docker.
Favicon
Building and Deploying a Dockerized Web Application
Favicon
Dive Into Docker: A Hands-On Lab For Getting Started
Favicon
Dockerfile for PHP Laravel
Favicon
Container Files and Dockerfiles: A Comprehensive Guide
Favicon
Understanding Dockerfile: The Blueprint of Docker Containers and Images
Favicon
How to Create Docker Image for HTTPD service, including portfolio web site.
Favicon
Optimized Keycloak build
Favicon
Fun with Avatars: Containerize the app for deployment & distribution | Part. 2
Favicon
The Comprehensive Guide to Dockerfiles
Favicon
[Go Tour ] 2. Optimize Dockerfile
Favicon
Docker II: 5 tópicos para iniciar o mais rápido possível
Favicon
Mastering Dockerfile ️🐳: 10 Advanced Tips and Best Practices ⚔
Favicon
hadolint - Dockerfile linter
Favicon
Dockerfile
Favicon
Creating a GitHub Action for a Docker Image
Favicon
🐳 Boost Your Docker Workflow: Introducing Docker Init for Python Developers 🚀
Favicon
3 Tips to Improve Your Dockerfile Build Time
Favicon
Abrindo mares: Dockerfile, o que precisamos saber!
Favicon
Failed to Read Dockerfile
Favicon
Why i use a smaller docker image
Favicon
Differences Between a DockerFile, Docker Image, and Docker Container
Favicon
Clear up the confusions with Dockerfile instructions
Favicon
How to Create a Dockerfile?

Featured ones: