This is an express tutorial on getting Docker up and running on Ubuntu 20.04 – Fast!
Docker is a virtualization platform that has exploded in popularity due to its ease of use and low resource overheads.
It is used to run applications inside containers, making them portable and easy to install – all of their dependencies and configuration can be kept separate even though they are being run on the same host – reducing the time it takes to configure your system and making sure applications with conflicting requirements don’t affect each other.
Installation
Adding the Docker Repository to apt
First, we’ll need to add the Docker repository to make sure we get the latest version. Run each of the following commands in sequence to install the required dependencies, download the Docker repository GPG key, and finally add the repository:
sudo apt update sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt update
Install Docker
Now install the Docker packages:
sudo apt install docker-ce docker-ce-cli containerd.io
Check Docker is Installed & Running
Docker should have started automatically after running, you can check it with the following command:
sudo systemctl status docker
You’ll know it’s installed and working as you’ll see
Active: active (running) since Sat 2020-12-12 21:22:06 GMT; 1min 55s ago
In the resulting status output.
Verifying Docker Installation
To fully test your new Docker installation, a hello-world container image is available:
sudo docker container run hello-world
This will pull (download) the image if it is not already installed locally, and then run it in a container. The container will output a confirmation message and immediately exit.
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec Status: Downloaded newer image for hello-world:latest 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. (amd64) 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 Docker Command
The docker command controls your Docker service and allows you to launch and manage your containers.
Syntax
docker [OPTIONS] COMMAND [ARG...]
The docker command should be run as root or using the sudo command so that unprivileged users can’t go running or installing virtual machines.
There are a huge number of options and commands to pass to Docker, so I won’t list them all here – the Docker documentation has them all. For this tutorial, we’ll stick with the basics to get you started.
https://docs.docker.com/engine/reference/commandline/docker/
Obtaining Docker Images
Docker Hub
By far the easiest way of getting Docker images is to search the Docker hub. This can be done from the command line, but their web interface is nicer.
Try searching for “WordPress” and you’ll see the official WordPress Docker image in the results. This image contains a full installation of the WordPress CMS ready to use.
Download it by running:
sudo docker pull wordpress
You can see a list of images available locally by running:
sudo docker images
You can remove the downloaded WordPress image using:
sudo docker image rm wordpress
Other Ways to Get Images
You can also create your own Docker images from scratch, but that’s a bit advanced for the scope of this article and unlikely to be something you’ll use unless you’re developing something very complex.
Most of the time you will be able to find an image to base your own Docker images on and then commit your changes to it to create your own custom image. We’ll explore this later in this article.
Managing Docker Containers
Starting Docker Containers
Following from the above example which installs WordPress, start that container with its default configuration using the following command:
sudo docker run wordpress
You’ll see some messages including a message from the Apache webserver with an IP address which can be used to access the WordPress installation running within the container.
Docker images can be launched with variables that are passed to the container, like database credentials or what port to run on.
Good Docker images will have these outlined in their documentation on the Docker Hub. WordPress has these details at
https://hub.docker.com/_/wordpress
Using an example from that page, this command runs the WordPress Docker image with a custom name (some-wordpress) and tells it to forward port 8080 from localhost (the server running Docker) to port 80 within the controller.
This way, you can access the WordPress installation within the Docker container from http://localhost:8080 rather than having to sift through the Docker container output to find the IP address given by Apache.
The -d option is also used so that the container runs in detached mode – running in the background so that when you close the terminal window, the container keeps running.
sudo docker run --name some-wordpress -p 8080:80 -d wordpress
If you don’t give a Docker container a name when running it, Docker will give it a randomly generated name.
You can view a full list of options for the run command, see:
https://docs.docker.com/engine/reference/run/
Listing
You can list running containers using:
sudo docker container ls
Click here for our in-depth look at the ls command in Docker.
Stopping Running Containers
To stop a Docker container, use the stop command. Below will stop the some-wordpress container we started in detached mode above:
sudo docker container stop some-wordpress
Deleting Containers
You must stop a container before deleting it. Once stopped, a container can be deleted by running:
sudo docker container rm some-wordpress
All data relating to the running container will be removed (Though the image used to launch the container will still be available locally).
Committing Changes Made to a Container to a New Image (Creating a Custom Image)
The contents of a running container may be modified – by yourself, or by the code running inside them. These changes will not be reflected in the image used to launch the container – and will be lost if the container is deleted.
You may want to keep these changes for later use or to share – creating a custom image for your own use.
To do this, run:
sudo docker container commit container-name my-image-name
Where container-name is the name of the existing container you have made changes to, and my-image-name is the name for your new custom image.
Note that the image names must be lower case!
Now when you list your images, you’ll see my-image-name in the list – that’s your custom image!
To save this image so you can back it up, or send it to someone, use the save command:
docker save my-image-name > my-image.tar
The save command will output the image as a TAR file as a stream – above we’re saving that stream to my-image.tar.
Conclusion
And there it is – a quick and dirty introduction to using Docker on Ubuntu 20.04. Docker is incredibly versatile and there are hundreds of images for all sorts of applications – blogs, databases, forums, game servers – which can be launched with a single line from the docker command-line interface.
Docker takes the pain out of hosting different applications on a single server by keeping all of their configuration and individual dependencies (which may often conflict with each other) neatly containerized.
The Docker documentation is thorough but easy to follow, and most Docker images come with their own documentation. Now that you have the basics down you can start exploring what Docker can really do.