2022-06-05

Docker Images and Containers

What are Docker Containers and Images

Docker Images are the building blocks of Docker Containers. An Image is a lightweight, stand-alone executable package that includes everything required to run a piece of software, including the code, runtime, system tools, libraries, and settings. Docker Containers are the runtime instances of Docker Images, providing an isolated and consistent environment in which the application can run.

Docker Images and Containers offer several benefits, including:

  • Portability
    Applications run consistently across various platforms, ensuring a smooth deployment process.

  • Resource efficiency
    Containers are lightweight, sharing the host system's resources, resulting in reduced infrastructure costs.

  • Scalability
    Applications can be quickly scaled up or down to meet demand.

  • Version control
    Images can be versioned and rolled back to previous versions if needed.

  • Collaboration
    Teams can share and collaborate on Docker Images using registries like Docker Hub.

Key Terminology

  • Dockerfile
    A script containing instructions for creating a Docker Image.

  • Docker Image
    A lightweight, stand-alone executable package that includes everything required to run a piece of software.

  • Docker Container
    A runtime instance of a Docker Image, providing an isolated environment for applications to run.

  • Docker Registry
    A centralized service for sharing and distributing Docker Images.

  • Docker Hub
    The default public registry for Docker Images, maintained by Docker Inc.

  • Data Volume
    A mechanism for persisting data generated by and used by Docker Containers.

Docker Images

Docker Images are the foundation of Docker Containers. In this chapter, I will delve into the creation and management of Docker Images, as well as how to work with Docker Image registries.

Creating Docker Images

To create a Docker Image, you need to define a Dockerfile and then build the image using Docker commands.

Dockerfile

A Dockerfile is a script that contains instructions for building a Docker Image. It starts with a base image, followed by various commands to configure the environment, install dependencies, and copy application files. Here's an example of a simple Dockerfile:

Dockerfile
# Use the official Python base image
FROM python:3.8

# Set the working directory
WORKDIR /app

# Copy requirements file and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY . .

# Expose the application port
EXPOSE 8080

# Run the application
CMD ["python", "app.py"]

Building a Docker Image

Once you have created your Dockerfile, you can build a Docker Image by running the following command in the same directory as the Dockerfile:

bash
$ docker build -t my_app:1.0 .

This command will create a Docker Image named my_app with the tag 1.0.

Managing Docker Images

After building your Docker Images, you will need to manage them. Common tasks include listing images, removing images, and tagging images.

Listing Images

To list all available Docker Images on your system, run the following command:

bash
$ docker images

Removing Images

To remove a Docker Image, use the following command:

bash
$ docker rmi <IMAGE_ID>

Replace <IMAGE_ID> with the actual ID of the image you want to remove.

Docker Image Registries

Docker Image registries are centralized services for sharing and distributing Docker Images. There are both public and private registries available.

Docker Hub

Docker Hub is the default public registry for Docker Images, maintained by Docker Inc. You can use Docker Hub to search for existing images, store your own images, and collaborate with other developers. To push an image to Docker Hub, you need to first log in:

bash
$ docker login

Next, tag your image with your Docker Hub username and the desired repository name:

bash
$ docker tag my_app:1.0 <USERNAME>/my_app:1.0

Finally, push the image to Docker Hub:

bash
$ docker push <USERNAME>/my_app:1.0

Private Registries

In addition to Docker Hub, you can also use private registries to store and distribute your Docker Images. Private registries can be hosted on-premises or in the cloud, providing additional security and access control options. Popular private registry services include Google Container Registry, Amazon Elastic Container Registry, and Azure Container Registry.

To push an image to a private registry, first tag the image with the registry URL:

bash
$ docker tag my_app:1.0 <REGISTRY_URL>/my_app:1.0

Then, log in to the private registry (if required) and push the image:

bash
$ docker login <REGISTRY_URL>
$ docker push <REGISTRY_URL>/my_app:1.0

Docker Containers

Let's explore Docker Containers, which are the runtime instances of Docker Images. In this chapter, I will cover creating, managing, and working with networking and data storage in Docker Containers.

Creating Docker Containers

Docker Containers can be created using the docker run command, with various options available to customize the container's behavior.

Running a Container

To create and start a new Docker Container from an image, use the following command:

bash
$ docker run -d --name my_container -p 8080:8080 my_app:1.0

This command creates a container named my_container based on the my_app:1.0 image. The -d flag runs the container in detached mode (in the background), and the -p flag maps the host's port 8080 to the container's port 8080.

Interactive Containers

To run a container in interactive mode (useful for debugging and exploration), use the -it flag:

bash
$ docker run -it --name my_container my_app:1.0 /bin/bash

This command starts a new container and opens a Bash shell inside it, allowing you to interact with the container's file system and processes.

Managing Docker Containers

After creating Docker Containers, you will need to manage them. Common tasks include listing containers, stopping containers, and removing containers.

Listing Containers

To list all running Docker Containers, use the following command:

bash
$ docker ps

To list all containers, including stopped ones, add the -a flag:

bash
$ docker ps -a

Stopping Containers

To stop a running Docker Container, use the following command:

bash
$ docker stop <CONTAINER_ID>

Replace <CONTAINER_ID> with the actual ID of the container you want to stop.

Removing Containers

To remove a stopped Docker Container, use the following command:

bash
$ docker rm <CONTAINER_ID>

Replace <CONTAINER_ID> with the actual ID of the container you want to remove.

Networking and Data Storage

Docker Containers can communicate with each other and with the host system through various networking options. Additionally, data generated by containers can be persisted using data volumes.

Container Networking

Docker provides several networking options, including:

  • Bridge
    The default network type, providing a private network between containers on the same host.

  • Host
    Removes network isolation, allowing the container to use the host's network directly.

  • Overlay
    Allows containers to communicate across multiple Docker hosts in a swarm.

  • Macvlan
    Assigns a MAC address to the container, making it appear as a separate physical device on the network.

To create a custom network, use the following command:

bash
$ docker network create --driver bridge my_network

To connect a container to a custom network, use the --net flag when running the container:

bash
$ docker run -d --name my_container --net my_network my_app:1.0

Data Volumes

Data volumes are used to persist data generated by and used by Docker Containers. Volumes are managed by Docker and can be shared between containers.

To create a new data volume, use the following command:

bash
$ docker volume create my_data

To mount a data volume to a container, use the -v flag when running the container:

bash
$ docker run -d --name my_container -v my_data:/data

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!