Docker is a pioneering force in containerization technology. As an open-source platform, Docker has revolutionized how developers package, deploy, and manage applications, ensuring consistency and efficiency across various environments.
Below is a list of features that have made Docker the force it is today:
- Containerization: Docker encapsulates applications and their dependencies within isolated containers, ensuring consistent performance across diverse environments.
- Portability: With Docker, applications encapsulated in containers can be effortlessly transitioned between environments, be it development, testing, or production.
- Resource Efficiency: Docker’s lightweight design ensures minimal resource consumption, optimizing performance and cost.
- Enhanced Security: Docker fortifies containers with robust security features, including isolated filesystems and controlled network access.
- Broad Compatibility: Docker seamlessly integrates with many programming languages, frameworks, and tools, granting developers the flexibility to employ their preferred resources.
Docker harnesses containerization by diverging from conventional virtualization methods that utilize hypervisors for virtual machine management. This methodology enables developers to craft lightweight deployments, simplifying application management and scalability.
This guide will demonstrate the installation process using the APT package manager and Docker’s APT repository.
Pre-installation For Docker
First, remove any older Docker versions that might be installed on your system to prevent conflicts and ensure a smooth installation of Docker on Ubuntu.
Run the following command to eliminate these older versions:
sudo apt remove docker docker-engine docker.io containerd runc
Note: If you have not installed any of these packages, apt will send a message indicating nothing to remove.
Uninstalling Docker does not automatically remove images, containers, volumes, and networks stored in /var/lib/docker/. To start with a clean installation and delete all existing data, use the following commands:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
After removing any older versions of Docker, you should update your system to ensure that all packages are up-to-date and avoid potential conflicts. You can do this by running the following command:
sudo apt update && sudo apt upgrade
This command updates the list of available packages and upgrades any ones that need updating.
Import Docker APT Repository
Before installing Docker, import the Docker repository and GPG key into your system. This ensures that your system can verify the downloaded packages from the repository and prevent unauthorized changes.
To do this, you can first install the required packages by running the following command:
sudo apt install ca-certificates curl gnupg lsb-release dirmngr software-properties-common apt-transport-https
This command installs the necessary packages for importing the Docker repository.
Next, you can import the GPG key using the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg > /dev/null
This command downloads the GPG key from the Docker repository and saves it in the /usr/share/keyrings/ directory.
Finally, you can import the Docker repository by running the following command:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
This command adds the Docker repository to your system’s sources list and configures it to use the GPG key downloaded earlier.
Proceed with the Installation of Docker
Update your system before installing Docker to ensure your source lists accurately reflect the newly imported repository. Run the following command to do this:
sudo apt update
Once your system is up-to-date, you can install Docker using the following command:
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
This command will install Docker and some additional plugins that you may find helpful.
After the installation is complete, you can run the following command to confirm that Docker is working:
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
Example of using hello-world on Docker with Ubuntu:
It’s important to note that the docker command requires root privileges to run. However, you can configure Docker to run as a non-root user, which provides a more secure way to manage containers and images.
If you experience any problems while working with Docker images going forward, try restarting your system, as sometimes this can fix issues related to path generation.
reboot
Managing Docker with systemd
Systemd, a system and service manager, simplifies Ubuntu’s process and service management. Upon Docker installation on Ubuntu, it establishes a systemd unit to oversee the Docker service. Systemd commands can control this unit, offering a convenient method for starting, stopping, and managing Docker containers and images.
Below are some frequently used systemd commands for Docker management on Ubuntu:
systemctl start docker.service
This command starts the Docker service, enabling it to run on system boot.
systemctl stop docker.service
The above command stops the Docker service from running on the system boot.
systemctl restart docker.service
This command restarts the Docker service.
systemctl status docker.service
The command displays the status of the Docker service and whether it is currently running.
systemctl enable docker.service
This command enables the Docker service to start on system boot.
systemctl disable docker.service
This command disables the Docker service from starting on the system boot.
Rundown of Docker Configuration Setup
Manage Docker as a non-root user
When running Docker, avoid using the root user to prevent security risks and accidental changes to the host system; instead, manage Docker as a non-root user.
Use the commands below to create a new user and add it to the Docker group:
sudo useradd -m dockeruser
sudo usermod -aG docker dockeruser
Furthermore, you can add your current username to the Docker group. For instance, if your username is “joshua,” you can execute the following command:
sudo usermod -aG docker joshua
These commands will create a new dockeruser user and add them to the Docker group. After adding the user to the Docker group, the user should log out and back in to apply the changes.
Note: You may need to restart if logging out does not work.
To ensure the user can run Docker commands, use the following command:
docker ps
This command displays a list of running containers, confirming the correct installation of Docker.
Configure default logging driver with Docker
By default, Docker logs to the JSON file format. However, you can configure the default logging driver to use a different format or send logs to a remote log management system.
To change the default logging driver, you can create a new file called daemon.json in the /etc/docker/ directory using a text editor such as nano. For example, you can use the following command to create the file:
sudo nano /etc/docker/daemon.json
Once the file is open, you can add the following contents:
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://logs.example.com:514",
"syslog-facility": "daemon",
"tag": "{{.Name}}"
}
}
In this example, we configure Docker to use the syslog driver and to send logs to a remote syslog server. Replace “logs.example.com” with your syslog server’s address.
After creating the file, you can use the following command to restart the Docker daemon and ensure that the changes made to the logging driver take effect:
sudo systemctl restart docker.service
It’s important to note that if you change the daemon.json file, you must restart the Docker daemon again to reload the changes.
Using the Docker Command on Ubuntu
When working with Docker, one must be familiar with the docker
command used to manage Docker containers, images, networks, and volumes. Here are some of the most commonly used docker
commands:
Command | Description |
---|---|
docker run | Run a new container from an image. |
docker ps | List all running containers. |
docker images | List all available images. |
docker build | Build a new image from a Dockerfile. |
docker stop | Stop a running container. |
docker rm | Remove a container. |
docker rmi | Remove an image. |
docker network | Manage Docker networks. |
docker volume | Manage Docker volumes. |
docker run
The docker run command runs a new container from an image. For example, to run a container from the ubuntu image, you can use the following command:
docker run -it ubuntu:latest /bin/bash
This command will start a new container from the ubuntu image and open a shell inside the container.
docker ps
The docker ps command lists all running containers and provides information about each, including container ID, image name, and status.
docker ps
This command will display a list of all running containers.
docker images
The docker images command lists all available images and supplies information about each image, including image ID, repository, and tag.
docker images
This command will display a list of all available images.
docker build
The docker build command builds a new image from a Dockerfile, a script containing instructions for building an image.
docker build -t myimage:latest .
This command will build a new image called myimage using the Dockerfile in the current directory.
docker stop
The docker stop command stops a running container. For example, to stop a container with the ID abcdefg, use the command:
docker stop abcdefg
This command will stop the container with the ID abcdefg.
docker rm
Use the docker rm command to remove a container. For example, to eliminate a container with the ID abcdefg, execute the following command:
docker rm abcdefg
This command will remove the container with the ID abcdefg.
docker rmi
The docker rmi command removes an image. For example, to eliminate an image with the ID 1234567, use the following command:
docker rmi 1234567
This command will remove the image with the ID 1234567.
docker network
The docker network command manages Docker networks. It offers options to create, list, and remove networks.
docker network create mynetwork
This command will create a new network called mynetwork.
docker volume
The docker volume command manages Docker volumes, providing options to create, list, and remove volumes.
docker volume create myvolume
This command will create a new volume called myvolume.
Running a Docker Container
To run a container from an image, you can use the docker run command followed by the image name. For example, to run a container from the ubuntu image, you can use the following command:
docker run ubuntu
This command will download the ubuntu image and run a container from it. You can also specify the version of the image by adding a tag. For example, to run a container from the ubuntu image version 20.04, you can use the following command:
docker run ubuntu:20.04
This command will download the ubuntu image version 20.04 and run a container from it.
When you run a container, Docker initiates a new image instance as a container. Each container operates in isolation, possessing its own file system, networking, and resources. You can interact with the running container using a shell or command prompt. For instance, execute the command below to open a shell inside the container:
docker run -it ubuntu /bin/bash
Managing Docker Containers
To manage Docker containers, you can use the docker ps command to list all running containers. For example, to list all running containers on your system, you can use the following command:
docker ps
This command will display a list of all running containers, including their container ID, image name, and status.
To stop a running container, you can use the docker stop command followed by the container ID or name. For example, to stop a container with the ID abcdefg, you can use the following command:
docker stop abcdefg
This command will stop the container with the ID abcdefg.
To remove a container, you can use the docker rm command followed by the container ID or name. For example, to remove a container with the ID abcdefg, you can use the following command:
docker rm abcdefg
This command will remove the container with the ID abcdefg.
When you remove a container, you lose all changes made to that container. To save changes as a new image, use the docker commit command, which creates a new image from the container.
Committing Changes in a Container to a Docker Image
When working with Docker containers, changing the container you want to save as a new image is common. You can use the docker commit command to commit changes in a container to a Docker image.
First, start a new container from the base image and make any necessary changes to the container. For example, to start a new container from the ubuntu image and open a shell inside the container, you can use the following command:
docker run -it --name mycontainer ubuntu:latest /bin/bash
This command will start a new container from the ubuntu image and open a shell inside the container. You can make any necessary changes to the container, such as installing new software or modifying configuration files.
Once you have made the necessary changes, you can use the docker commit command to create a new image from the container. For example, to create a new image called myimage with the changes made in the mycontainer container, you can use the following command:
docker commit mycontainer myimage:latest
This command will create a new image called myimage with the changes made in the mycontainer container. You can now use this new image to create and run new containers with the updated software or configuration.
It’s important to note that the docker commit command only saves changes made to the container’s file system and does not save changes to the container’s networking or storage. If you need to save changes to these areas, use other Docker commands, such as docker network or docker volume.
Conclusion
It’s important to remember that Docker is a powerful tool that requires careful configuration and management practices to ensure optimal performance and security. With this in mind, it’s recommended that you continue to explore Docker’s many features and capabilities and stay up-to-date with best practices for managing containers on Ubuntu. Doing so lets you take full advantage of Docker’s many benefits and simplify your application deployment process.
Useful Links
Here are some valuable links related to using Docker:
- Docker Official Website: Visit the official website for information about the container platform, features, and download options.
- Docker Support: Access support resources for help with Docker, including documentation and community assistance.
- Docker Documentation: Explore comprehensive documentation for detailed guides on installing, configuring, and using Docker.
- Docker Training Resources: Access training resources and tutorials to learn how to use Docker effectively.