In this post, we will explain how to install Docker on Ubuntu 22.04 step by step.
What is Docker ?
Docker, an open-source platform, provides a standardized and efficient way to package, distribute, and run applications inside containers in isolated environments. Host on which docker is installed is known docker engine. Docker uses the OS level virtualization and providers container run time environment. In other words, Docker can also defined as PaaS (platform as service) tool.
Components of Docker
- CLI (Docker Command)
- REST API
- Docker Daemon (Server)
Docker comes with two editions , Community (Docker CE) and Enterprise. In this post, we will install docker community edition.
Prerequisites
- Ubuntu 22.04 along with ssh access
- sudo user with privilege rights
- Stable Internet Connection
Let’s deep dive into Docker installation steps on Ubuntu 22.04.
1) Install Docker Dependencies
Login to your Ubuntu 22.04 system and run the following apt commands to install docker dependencies,
$ sudo apt update $ sudo apt install ca-certificates curl gnupg -y
2) Add Docker official APT Repository
Though the docker packages are available in default package repositories of Ubuntu 22.04 but it is recommended to use docker official repository.So, to enable its repository first add Docker’s official GPG key, run
$ sudo install -m 0755 -d /etc/apt/keyrings $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg $ sudo chmod a+r /etc/apt/keyrings/docker.gpg
Next, add the Docker repository to the system’s package sources,
$ echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
3) Install Docker on Ubuntu 22.04
Now that the Docker repository is added, update the package index and install Docker:
$ sudo apt update $ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Once the docker package is installed, add your local user to docker group so that user can run docker commands without sudo,
$ sudo usermod -aG docker $USER $ newgrp docker
Note: Make sure logout and login again after adding local user to docker group
Verify the Docker version
$ docker version
Check docker service status, run below systemctl command
$ sudo systemctl status docker
Above output confirms that docker service is up and running.
Step 4) Verify and Test Docker Installation
In order to test and verify docker installation, spin up a ‘hello-world’ container using below docker command.
$ docker run hello-world
Above docker command will download ‘hello-world’ container image and then will spin up a container. If container displays the informational message, then we can say that our docker installation is successful. Output of above ‘docker run’ would look like below.
Docker Compose Installation on Ubuntu 22.04
To install docker compose on Ubuntu Linux, execute the following commands one after the another
$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose $ sudo chmod +x /usr/local/bin/docker-compose
Check the docker-compose version by running following command,
$ docker-compose --version Docker Compose version v2.20.0 $
Perfect, above output confirms that docker compose of version 2.20.0 is installed.
Test Docker Compose Installation
To test docker compose, let’s try to deploy WordPress using compose file. Create a project directory ‘wordpress’ using mkdir command.
$ mkdir wordpress ; cd wordpress
Create a docker-compose.yaml file with following content.
services: db: image: mariadb:latest command: '--default-authentication-plugin=mysql_native_password' volumes: - db_data:/var/lib/mysql restart: always environment: - MYSQL_ROOT_PASSWORD=sqlpass@123# - MYSQL_DATABASE=wordpress_db - MYSQL_USER=dbuser - MYSQL_PASSWORD=dbpass@123# expose: - 3306 - 33060 wordpress: image: wordpress:latest volumes: - wp_data:/var/www/html ports: - 8080:80 restart: always environment: - WORDPRESS_DB_HOST=db - WORDPRESS_DB_USER=dbuser - WORDPRESS_DB_PASSWORD=dbpass@123# - WORDPRESS_DB_NAME=wordpress_db volumes: db_data: wp_data:
Save and close the file.
As we can see, we have used two containers one for WordPress web and other one is for database. We are also creating the persistent volume for DB container and WordPress GUI is exposed on ‘8000’ port.
To deploy the WordPress, run the below command from your project’s directory
$ docker-compose up -d
Output of above command would like below:
Above confirms that two containers are created successfully. Now try to access WordPress from the Web Browser by typing URL:
http://<Server-IP-Address>:8080
Great, above confirms that WordPress installation is started via docker-compose. Click on Continue and follow screen instructions to finish the installation.
That’s all from this post. I hope you found it useful and informative, please don’t hesitate to share your queries and feedback in below comments section.
For more documentation on docker please refer : Docker Documentation
Also Read : How to Setup Local APT Repository Server on Ubuntu 20.04
What about security after this installation? Docker will change iptables rules as he likes, and ufw will not work anymore.
Hi Andrei,
UFW will work with Docker iptables but for that you need modify ‘/etc/ufw/after.rules’.
Add the following lines at end of file and replace the interface that suits to your setup and restart ufw service
:DOCKER-USER – [0:0]
:ufw-user-input – [0:0]
-A DOCKER-USER -m conntrack –ctstate RELATED,ESTABLISHED -j ACCEPT
-A DOCKER-USER -m conntrack –ctstate INVALID -j DROP
-A DOCKER-USER -i enp0s3 -j ufw-user-input
-A DOCKER-USER -i enp0s3-j DROP
COMMIT
I have ubuntu 20.04, can i install docker that run ubuntu 22.04?
Yes, You can run container with docker image Ubuntu 22.04 on Ubuntu 20.04 host.
Am facing like this error
root@ip-172-31-46-39:~/wordpress/wordpress# docker-compose up -d
validating /root/wordpress/wordpress/docker-compose.yaml: volumes must be a mapping
I got the same error 🙁
Hi Frank,
It appears like an indentation problem in your yaml file. Please cross-check it once.
Sorry, is with tab before the db_data: and wp_data:
volumes:
db_data:
wp_data:
Hi Ruben,
Yes, there two space as per the yaml indentation. I have also corrected the code.