Docker on Linux enables you to use containerization technology to package, distribute, and run applications efficiently. This guide covers the essential steps to install Docker, work with containers and images, and create your own custom Docker images.
How to Get Started
-
Step 1: Install Docker Engine
Docker Engine is available for various Linux distributions. Install it using your distribution's package manager.
For Debian/Ubuntu-based systems:
sudo apt update sudo apt install docker.ioFor Fedora/RHEL-based systems:
sudo yum install docker -
Step 2: Start and Enable Docker Service
After installation, start the Docker service and enable it to run at boot time:
sudo systemctl start docker sudo systemctl enable docker -
Step 3: Verify Installation
Check the Docker version to ensure it's installed correctly:
docker --version -
Step 4: Pull Docker Images
Docker images are templates for containers. You can pull images from Docker Hub, a public repository of Docker images, or other image registries.
For example, to pull the official Ubuntu image:
docker pull ubuntu -
Step 5: Run Your First Container
Start a container from a Docker image using the
docker runcommand. For example, to run a basic Ubuntu container:docker run -it ubuntuThis command will start an interactive terminal session (
-it) in an Ubuntu container. Once inside, you can perform various operations, such as installing software, running commands, and modifying files. To exit the container without stopping it, use theexitcommand. -
Step 6: List Running Containers
View the list of running containers using the
docker pscommand. To see all containers (including stopped ones), add the-aflag:docker ps docker ps -a -
Step 7: Stop and Remove Containers
Stop a running container using its ID or name:
docker stop <container_id_or_name>Remove a stopped container:
docker rm <container_id_or_name> -
Step 8: Create a Dockerfile
Dockerfiles are text files that contain instructions for building Docker images. You can create custom Docker images by writing Dockerfiles.
Start by creating a new directory and a Dockerfile inside it. Here's a simple example Dockerfile for a Node.js application:
FROM node:latest WORKDIR /app COPY . . RUN npm install CMD ["node", "app.js"] -
Step 9: Build Custom Images
Build a Docker image using the
docker buildcommand. Navigate to the directory containing the Dockerfile and run:docker build -t my-node-app .This command will build an image named
my-node-appusing the Dockerfile in the current directory (.). -
Step 10: Run Containers from Custom Images
Once you've built a custom image, you can run containers from it just like you did with pre-built images:
docker run -d my-node-app -
Step 11: Publish Images to Docker Hub (Optional)
If you've created a useful Docker image, you can publish it to Docker Hub or another image registry to share it with others:
docker login docker tag my-node-app username/my-node-app docker push username/my-node-app -
Step 12: Explore Docker Compose (Optional)
Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to configure the application's services and dependencies. Install Docker Compose and use it to manage complex multi-container setups.