Docker is a powerful tool that allows you to easily create, deploy, and run applications in containers. Containers are lightweight, isolated environments that can run anywhere, making them perfect for development and production environments. In this article, we’ll show you how to install Docker on Ubuntu and start using it to run your applications.
Before we begin, it’s important to note that you’ll need to have Ubuntu 18.04 or later installed on your machine. If you’re running an older version of Ubuntu, you can upgrade by following these instructions: https://ubuntu.com/tutorials/upgrade-ubuntu-desktop
Installing Docker
The first step in installing Docker on Ubuntu is to update your package index. This is done by running the following command in the terminal:
sudo apt update
Once the package index is up-to-date, you can install Docker by running the following command:
sudo apt install -y docker.io
This command will install the Docker package and all of its dependencies. Once the installation is complete, you can start the Docker service by running the following command:
sudo systemctl start docker
You can check the status of the Docker service by running the following command:
sudo systemctl status docker
This command should return output similar to the following:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-01-01 00:00:00 UTC; 1h 11min ago
Docs: https://docs.docker.com
Main PID: 1234 (dockerd)
Tasks: 16
Memory: 47.8M
CPU: 10.5s
CGroup: /system.slice/docker.service
├─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
├─2345 docker-containerd --config /var/run/docker/containerd/containerd.toml
└─3456 docker-containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/1234
This output shows that the Docker service is running and that it has been active for 1 hour and 11 minutes.
Managing Docker as a non-root user
By default, Docker runs as the root
user. However, it’s a good practice to run Docker as a non-root user for security reasons. To do this, you’ll need to create a new user and add them to the docker
group.
To create a new user, run the following command:
sudo adduser <username>
Replace <username>
with the username you want to use.
Once the user is created, you can add them to the docker
group by running the following command:
sudo usermod -aG docker <username>
Replace <username>
with the username you just created.
After you’ve added the user to the docker
group, they’ll be able to run Docker commands without using sudo
.
Running Your First Docker Container
Now that you have Docker installed and configured on your Ubuntu machine, it’s time to start running some containers. To do this, you’ll use the docker run
command.
The docker run
command is used to start a new container from a Docker image. A Docker image is a pre-configured environment that contains everything an application needs to run. You can think of it like a virtual machine, but it’s much more lightweight and efficient.
There are thousands of images available on the Docker Hub, which is a public repository of Docker images. You can search for images on the Docker Hub by going to https://hub.docker.com/.
To start your first Docker container, you’ll use the docker run
command to start a container from the hello-world
image. This image is a simple “Hello, World!” application that is used to test that Docker is working correctly.
To start the container, run the following command:
docker run hello-world
This command will start the container and print some output to the terminal. The output should be similar to the following:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
The output is a message that explains what Docker did to start the container. It shows that the Docker client contacted the Docker daemon, which then pulled the hello-world
image from the Docker Hub, created a new container from that image, and ran the application inside the container.
This is a basic example of how to use Docker to run a container. With this you can run your application in a containerized environment for better security and maintainability.
Conclusion
Docker is a powerful tool that allows you to easily create, deploy, and run applications in containers. By following the steps outlined in this article, you should now have Docker installed and running on your Ubuntu machine. With this, you can run your application in a containerized environment for better security and maintainability.
In this article, we’ve covered the basics of installing Docker on Ubuntu and running your first container. To learn more about using Docker and how to use it to run your own applications, I recommend checking out the official Docker documentation at https://docs.docker.com/.
0 Comments