Docker

Docker #

Docker is an open-source platform for containerization that allows you to automate the deployment, scaling, and management of applications within containers. Docker makes it easier to create, test, and run applications consistently across different environments by packaging them and their dependencies into portable, isolated units.

Docker Components #

There are three main components in the Docker ecosystem:

  1. Docker Engine → The core component of Docker that runs and manages containers. It is responsible for creating, starting, stopping, and monitoring containers, as well as managing container images.

  2. Docker Images → Read-only templates that define the application, its dependencies, and the runtime environment. Images are used as the basis for creating containers. You can create your own images or use pre-built images from the Docker Hub, a public registry of images maintained by Docker and the community.

  3. Docker Containers → Instances of Docker images that run in the Docker Engine. Containers are isolated from each other and from the host system, providing a consistent and portable runtime environment for applications.

Installing Docker #

To get started with Docker, you’ll need to install the Docker Engine on your local machine or server. Follow the official Docker installation guides for your specific operating system.

After installation, you should have access to the docker command in your terminal or command prompt.

Basic Docker Commands #

Here are some basic Docker commands to help you get started:

  • docker run: Run a container from an image. If the image is not available locally, Docker will automatically pull it from the Docker Hub.

    docker run --name my_container -p 80:80 -d my_image
    
  • docker ps: List running containers.

    docker ps
    
  • docker stop: Stop a running container.

    docker stop my_container
    
  • docker rm: Remove a stopped container.

    docker rm my_container
    
  • docker images: List available images on your local machine.

    docker images
    
  • docker rmi: Remove an image.

    docker rmi my_image
    
  • docker pull: Pull an image from the Docker Hub.

    docker pull my_image
    
  • docker build: Build an image from a Dockerfile.

    docker build -t my_image .
    

Dockerfile #

The previous command makes use of a Dockerfile. A Dockerfile is a script that contains instructions for building a Docker image. It defines the base image, application code, dependencies, and configurations required to create a container that can run your application. Dockerfiles are written in a simple, declarative language and follow a specific format.

Here’s a brief overview of some common instructions used in a Dockerfile:

  • FROM: Specifies the base image to be used as a starting point for building the new image. This can be an official image from the Docker Hub or another custom image.

    FROM python:3.11
    
  • RUN: Executes a command during the build process, usually for installing packages or setting up the environment.

    RUN apt update && apt install -y curl
    
  • COPY: Copies files from the local filesystem to the filesystem of the image. This is typically used to add your application code to the image.

    COPY . /app
    
  • WORKDIR: Sets the working directory for subsequent instructions such as RUN, CMD, ENTRYPOINT, COPY, and ADD.

    WORKDIR /app
    
  • EXPOSE: Informs Docker that the container will listen on the specified network ports at runtime. This is useful for documentation purposes and does not actually publish the port.

    EXPOSE 8080
    
  • CMD: Provides default arguments that can be executed by the container when it starts. These can be overridden by providing arguments when running the container.

    CMD ["python", "app.py"]
    
  • ENTRYPOINT: Specifies the executable that should be run when the container starts. This is often used in combination with CMD to provide default arguments.

    ENTRYPOINT ["python"]
    CMD ["app.py"]
    

To build a Docker image using a Dockerfile, you can use the docker build command. Navigate to the directory containing the Dockerfile and execute the following command:

docker build -t my_image .

This will instruct Docker to build an image using the Dockerfile in the current directory (.) and tag it with the name my_image.

Once the image is built, you can run containers using the docker run command:

docker run -d -p 8080:8080 my_image

This command will create a new container from the my_image image, run it in detached mode (-d), and map port 8080 on the host to port 8080 on the container.

In the following section, we’ll explore Docker Compose, a tool that simplifies the management of multi-container applications.