Share this page
Linux VPS illustration

Install Docker on Ubuntu Server: A Secure First Container

Linux VPSBy Peter French

Install the official Engine and Compose packages, verify a first container, and keep privilege, published ports and application data under deliberate control.

Install Docker Engine from Docker’s official apt repository, verify the daemon with hello-world, and inspect a named test container before deploying an application. Keep Docker access restricted to trusted administrators and choose port bindings deliberately: adding a user to the Docker group grants root-level privileges, while publishing a container port can bypass UFW filtering.

This guide uses Ubuntu Server 24.04 LTS on amd64, an administrator with sudo, and Docker’s default bridge networking. Run each shell command separately in an interactive Bash terminal on that server. Read its result before continuing; these commands are not an unattended installation script. The outcome is a verified installation and a bounded first-container exercise, not a guarantee that a future application is secure.

Check the server before changing packages

Docker’s Ubuntu installation documentation lists Ubuntu 26.04 LTS, 24.04 LTS and 22.04 LTS, and identifies amd64, armhf, arm64, s390x and ppc64le architectures. The concrete repository file below is specifically for Noble 24.04 and amd64; do not paste it onto a different release or architecture. Docker does not officially support Ubuntu derivatives through this installation procedure.

Read /etc/os-release with cat /etc/os-release, then run dpkg --print-architecture. Continue with the literal repository example only when the release is Ubuntu 24.04, its codename is noble, and the architecture output is amd64. Keep your administrative session and recovery access available throughout the work.

On an existing server, identify applications already using containers before removing packages or restarting services. Record their deployment configuration, data locations and recovery procedure. A fresh-server tutorial is not permission to interrupt an existing workload. If you find an established container platform, plan its migration separately rather than replacing its runtime as a preparation step.

Docker lists these conflicting packages: docker.io, docker-compose, docker-compose-v2, docker-doc, docker-buildx and podman-docker. Separately installed containerd and runc also conflict with Docker’s bundled containerd.io. Inspect the package selections first:

List conflicting container packages
dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc

Review the result and remove only installed conflicting packages using sudo apt remove followed by their exact names. Read apt’s proposed changes before confirming. Stop if removal would disrupt a workload you have not prepared to migrate. If none are installed, there is nothing to remove. Package removal does not automatically erase Docker’s stored images, containers, volumes or networks; do not delete existing data to make the installation appear clean.

Add the official repository

First run sudo apt update. Resolve failed repository or signature checks before moving on. Install the download and certificate tools:

Install repository transport prerequisites
sudo apt install ca-certificates curl

Inspect any existing Docker repository entries and signing-key file before changing them. This procedure uses /etc/apt/sources.list.d/docker.sources and /etc/apt/keyrings/docker.asc. If those paths already contain a configuration, preserve and reconcile it; do not create competing entries or overwrite an unexplained symlink. The following steps implement Docker’s documented repository method.

Create the keyring directory:

Create the APT keyring directory
sudo install -m 0755 -d /etc/apt/keyrings

Download Docker’s official key. Continue only if the download completes successfully:

Download Docker signing key
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

Make the key readable by apt:

Make the Docker signing key readable
sudo chmod a+r /etc/apt/keyrings/docker.asc

Using your privileged text editor, create or deliberately update /etc/apt/sources.list.d/docker.sources with the following contents. This is configuration text, not a shell command. The fixed suite and architecture match the checks above; Signed-By points apt to the downloaded key.

Docker APT source definition
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: noble
Components: stable
Architectures: amd64
Signed-By: /etc/apt/keyrings/docker.asc

Save the file, then run sudo apt update again. The repository change takes effect through that index refresh. Resolve any errors involving the suite, key or duplicate entries before installing packages. Disabling signature verification is not a fix for a broken repository configuration.

Install Engine and the Docker Compose plugin

Install the official package set:

Install Docker Engine and Compose packages
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

This installs Docker Engine, its CLI, the bundled container runtime, Buildx and the Docker Compose plugin. Review apt’s proposed changes and let the installation finish. Check the service:

Inspect Docker service status
sudo systemctl status docker

Look for a running service. If it is not running, Docker documents sudo systemctl start docker; inspect the service status again after that command. If startup fails, read the reported failure before changing networking or permissions. A daemon that did not start cannot run the container test.

Check the installed Compose plugin using sudo docker compose version. The expected result is a Compose version, not “unknown command”. The current plugin uses docker compose with a space; this guide does not install the older standalone docker-compose command. A version response confirms plugin availability, not that an application’s Compose configuration works.

Run and inspect the first container

Use a unique, recognisable name for the test. Inspect existing containers with sudo docker ps -a before proceeding; if virtarix-hello-test already belongs to another task, choose a different name and use it consistently. Do not delete an unfamiliar container to free the name.

Run the named hello-world container
sudo docker run --name virtarix-hello-test hello-world

Docker’s documented test downloads the image if needed, runs it, prints a confirmation message and exits. That is the expected pattern for this command. An exited hello-world container is therefore normal; it is not a service that should remain running. The explicit name makes the next inspection and removal target clear.

Inspect the saved container record:

Inspect the completed test container
sudo docker container inspect virtarix-hello-test

Read its state and image information, then use sudo docker logs virtarix-hello-test to view the output. Distinguish downloading an image, starting a process and receiving its successful result. A download error does not establish a daemon failure, and a running daemon alone does not prove that a container executed correctly.

Once you have inspected the test, remove that stopped container:

Remove the completed test container
sudo docker container rm virtarix-hello-test

This targets the named container. Do not replace it with a command that removes every stopped container on the host. Container removal also removes its writable container layer; persistent application data needs an intentional volume or bind-mount design. The disposable hello-world exercise is not a backup or recovery test.

Test a deliberately restricted port binding

The hello-world check does not publish an application port. For a separate web-container exercise, use the default bridge network and bind only to the host’s IPv4 loopback address. Docker documents this form for nginx. The example host port 8080 maps to container TCP port 80; these are exercise settings, not a production deployment prescription.

Use Docker Engine 28.0.0 or newer for this exercise. Docker documents that older releases could expose localhost-published ports to other hosts on the same layer-2 network. Inspect your installed Engine version with sudo docker version before proceeding. This exercise also assumes you have not enabled custom direct routing or changed the bridge gateway mode; those settings change the access model.

After checking that the container name and host port are unused, start the test:

Run a loopback-only Nginx container
sudo docker run -d --name virtarix-web-test -p 127.0.0.1:8080:80 nginx

Inspect its actual bindings:

Inspect the Nginx port binding
sudo docker container inspect virtarix-web-test --format '{{json .NetworkSettings.Ports}}'

The mapping should associate container 80/tcp with host IP 127.0.0.1 and host port 8080. On the server itself, run curl -I http://127.0.0.1:8080 and look for an HTTP response. Then, from a separate machine on the network you intend to exclude, try the server’s externally reachable address on port 8080. It should not expose this container. If it does, stop the named test and investigate the actual Docker/network configuration before deploying an application.

Do not replace the binding with -p 8080:80 casually: omitting the host IP publishes to all host addresses by default, potentially exposing the service externally. IPv4 and IPv6 are distinct access paths; inspect every binding you create. Testing the server’s loopback URL from your laptop tests your laptop, not the server.

Docker creates firewall rules for bridge networking. Published traffic can be diverted before it reaches the UFW chains that would otherwise filter it. Consequently, a reassuring UFW status does not prove a published Docker port is restricted. Keep Docker’s networking rules intact, use intentional bindings, and test the actual access path. This guide uses the default networking path rather than changing Docker’s firewall backend.

Stop the disposable web test after checking its identity:

Stop the Nginx verification container
sudo docker container stop virtarix-web-test

Check that it stopped, then separately run sudo docker container rm virtarix-web-test. Avoid forced removal and volume-removal flags here. If a port mapping needs correction, remove and recreate this disposable test with the corrected mapping; restarting it does not rewrite the creation command. Keep real application volumes and unrelated containers outside this cleanup.

Keep Docker access deliberate

The rootful Docker daemon’s socket gives powerful control over the host. Membership of the docker group grants root-level privileges; adding an account merely to avoid typing sudo is not a hardening step. This walkthrough retains explicit sudo commands and does not broaden group access.

Rootless mode is a separate option: both the daemon and containers run without root privileges inside a user namespace. It has its own prerequisites, including subordinate user/group ID mappings, and its own user-service setup. Adding a user to the ordinary Docker group does not enable rootless mode. Choose that deployment model deliberately rather than mixing its instructions with this rootful installation.

Update and remove Docker without losing application state

For repository-managed updates, refresh the package index and repeat the official package-install step during planned maintenance. Docker also supports selecting an available Engine version explicitly. Record the installed version and test your real application after an upgrade; a fresh hello-world result checks the engine path, not every workload. Update the Compose package through the repository too.

Docker starts on boot by default on Ubuntu, but that does not establish that each application will recover correctly after a reboot. Keep application startup, data restoration and functional checks in its deployment procedure. Monitor log storage: Docker’s default json-file logs can grow until they exhaust disk space unless rotation or a suitable alternative logging configuration is provided.

If you need to uninstall Docker, first stop affected workloads deliberately and preserve their configuration and recoverable data. Docker’s documented package removal is:

Remove Docker packages during rollback
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Inspect apt’s proposal before approving it. Purging these packages does not automatically remove images, containers, volumes or custom host configuration. Keep /var/lib/docker and /var/lib/containerd intact unless you have separately decided to destroy that state. Remove only the Docker repository entry and key you introduced when retiring the repository. Uninstallation is not a rollback guarantee for an application or its data.

Our Cloud VPS provides self-managed infrastructure and root access.

You install and operate Docker, maintain the operating system and applications, restrict access, and arrange independent copies and recovery. Before moving beyond these disposable tests, give the application an equally explicit plan for exposed services, persistent data, updates and recovery.

Prepare a host for your container stack

Allow for containers, persistent volumes and build cache when comparing self-managed Cloud VPS plans.

Compare Cloud VPS plans