Using Docker: Basics & Steps

Docker is a platform and toolset designed to make it easier to create, deploy, and run applications using containers. Containers are lightweight, portable, and self-sufficient units that package together an application's code, runtime, system tools, libraries, and settings. Docker provides a consistent environment for running applications across different systems, whether it's a developer's laptop, a testing server, or a production environment.

Installation: First, you need to install Docker on your system. Visit the official Docker website and download the appropriate version for your operating system. Install the Docker Desktop (for Windows and macOS) or Docker Engine (for Linux).

Basic Concepts:

Images: Docker images are read-only templates that contain the application and its dependencies. Images are used to create containers.

Containers: Containers are instances of Docker images. They are isolated and contain everything needed to run an application, including the code, runtime, libraries, and environment variables.

Creating a Dockerfile:

To create a Docker image, you typically start by writing a Dockerfile. This file contains instructions for building the image, including specifying a base image, adding files, setting environment variables, and running commands.

Example Dockerfile for a simple Python application:

FROM python:3.8
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Building an Image:

Use the docker build command to build an image from a Dockerfile.

Navigate to the directory containing your Dockerfile and run:

docker build -t my-python-app .

Running a Container:

Once you have an image, you can create and run containers from it.

Use the docker run command to create a container:

docker run -d -p 8080:80 my-python-app

This runs the container in detached mode and maps port 8080 on your system to port 80 in the container.

Managing Containers:

To view running containers: docker ps

To stop a container: docker stop container_id

To remove a stopped container: docker rm container_id

Sharing Images:

Docker images can be pushed to and pulled from container registries like Docker Hub or other private registries.

Pushing an image: docker push image_name

Pulling an image: docker pull image_name