1. Installing Docker on Linux
1.1 Update Your System
Update your package list and upgrade installed packages:
1.2 Install Required Packages
Install prerequisites for Docker:
1.3 Add Docker’s Official GPG Key
Download and add Docker’s GPG key:
1.4 Add the Docker Repository
Add the Docker APT repository:
1.5 Install Docker Engine
Update the package database and install Docker:
1.6 Verify Docker Installation
Run the hello-world container to test your Docker installation:
1.7 Optional: Manage Docker as a Non-root User
Add your current user to the Docker group to run Docker commands without sudo
:
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):
2.2 Listing Containers
- List running containers:
- List all containers (including stopped ones):
2.3 Managing Images
- List downloaded images:
- Pull an image from Docker Hub (example: Ubuntu):
2.4 Container Lifecycle Commands
- Stop a running container:
- Start a stopped container:
- Remove a container:
- Remove an image:
2.5 Executing Commands in a Running Container
- Open a shell inside a running container:
(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:
3.2 Build the Docker Image
From the directory containing your Dockerfile, run:
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:
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):
4.2 Create a docker-compose.yml
File
Create a file named docker-compose.yml
with content like:
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:
- Start services in detached mode:
- Stop and remove containers, networks, etc.:
5. Advanced Docker Commands and Tips
5.1 Viewing Logs
- Display container logs:
- Follow logs in real-time:
5.2 Inspecting Containers
- Get detailed information about a container:
5.3 Cleaning Up
- Remove all stopped containers, unused networks, images, and caches:
- Remove all stopped containers:
5.4 Tagging and Pushing Images to Docker Hub
- Tag your image:
- Log in to Docker Hub:
- Push the image to Docker Hub:
5.5 Running One-off Commands in a Container
- Run a temporary container to execute a command (e.g., list directory contents):
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
Post a Comment