Docker on Ubuntu: Install and Compose
Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications using containerization. In this guide, we will explore how to install and use Docker on Ubuntu (Linux) operating system.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Ubuntu (Linux) operating system
- Root or sudo access
Step 1: Install Docker
To install Docker on Ubuntu, follow these steps:
-
- Update the package index:
sudo apt update
-
- Install Docker dependencies:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
-
- Add the Docker GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-
- Add the Docker repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
-
- Update the package index again:
sudo apt update
-
- Install Docker:
sudo apt install docker-ce
-
- Start and enable Docker:
sudo systemctl start docker
sudo systemctl enable docker
Step 2: Verify Docker Installation
To verify that Docker is installed correctly, run the following command:
docker --version
You should see the version of Docker installed on your system.
Step 3: Docker Commands
Here are some commonly used Docker commands:
Command | Description |
---|---|
docker pull <image> |
Pulls an image from a Docker registry |
docker run <image> |
Runs a container from an image |
docker ps |
Lists running containers |
docker images |
Lists available images |
docker stop <container> |
Stops a running container |
docker rm <container> |
Removes a container |
docker rmi <image> |
Removes an image |
Step 4: Dockerfile
A Dockerfile is a text file that contains a set of instructions for building a Docker image. Here’s an example of a simple Dockerfile:
FROM ubuntu:latest
RUN apt update
RUN apt install -y nginx
CMD ["nginx", "-g", "daemon off;"]
To build an image from a Dockerfile, use the following command:
docker build -t <image_name> <path_to_dockerfile>
Once the image is built, you can run a container from it using the docker run
command.
Step 5: Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application’s services, networks, and volumes. Here’s an example of a simple Docker Compose file:
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./html:/var/www/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
To start the application defined in the Docker Compose file, use the following command:
docker-compose up
This will start the containers defined in the file and attach their output to the console.
Conclusion
In this guide, we have covered the basics of Docker on Ubuntu (Linux). You should now have a good understanding of how to install Docker, use Docker commands, create Docker images using a Dockerfile, and run multi-container applications with Docker Compose. Docker provides a powerful and efficient way to manage your applications and their dependencies, making it an essential tool for modern software development.
This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.