Thursday, January 4, 2024

Docker concepts and commands

Here’s a comprehensive guide to Docker concepts and commands, complete with explanations to help you understand each one.

1. Basic Docker Commands

1.1. Check Docker Version


docker --version

Explanation: This command displays the installed Docker version. It's useful for verifying that Docker is installed and checking its version.

1.2. List Running Containers


docker ps

Explanation: Lists all currently running containers. By default, it shows the container ID, image, command, creation time, status, ports, and names.

1.3. List All Containers (including stopped ones)


docker ps -a

Explanation: Lists all containers, both running and stopped. This helps in managing and inspecting containers that are not currently active.

1.4. Pull an Image from Docker Hub


docker pull nginx

Explanation: Downloads the nginx image from Docker Hub (the default image repository). If the image is already on your local machine, Docker will pull the latest version.

1.5. Run a Container


docker run -d -p 80:80 --name webserver nginx

Explanation:

  • -d: Runs the container in detached mode (in the background).
  • -p 80:80: Maps port 80 on the host to port 80 in the container.
  • --name webserver: Assigns the name "webserver" to the container.
  • nginx: Specifies the image to use.

1.6. Stop a Container


docker stop webserver

Explanation: Stops the running container named "webserver." It sends a SIGTERM signal, followed by SIGKILL after a grace period if the container doesn’t stop.

1.7. Remove a Container


docker rm webserver

Explanation: Removes the stopped container named "webserver." The container must be stopped before it can be removed.

1.8. Remove an Image


docker rmi nginx

Explanation: Deletes the nginx image from your local Docker repository. If any containers are using this image, Docker will prevent its removal unless forced.

2. Dockerfile Basics

A Dockerfile is a script used to automate the building of Docker images.

2.1. Simple Dockerfile

Create a file named Dockerfile with the following content:


# Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]

Explanation:

  • FROM python:3.9-slim: Sets the base image to Python 3.9 slim version.
  • WORKDIR /app: Sets the working directory inside the container.
  • COPY . /app: Copies files from the current directory to the container’s /app directory.
  • RUN pip install --no-cache-dir -r requirements.txt: Installs Python dependencies listed in requirements.txt.
  • EXPOSE 80: Documents that the container listens on port 80.
  • ENV NAME World: Sets an environment variable NAME with value World.
  • CMD ["python", "app.py"]: Specifies the default command to run when the container starts.

2.2. Build the Docker Image


docker build -t my-python-app .

Explanation: Builds an image from the Dockerfile in the current directory (.) and tags it as my-python-app.

2.3. Run a Container from the Image


docker run -p 4000:80 my-python-app

Explanation: Runs a container from the my-python-app image, mapping port 4000 on the host to port 80 in the container.

3. Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications.

3.1. Basic docker-compose.yml File

Create a file named docker-compose.yml with the following content:


version: '3' services: web: image: nginx ports: - "8080:80" redis: image: redis

Explanation:

  • version: '3': Specifies the version of Docker Compose syntax.
  • services: Defines the services (containers) to run.
    • web: A service using the nginx image, exposing port 8080.
    • redis: A service using the redis image.

3.2. Start Services with Docker Compose


docker-compose up

Explanation: Starts the services defined in docker-compose.yml. If the images are not available locally, Docker Compose will pull them.

3.3. Stop and Remove Containers with Docker Compose


docker-compose down

Explanation: Stops and removes the containers defined in docker-compose.yml. It also removes the associated networks and volumes.

4. Docker Networking

Docker networking allows containers to communicate with each other and with the outside world.

4.1. Create a Network


docker network create my-network

Explanation: Creates a new network named my-network. Containers connected to this network can communicate with each other.

4.2. Run Containers on a Custom Network


docker run -d --name db --network my-network mongo docker run -d --name app --network my-network my-python-app

Explanation: Runs mongo and my-python-app containers on the my-network network, allowing them to communicate directly.

4.3. Inspect a Network


docker network inspect my-network

Explanation: Shows detailed information about the my-network network, including connected containers and configuration.

5. Docker Volumes

Volumes are used to persist data across container restarts and to share data between containers.

5.1. Create a Volume


docker volume create my-volume

Explanation: Creates a new Docker volume named my-volume. Volumes are stored in a part of the host filesystem managed by Docker.

5.2. Run a Container with a Volume


docker run -d -v my-volume:/data --name my-container nginx

Explanation: Runs a container with the volume my-volume mounted to /data in the container. This allows data to persist and be shared.

5.3. List Volumes


docker volume ls

Explanation: Lists all Docker volumes on the system.

5.4. Inspect a Volume


docker volume inspect my-volume

Explanation: Provides detailed information about the my-volume volume, including its mount point and usage.

5.5. Remove a Volume


docker volume rm my-volume

Explanation: Deletes the my-volume volume. It can only be removed if no containers are using it.

6. Advanced Docker Commands

6.1. Build an Image with Build Arguments


docker build --build-arg MY_ARG=value -t my-image .

Explanation: Passes build arguments to the Dockerfile. You can use MY_ARG in the Dockerfile with ARG directive.

6.2. Tag an Image


docker tag my-image my-repo/my-image:latest

Explanation: Tags an image (my-image) with a new name and tag (my-repo/my-image:latest). This helps in organizing and managing images.

6.3. Push an Image to Docker Hub


docker push my-repo/my-image:latest

Explanation: Uploads the tagged image to Docker Hub or another Docker registry.

6.4. Pull an Image from a Private Repository


docker login docker pull my-repo/my-image:latest

Explanation: Logs into a Docker registry and pulls an image from it. Authentication is required for private repositories.

6.5. Create and Manage Docker Swarm


docker swarm init docker service create --name my-service -p 80:80 nginx docker service ls docker service ps my-service docker service rm my-service docker swarm leave --force

Explanation:

  • docker swarm init: Initializes a Docker Swarm cluster.
  • docker service create --name my-service -p 80:80 nginx: Creates a new service named my-service using the nginx image.
  • docker service ls: Lists all services in the swarm.
  • docker service ps my-service: Lists tasks (containers) of the specified service.
  • docker service rm my-service: Removes the specified service.
  • docker swarm leave --force: Forces the current node to leave the swarm.

7. Debugging and Logs

7.1. View Container Logs


docker logs my-container

Explanation: Displays the logs of the specified container, useful for debugging issues.

7.2. Attach to a Running Container


docker attach my-container

Explanation: Attaches your terminal to the running container’s process, allowing you to interact with it directly.

7.3. Exec into a Running Container


docker exec -it my-container /bin/bash

Explanation: Opens an interactive terminal session (-it) inside the container, allowing you to run commands directly.

This guide covers a broad range of Docker commands and concepts, giving you a solid foundation to work with Docker in various scenarios. 

No comments:

Post a Comment