How to install Docker and Run Your Fist Container On Ubuntu
Docker is an open-source platform that allows developers to build, package, and run applications isolated in environments called containers. These containers include everything an application needs to run such as code, libraries, and dependencies. It is lightweight and efficient with resources. Docker gives you the power to spin up containers with thousands of already made containers that can be deployed in a matter of minutes. In this tutorial, we will show you how to install docker, and get your first container up and running. Let's get started!
What you'll need:
- Your host machine (to install docker to) This could be a PC that you've dedicated to hosting, your own personal machine, or a dedicated server.
- Linux or Windows installed on your machine. For this tutorial, we will be using Ubuntu Server. This tutorial assumes you already have installed Ubuntu and have access to the terminal or a shell.
- An internet connection
Step 1:
Update your package index and install dependencies by running:
sudo apt update
Then:
sudo apt install ca-certificates curl gnupg lsb-release -y
Step 2:
Add Docker's GPG Key by running:
sudo mkdir -p /etc/apt/keyrings
Then:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Step 3:
Add the Docker repository to your keyring:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 4:
Update your package index again and install Docker and its components:
sudo apt update
Then:
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Congrats! you have now installed docker! Now, let's make sure its running by using this command:
sudo systemctl status docker
If you want to run Docker without having to use sudo, you'll want to run this:
sudo usermod -aG docker $USER
After this, log out and back in or restart your machine to apply changes.
Now that we have installed docker and all of its components, lets spin up our first container! For this, we will use docker-compose, as it is fairly simple to get pre made containers for a very wide variety of applications we can host, and you can change settings and configuration in a simple to use docker-compose.yml file.
In your home directory, make a new folder for your application. This is not where all of your containers data is stored, but where your config file will live. For this demonstration, we will use Netdata, a self-hosted dashboard for your machines resources. To make a new folder, run:
mkdir Netdata
Then we can move to that folder with:
cd Netdata
Now we can create our compose file. The name of this file will always remain the same no matter what application you are installing. Use:
nano docker-compose.yml
Now, you will be in the text editor for your docker-compose.yml. For Netdata, paste the following configuration:
version: '3.8'
services:
netdata:
image: netdata/netdata:latest
container_name: netdata
hostname: ${HOSTNAME:-netdata} # Shows your host’s name in the dashboard
ports:
- 19999:19999 # Exposes the Netdata web UI on http://localhost:19999
cap_add:
- SYS_PTRACE # Allows Netdata to monitor processes
security_opt:
- apparmor:unconfined # Required for system monitoring on some distros
volumes:
- netdataconfig:/etc/netdata
- netdatalib:/var/lib/netdata
- netdatacache:/var/cache/netdata
- /etc/passwd:/host/etc/passwd:ro
- /etc/group:/host/etc/group:ro
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /etc/os-release:/host/etc/os-release:ro
restart: unless-stopped
volumes:
netdataconfig:
netdatalib:
netdatacache:
Then press CTRL+X, then Y and enter to save and exit the config file. While still in your application directory, run this command to start your container:
docker compose up -d
As you may have noticed in your docker-compose.yml file, your port is set to 19999. This can be changed to whichever port you'd like to use by changing the value in the config. Currently, it's set to:
ports:
- 19999:19999 Say we want port 12345, we can change that config to:
ports:
- 12345:12345 Now, we can connect to our dashboard by using the following URL:
http://yourip:12345
If you aren't sure what your machines local IP is, you can use the following command to get it:
ifconfig
Now look for your adapters local address. Should look something like this: 192.168.0.XXX OR 10.0.0.XXX depending on what type of router you use.
If the command ifconfig is not found, use the following command to install the necessary package, then rerun ifconfig:
sudo apt install net-tools
Here are some common commands you will need to use for Docker.
Bringing a container up or down:
docker compose up -d
And:
docker compose down
Removing a container:
docker rm container_name
Listing all locally stored images on your machine:
docker images
Show which containers are currently running:
docker ps
To list all containers, including stopped ones:
docker ps -a
Now, at this point you should have installed Docker and its dependencies, and started your first container! From here, you can do much more. I would recommend checking out selfh.st for a massive list of applications you can self host, the vast majority you can run on Docker! It would also be a good idea to check out the Docker hub, where you can find exclusively Docker ran applications, and many will show you how how to configure your docker-compose.yml file for that app.
That concludes this tutorial, I hope you enjoyed the read and learned something useful! Good luck in your containerized adventures.