Docker Tutorials

 



1. Installing Docker on Linux

1.1 Update Your System

Update your package list and upgrade installed packages:

bash
sudo apt-get update sudo apt-get upgrade -y

1.2 Install Required Packages

Install prerequisites for Docker:

bash
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

1.3 Add Docker’s Official GPG Key

Download and add Docker’s GPG key:

bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

1.4 Add the Docker Repository

Add the Docker APT repository:

bash
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

1.5 Install Docker Engine

Update the package database and install Docker:

bash
sudo apt-get update sudo apt-get install -y docker-ce

1.6 Verify Docker Installation

Run the hello-world container to test your Docker installation:

bash
sudo docker run hello-world

1.7 Optional: Manage Docker as a Non-root User

Add your current user to the Docker group to run Docker commands without sudo:

bash
sudo usermod -aG docker $USER

Log out and log back in (or reboot) for this change to take effect.


2. Basic Docker Commands

2.1 Running Containers

  • Run a container from an image (example: hello-world):
    bash
    docker run hello-world

2.2 Listing Containers

  • List running containers:
    bash
    docker ps
  • List all containers (including stopped ones):
    bash
    docker ps -a

2.3 Managing Images

  • List downloaded images:
    bash
    docker images
  • Pull an image from Docker Hub (example: Ubuntu):
    bash
    docker pull ubuntu

2.4 Container Lifecycle Commands

  • Stop a running container:
    bash
    docker stop <container_id>
  • Start a stopped container:
    bash
    docker start <container_id>
  • Remove a container:
    bash
    docker rm <container_id>
  • Remove an image:
    bash
    docker rmi <image_id>

2.5 Executing Commands in a Running Container

  • Open a shell inside a running container:
    bash
    docker exec -it <container_id> /bin/bash
    (If Bash is not available, try sh instead.)

3. Building Your Own Docker Image

3.1 Create a Dockerfile

Create a file named Dockerfile in your project directory. For example, here’s a basic Dockerfile for a Python application:

dockerfile
# Use an official Python runtime as a parent image FROM python:3.8-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 # Expose port 80 for the container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]

3.2 Build the Docker Image

From the directory containing your Dockerfile, run:

bash
docker build -t my-python-app .

This command builds an image named my-python-app using the current directory (the dot .).

3.3 Run Your Docker Container

Run your newly built image and map port 4000 on your host to port 80 in the container:

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

Now, you can access your application at http://localhost:4000.


4. Docker Compose

Docker Compose allows you to define and manage multi-container applications.

4.1 Install Docker Compose

Download and install Docker Compose (check Docker Compose releases for the latest version):

bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose

4.2 Create a docker-compose.yml File

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

yaml
version: "3.8" services: web: build: . ports: - "5000:80" redis: image: "redis:alpine"

This file defines two services:

  • A web service built from your current directory.
  • A Redis service pulled from Docker Hub.

4.3 Using Docker Compose Commands

  • Start services:
    bash
    docker-compose up
  • Start services in detached mode:
    bash
    docker-compose up -d
  • Stop and remove containers, networks, etc.:
    bash
    docker-compose down

5. Advanced Docker Commands and Tips

5.1 Viewing Logs

  • Display container logs:
    bash
    docker logs <container_id>
  • Follow logs in real-time:
    bash
    docker logs -f <container_id>

5.2 Inspecting Containers

  • Get detailed information about a container:
    bash
    docker inspect <container_id>

5.3 Cleaning Up

  • Remove all stopped containers, unused networks, images, and caches:
    bash
    docker system prune
  • Remove all stopped containers:
    bash
    docker container prune

5.4 Tagging and Pushing Images to Docker Hub

  • Tag your image:
    bash
    docker tag my-python-app yourdockerhubusername/my-python-app:latest
  • Log in to Docker Hub:
    bash
    docker login
  • Push the image to Docker Hub:
    bash
    docker push yourdockerhubusername/my-python-app:latest

5.5 Running One-off Commands in a Container

  • Run a temporary container to execute a command (e.g., list directory contents):
    bash
    docker run --rm ubuntu ls -la

Final Thoughts

This guide covers the basics of Docker—from installation and container management to building your own images and using Docker Compose. As you become more familiar with Docker, you can explore topics such as:

  • Networking: How containers communicate with each other.
  • Volumes: Persistent data storage for containers.
  • Docker Swarm and Kubernetes: Advanced orchestration for containerized applications.

For further learning, refer to the official Docker documentation.

Happy Dockering!

Comments