Docker is a tool that lets you develop, ship, and run applications consistently across different environments. Imagine you write a program using certain tools. Later, if those tools get updated or you try running your code on another device, it might fail or behave unpredictably (witty smile). Docker solves this by replicating the exact environment you developed in, so your application runs the same way anywhere. For applications with a user interface, it also defines ports for interaction.
Beyond this, Docker allows you to maintain multiple isolated environments without relying on traditional virtual environments. Its architecture is fundamentally different from conventional virtualization, as illustrated below. (Source: Internet)
A Docker container can be built from scratch using a Docker image hosted on Docker Hub. The instructions for creating this image are specified in a file called a Dockerfile.
Key Components:
1. Dockerfile: A text file containing instructions to build a Docker image. Common instructions include:
FROM: Specifies the base image to use.
RUN: Executes commands during the image build process.
COPY: Copies files from your local system into the image.
WORKDIR: Sets the working directory for subsequent commands.
CMD: Specifies the command(s) to run when the container starts.
2. Docker image: A blueprint or template for creating containers.
3. Docker container: A running instance of a Docker image, containing everything your application needs to function.
docker pull image_name – download an image from Docker Hub (this does not start a container).
docker run image_name – run a container from the image.
docker run -it image_name – interactive mode
docker run -it --name container_name -d image_name – interactive, detached, with a custom name
docker rmi image_name – remove an image
docker images – list all images
docker exec -it container_name command_name – run a command inside a running container
docker ps – list running containers
docker ps -a – list all containers, including stopped ones
docker rm container_name – remove a container
docker stop container_name – stop a running container
docker start container_name – start a stopped container
docker container prune – remove all stopped containers
docker rename old_name new_name – rename a container
Pro tip: You can bind-mount your container to your local system so that changes in the container immediately reflect on your local machine, and vice versa. It’s like working in two places at once, in perfect sync. My personal setup uses Docker Dashboard integrated with VS Code and necessary extensions.
Once your program is ready, you can create a Docker image and share it with others. This ensures they can run your application in the exact same environment, avoiding dependency conflicts, OS version issues, or library mismatches.
Create a Dockerfile with all the instructions for your application.
Open a terminal in the same directory as your Dockerfile. Build the image using: docker build -t your_image_name:tag
Verify the image was created: docker images
Run a container from your image: docker run -it --name your_container_name your_image_name:tag
Now you can share your Docker image or push it to Docker Hub for others to use, making deployment seamless and consistent.
You can also refer to the following YT video for better understanding and reference.