How to Install Docker and Docker Compose on Ubuntu 22.04 Linux

Docker and Docker Compose are two amazing tools that let you run containerized applications on Ubuntu (and every other flavor of Linux). Docker is super simple to use, and it’s fantastic for testing new programs. Although Docker is available on Windows, I find the entire experience much smoother on Linux-based systems. It’s stable and reliable, and there are some great tools you can install that make it very easy to manage. Lets learn how to Install Docker, and how to install WordPress using Docker Compose.

This procedure will show you how to install Docker and Docker Compose. I will also demonstrate some of my favorite Docker images from the Docker hub that you can play around with.

Step 1 – Update Server

First, update your Linux Server. This process will take a little while if you have just installed Ubuntu.

Bash
sudoaptupdate-y

Step 2 – Install Docker

The following commands will install all the prerequisites for Docker and Docker Compose. It includes the GPG key for the Docker Repo. Bare in mind that occasionally, Docker will change this repo, so if you hit any errors, visit https://download.docker.com/linux/ubuntu/gpg to see if anything has changed.

Bash
# Install prerequisitessudoaptinstallapt-transport-httpsca-certificatescurlsoftware-properties-commongnupglsb-release-y# Add Docker's official GPG key:curl-fsSLhttps://download.docker.com/linux/ubuntu/gpg| sudogpg--dearmor-o/usr/share/keyrings/docker-archive-keyring.gpg# Add Docker's repository:echo"deb [arch=$(dpkg--print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release-cs) stable"| sudotee/etc/apt/sources.list.d/docker.list> /dev/null# Update the package index again:sudoaptupdate# Install Docker Engine, containerd, and Docker CLI:sudoaptinstalldocker-cedocker-ce-clicontainerd.iodocker-buildx-plugindocker-compose-plugindocker-compose-y

Step 3 – Verify Docker Installation

Now, we need to quickly check everything was installed correctly. To do this, we simply run a hello-world test.

Bash
sudodockerrunhello-world

You should see the following output:

Bash
sudodockerrunhello-worldUnabletofindimage'hello-world:latest'locallylatest:Pullingfromlibrary/hello-worldc1ec31eb5944:PullcompleteDigest:sha256:266b191e926f65542fa8daaec01a192c4d292bff79426f47300a046e1bc576fdStatus:Downloadednewerimageforhello-world:latestHellofromDocker!Thismessageshowsthatyourinstallationappearstobeworkingcorrectly.Togeneratethismessage,Dockertookthefollowingsteps:1.TheDockerclientcontactedtheDockerdaemon.2.TheDockerdaemonpulledthe"hello-world"imagefromtheDockerHub.(amd64)3.TheDockerdaemoncreatedanewcontainerfromthatimagewhichrunstheexecutablethatproducestheoutputyouarecurrentlyreading.4.TheDockerdaemonstreamedthatoutputtotheDockerclient,whichsentittoyourterminal.Totrysomethingmoreambitious,youcanrunanUbuntucontainerwith:$dockerrun-itubuntubashShareimages,automateworkflows,andmorewithafreeDockerID:https://hub.docker.com/Formoreexamplesandideas,visit:https://docs.docker.com/get-started/root@AIML:~#

Step 4 – Deploy WordPress

Now, we will deploy WordPress using Docker Compose. First, we need to set the local host filesystem, then we can deploy the WordPress Container

Note: If you are following along at home, make sure you change the Password to something more secure.

Bash
nanodocker-compose.yml

Add the following code

Dockerfile
version: '3.1'services:db:image: mysql:5.7volumes:- db_data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: yourpasswordMYSQL_DATABASE: wordpressMYSQL_USER: wordpressuserMYSQL_PASSWORD: yourpasswordwordpress:image: wordpress:latestports:- "8080:80"restart: alwaysenvironment:WORDPRESS_DB_HOST: db:3306WORDPRESS_DB_USER: wordpressuserWORDPRESS_DB_PASSWORD: yourpasswordWORDPRESS_DB_NAME: wordpressdepends_on:- dbvolumes:db_data:

Save and quit

Now use the following command

Bash
docker-composeup-d

You should see the following output:

Bash
docker-composeup-dCreatingnetwork"root_default"withthedefaultdriverCreatingvolume"root_db_data"withdefaultdriverPullingdb(mysql:5.7)...5.7:Pullingfromlibrary/mysql20e4dcae4c69:Pullcomplete1c56c3d4ce74:Pullcompletee9f03a1c24ce:Pullcomplete68c3898c2015:Pullcomplete6b95a940e7b6:Pullcomplete90986bb8de6e:Pullcompleteae71319cb779:Pullcompleteffc89e9dfd88:Pullcomplete43d05e938198:Pullcomplete064b2d298fba:Pullcompletedf9a4d85569b:PullcompleteDigest:sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bbStatus:Downloadednewerimageformysql:5.7Pullingwordpress(wordpress:latest)...latest:Pullingfromlibrary/wordpress09f376ebb190:Pullcomplete76afcdc86551:Pullcompleteceed4541c527:Pullcomplete9ec84be954b0:Pullcompleteff0e278869f9:Pullcomplete1693466e4cc6:Pullcomplete57c8d94a4882:Pullcomplete43af3fe8136a:Pullcompleteddef75e08a5d:Pullcompletea4ba0bdbbf0a:Pullcomplete29e89bc69515:Pullcompletebdad27815722:Pullcompletedbbbc9a88332:Pullcompleteca4ce74a758b:Pullcompleteb8447b6adfa2:Pullcomplete6384f9388f98:Pullcompletead26f7163064:Pullcomplete4b2fddf63d23:Pullcomplete5967a1599e89:Pullcompletecabecef25649:Pullcompletec9f0fbb308c3:PullcompleteDigest:sha256:f468bab53528df6f87dfe11a80de26eff57e0f515e243d9dec73a02c80c273a7Status:Downloadednewerimageforwordpress:latestCreatingroot_db_1...doneCreatingroot_wordpress_1...done

You can now browse to your Server IP on port 8080 to see the WordPress Installation page

Basic Docker Commands

Image Management:

  • docker images: List all Docker images on your system.
  • docker pull <image>: Download a Docker image from a registry (e.g., Docker Hub).
  • docker search <term>: Search for images on Docker Hub.
  • docker build -t <image_name> <path_to_Dockerfile>: Build a Docker image from a Dockerfile.
  • docker rmi <image>: Remove an image from your local system.

Container Management:

  • docker run <image>: Create and start a new container from an image.
  • docker ps: List running containers.
  • docker ps -a: List all containers (running and stopped).
  • docker start <container>: Start a stopped container.
  • docker stop <container>: Stop a running container.
  • docker restart <container>: Restart a container.
  • docker rm <container>: Remove a stopped container.
  • docker logs <container>: View the logs of a container.
  • docker exec -it <container> <command>: Execute a command inside a running container.
  • docker cp <container>:<path> <local_path>: Copy files between a container and your local machine.

Docker Compose:

  • docker-compose up -d: Start all services defined in a docker-compose.yml file.
  • docker-compose down: Stop and remove all containers, networks, and volumes defined in a docker-compose.yml file.
  • docker-compose ps: List containers managed by Docker Compose.

Other Useful Commands:

  • docker version: Show the Docker version information.
  • docker info: Display system-wide information about Docker.
  • docker login: Log in to a Docker registry.
  • docker system prune: Remove unused images, containers, networks, and build cache.

Thats it, you now know how to install Docker, and use Docker Compose to install WordPress with a MySQL backend. There are loads more things to install on the Docker Hub, you can also find out moreLinux hints and tips on our website.

Thanks for taking the time to read this article. if you have any questions or feedback, please write in the comment section below.

Elsewhere On TurboGeek:  Bash CLI Terminal Tips and Tricks

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

1 Response

  1. 28/05/2024

    […] Minecraft is one of the most popular games ever made. Did you know you can host your own private server that you and your mates can play on? Checkout of this procedure of how to create a Minecraft server on Ubuntu 22.04 […]

Leave a Reply

Your email address will not be published.Required fields are marked *

Translate »