How to Install and Use Docker on Ubuntu (In the Real World)


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Docker is a container management system that makes it easy to run applications in the cloud. It is an open source piece of software that increases development efficiency and provides better scalability for large-scale data processing work. This tutorial will teach you how to install Docker on Ubuntu so you can start using it now!

The “docker ubuntu” is a very popular command-line tool that allows users to create and manage containers. This article will show you how to install Docker on Ubuntu, and then use it in the real world.

How to Install and Use Docker on Ubuntu (In the Real World)

You’ve come to the correct spot if you want to learn how to install Docker on Ubuntu. Not only that, but this article will also show you how to use basic Docker commands to launch and manage containers as a bonus.

You’ll also learn how to improve your SSH abilities by using the advantages of Visual Studio (VS) Code. Let’s get going!

A Windows Guy in a Linux World: Visual Studio Code and Remote SSH


If you want to follow along with this tutorial step by step, make sure you have the following items:

  1. SSH enabled on a new install of Ubuntu Server LTS (Ubuntu Server LTS 20.04.1) will be used in this tutorial.
  2. VSCode installed on a Windows machine (This guide will be using Visual Studio Code 1.52.1)
  3. Installed and connected to your Ubuntu Server LTS installation using the official VSCode SSH addon.

Docker installation on Ubuntu

Let’s get started by first Docker installation on Ubuntu. At this point, the tutorial assumes that you’re on your local Windows computer with VS Code open connected to your Ubuntu Server via SSH. Learn how to set up this handy environment in the latest article on using VS Code and SSH.

VSCode is remotely linked to Ubuntu in the following example, with the home folder (in this instance, /home/homelab) opened as a workspace:

When SSHed into an Ubuntu Machine, VSCodeWhen SSHed into an Ubuntu Machine, VSCode

The actual installation of Docker on Ubuntu Server just takes two commands. Docker is provided as an installation option in Ubuntu’s default package manager, apt.

To install Docker, perform the following two commands in VS Code’s SSH terminal window:

sudo apt install -y sudo apt update -y

You may have been given the opportunity to install Docker as a snap during the Ubuntu Server installation. If you’ve already done so, run sudo snap remove docker to remove the snap package.

In the animation below, you can see how to install Docker:

The Docker Installation Process on UbuntuThe Docker Installation Process on Ubuntu

Ubuntu has been kind enough to enable and start the service on boot for you, so you’re ready to use Docker right now!

Using Ubuntu to Create and Run a Docker Container

What can you do with Docker now that it’s installed? Let’s begin by building a Docker container. As an example of a Docker container, this article will set up a static web server. This section contains the following information:

  • To launch an HTTP service, create a new container from the Docker Hub Image Repository.
  • To map the HTTP port inside the container to your Ubuntu host, use Port Mapping.
  • Bind Mounts may be used to map essential data from the container to your Ubuntu host.
  • Set up your container’s persistence by rebooting it.

Don’t worry if any of the following stages seem complicated; we’ll go through them one by one to assist you comprehend the process.

The Docker Image is being downloaded.

The first issue to consider is where this container will originate. Let’s check out the Docker Hub.

Understanding image repositories is a big aspect of Docker. Instead of being deployed as packages, Docker services are released as Docker Images.

A Docker Image is a snapshot of the program and files system that the publisher intends to deliver! This is similar to building a Windows.wim image.

Docker’s popularity stems from its ability to capture both the program and the operating environment. This avoids the issues brought on by variances in server settings.

The Docker Hub, commonly known as the official Docker repository, is one of the most prominent (and default) image repositories. You may download hundreds of pre-created Docker images to run as containers from the image repository.

You’ll need to download a web server image since this guide is about setting up a static web server. The two most common web servers are Apache httpd and Nginx, but let’s try something different and maybe expose you to a new web server called Caddy.

Caddy is a web server that is noted for its ease of use. A single line in a file may be used to deploy a variety of legal server configurations. Simple is nice, and it also serves as a wonderful example.

  1. You must first locate the Docker image. Go to on your Windows PC.
  2. In the top left corner of the website, type caddy into the search box. You should see something similar to this:

On the Docker Hub, I'm looking for caddy. On the Docker Hub, I’m looking for caddy.

Docker Hub has the advantage (and disadvantage) of allowing anybody, including you, to develop and submit Docker Images.

You must make certain that the picture you choose is from a reputable source. Anyone may opt to include malware in an image and post their version to the Docker Hub.

3. Write down the image’s name. The name caddy is precisely what it seems to be in the screenshot above. This name will be used to provide the picture name in the next phases.

Using Docker to Run a Container on Ubuntu

Once you have the name of the picture you want to download, you may download it and use it to make a container.

A single command is required to start a container from an image. Run the following docker run command on your SSH terminal connected to your Ubuntu Server.

The command below searches the local computer for the caddy image. It gets the image from Docker Hub, builds a container, and starts it up if it doesn’t exist. The -p option is used to map the Ubuntu Server’s listening port 80 to the container’s port 80 in the command below. Port mapping is the term for this capability.

caddy sudo docker run -p 80:80

Most Docker Hub Images follow a naming convention of <user>/<image name>. However, images branded as “official” by Docker do not have a <user> in front of them (like caddy above).

Running the above command produces the following result, with caddy’s log information shown right in the terminal:

Caddy Docker container in useCaddy Docker container in use

You’ll note that practically every docker command starts with sudo, which forces the command to execute as an administrator. The docker service operates as root by default, and any modifications to containers or images must be made as an administrator.

And now you’re ready to go! It’s as simple as that. Aren’t you pleased Docker was installed on Ubuntu?

Now navigate to http://<your-ip> on your Windows computer and you should see a landing page for Caddy. You can see that below (the IP of this guide is replaced with http://homelab-docker):

Caddy's default landing page.Caddy’s default landing page.

The caddy container is now operational, however you may have detected an issue. When you run the docker command, it takes control of your command line. You can’t execute any more commands, and the running container will terminate when the session finishes. Let’s fix this by starting the container in the background (also known as detaching the container).

Docker Containers are running in the background.

Your container is now operating, however your command-line is stuck. There’s nothing more you can do. You need a better approach to launch a container than just running it as a service in the background. To do so, first:

  1. Control+c on the command line will stop the current container. This should return your command line to you.
  2. Rerun the same command as before, but this time using the -d argument, as seen below. Docker will provide a container ID and the command line will be sent back to you.

caddy sudo docker run -d -p 80:80

Using a separate containerUsing a separate container

Docker Commands for Managing Background Containers

You’ll need to manage one or more background Dockers once they’re up and running. Using docker container commands, Docker gives you with a number of options.

  • sudo docker container list -a: Displays a list of all operating and stopped containers, as well as their status.
  • sudo docker container stop <name>: stop a docker container by their name (or by their ID)
  • sudo docker container start <name>: start a docker container by their name (or by their ID)
  • container prune sudo docker: delete and remove all stopped containers

In the image below, you can see all of the following instructions in action:

Using Docker container commands to manage detached containersUsing Docker container commands to manage detached containers

Other docker container commands may be used to view, modify, examine, or even remote enter containers on your server. Running sudo docker container —help will show you all of them.

You still have no method to host your unique content, even if you’ve now installed a web server in a background container. Caddy is currently providing the default site.

Let’s look at how you can deploy containers with meaningful data using Docker images and a notion called bind mounts.

Bind Mounts for Container Data Storage

Docker is based on the idea of images (and the containers they produce) being disposable. If the Caddy software is updated, you do not update the service; instead, you discard the whole baby with the bathtub and recreate it from scratch.

The advantages of removing and repurposing containers like these are substantial. Constantly upgrading (possibly) years old software introduces software instabilities over time. Docker offers a stable, trustworthy, and (supposedly) tested basis for every update by utilizing a new image each time.

Every time you upgrade your Windows application software, this approach is akin to installing Windows from scratch. On Windows, this isn’t a pleasant notion, but it’s perfect for Docker.

However, there is an obvious flaw in the throwaway process. When your present service is blown away, you don’t want important data to be lost. Bind mounts are a notion used by Docker to alleviate this issue.

Now we’ll look at how to make a bind mount for a container.

Cleaning Up and Creating the Folder Structure

You must first build a storage place for this data before you can utilize bind mounts. In your home directory, this guide will create a folder. To do so in VS Code while connected to your Ubuntu Server, follow these steps:

  1. Choose new folder by right-clicking on a vacant space in the VS Code Explorer panel.

2. Give the new folders/caddy/files a name.

The names of the folders are up to you, as long as they are specified appropriately in the docker command that follows. VS Code sees the forward slash as the creation of three folders. Caddy is a subfolder of containers, while containers is a subdirectory of caddy. Although you are not need to utilize this folder structure, it makes more sense when you have numerous containers on the same server.

Stop and delete any containers you made earlier if you haven’t already done so:

# Stop and delete any containers that are presently operating. # The $(sudo docker ps -q) command dynamically collects all currently running container IDs. docker container stop sudo sudo docker container prune $(sudo docker ps -q)

This command can be seen in the image below:

Using the Explorer Window in VSCodeUsing the Explorer Window in VSCode

Using Bind Mounts to Deploy a Caddy Container

The folder structure on the Ubuntu Server is now complete. Now is the time to build a Caddy container with bind mounts.

  1. Before you go any further, find out where the permanent data in the container you’re dealing with is stored. Depending on who generated the Docker image and what function it serves, the location will be different.

The simplest way to figure out where persistent data is saved is to go through the documentation for the image in question on the Docker Hub. The documentation for Caddy may be found here:

Docker Hub's Caddy docsDocker Hub’s Caddy docs

2. Run the command below to start the container. The -v /containers/caddy/files:/usr/share/caddy argument maps the path before the colon (/containers/caddy/files) to the /usr/share/caddy folder within the container. This is quite similar to the port mapping command, however instead of mapping a port, you’re mapping a folder. Bind Mount is the name for this sort of command.

sudo docker run -d -p 80:80 -v /containers/caddy/files:/usr/share/caddy caddy # bind a folder on the host (the path before the colon) to # /usr/share/caddy within the container

In the preceding code, the tilde () refers to the home folder. This is comparable to /home/homelab in this article.

3. Reopen your browser and go back to your server’s http address. The server is currently providing a 404 page, as you can see. Because you don’t have an index.html file in /containers/caddy/files, this is to be anticipated.

4. In the VS Code Explorer window, create an index.html file that looks like this in /containers/caddy/files on the Ubuntu server:

<body><h2>hello world!</h2></body>

5. Go to your server’s HTTP address and verify that the container is now providing your updated index.html page.

All of the above may be seen in the following animation:

Your new index.html page is now being served by container.Your new index.html page is now being served by container.

The files you’re creating (like index.html) don’t need administrator access, unlike the docker management instructions. Because the material served by the caddy server is stored in your home folder, you own it.

The Bind Mount’s Validation

Excellent! You’re not only utilizing a brand new Docker container, but that container is also providing material from your home folder! You may demonstrate this by executing the following commands:

  1. Stop the container from running and delete it. If you weren’t utilizing bind mounts, this step will erase everything, including the index.html file.

docker container stop sudo $$$$$$ (sudo docker ps -q) prune sudo docker container

2. Make a completely new container.

/containers/caddy/files:/usr/share/caddy caddy sudo docker run -d -p 80:80

3. Verify that the new container is still serving the index.html file on the http://<your server> address.

Creating Docker Containers That Last

When the server reboots, a container is rendered useless. If you don’t make it happen, that’s what will happen by default. To avoid this, construct a new caddy container, but this time restart it when the Docker host, in this instance Ubuntu, restarts.

  1. All operating containers should be stopped and removed.

# stop and remove ALL currently running containers docker container stop sudo $$$$$$ (sudo docker ps -q) prune sudo docker container

2. With the —restart always argument, restart the caddy container to have this new container start with the host on reboot. Your container now functions like a genuine service, starting immediately on boot thanks to the —restart always parameter.

caddy —restart always sudo docker run -d -p 80:80 -v /containers/caddy/files:/usr/share/caddy

3. Start the server again.

4. Now verify that the new container comes up and is still serving the index.html file on the http://<your server> address.

The following commands are shown below:

Caddy is set to restart on boot.Caddy is set to restart on boot.

Moving on

You should have a functional Docker setup at this point, as well as a basic grasp of images and containers. You can pull, start, stop, and manage your containers in simple ways. Using bind mounts and port mapping, you’ve also successfully constructed a functioning web service container.

There’s still a lot to learn about Docker: Keep an eye on this place for the upcoming post, which will cover advanced Docker Compose administration.

The “ubuntu install docker-compose” is a tutorial that will show you how to install and use Docker on Ubuntu. It will also provide instructions for installing Docker Compose, which is an alternative to the default Docker command line interface.

Related Tags

  • docker hub
  • ubuntu 22.04 docker image
  • ubuntu docker gui application
  • docker desktop ubuntu
  • gui for docker linux

Table of Content