Return to site

Docker Post Install Ubuntu

broken image


Minimal Ubuntu is the smallest Ubuntu base image for your cloud operations. These images are less than 50% the size of the standard Ubuntu server image, and boot up to 40% faster. Images of Minimal Ubuntu 16.04 LTS and 18.04 LTS are available for use now in Amazon EC2, Google Compute Engine (GCE), LXD and KVM/OpenStack. A minimal Ubuntu base image modified for Docker-friendliness. Baseimage-docker only consumes 8.3 MB RAM and is much more powerful than Busybox or Alpine. Baseimage-docker is a special Docker image that is configured for correct use within Docker containers. It is Ubuntu, plus: Modifications for Docker-friendliness. In this guide I cover installing Docker on Ubuntu 20.04 LTS (Focal Fossa), but the same steps may also work on older versions of Ubuntu, including Ubuntu 18.04 LTS. In this post you will learn how to install Docker from the regular Ubuntu repository, how to enable Docker to start automatically at system boot, and how to install Docker images.

Docker is a combo of ‘platform as a service' products and services which use OS virtualisation to provide software in packages called containers.

Containers contain everything an app, tool or service needs to run, including all libraries, dependencies, and configuration files. Containers are also isolated from each other (and the underlying host system), but can communicate through pre-defined channels.

This introduction to Docker video will give you a quick top-level overview of the tech and how it works:

Because Docker is open source software it's not only free to use, but free to adapt, extend, hack, or build on. In this guide I cover installing Docker on Ubuntu 20.04 LTS (Focal Fossa), but the same steps may also work on older versions of Ubuntu, including Ubuntu 18.04 LTS.

In this post you will learn how to install Docker from the regular Ubuntu repository, how to enable Docker to start automatically at system boot, and how to install Docker images and run them locally.

But this isn't a deep dive. This tutorial is intentionally short and to the point. This is so you spend less time reading and more time doing.

Install Docker from Ubuntu Repository

There are two hard requirements to install Docker on Ubuntu 20.04:

  • You need to have sudo access
  • You need to be connected to the internet

If you're managing or setting up an Ubuntu server then you (probably) meet both of these requirements already, but do check before you begin.

Step 1: Install Docker from the main Ubuntu repository. Do this using the apt command and the docker.io package name (note: the package name is not simply ‘docker'):

Ubuntu will download the latest version of Docker from its archives, unpack it, and install it on your system.

Step 2: Make Docker start automatically on system boot:

Step 3: Test it.

Now that Docker is installed and running you should verify that everything is working okay. This can be done using the hello-world app. From the command line run:

When you run this command you'll see a lengthy message informing you that the ‘installation appears to be working correctly'.

But look closely at the message:

You'll notice something interesting near the start: Docker was ‘unable to find' the a ‘hello-world' image. But instead of quitting it searched for and downloaded it from Docker Hub.

Which leads us neatly on to…

Step 4: Find and install Docker images.

Now you're set-up the world (or rather the Docker ecosystem) is your oyster, and Docker Hub your port of call. Docker Hub is billed as ‘the world's largest library and community for container images'. Any image available on Docker Hub can be installed on your system too.

Let's look at how to do that.

To search for an image on Docker Hub run the docker command with the search subcommand, like so:

For example, I want to search for Alpine Linux on Docker Hub so I run docker search alpine. A list of matching images (which match the term alpine) will appear. I want the official Alpine image so I look in the OFFICIAL column for the word OK

When you find the image you want to use you can download it using the pull subcommand, For example, to install Alpine Linux I run sudo docker pull alpine.

To run a downloaded image you need to add the run subcommand and the name of the image, e.g., sudo docker run alpine.

If you want to run an image as a container and get instant ‘interactive terminal' shell access add the -it flag. For example: I run sudo docker run -it alpine and it drops me straight into the Alpine container, ready to work:

To exit the ‘interactive terminal' type the word exit and hit enter.

Docker Post Install Ubuntu Usb

Check out the Docker Docs page for a wealth more info on how to use, admin, manage, and maintain your containers.

A couple of useful commands to know include docker ps -a to list all images you've used (and see their container ID/name); docker stop {container id} to close an image down; and when you're done with a container remove it using the docker rm command, again adding the the container ID/name at the end.

Going Further

In this guide we looked at installing Docker on Ubuntu 20.04 and getting official images installed. But this is only the beginning of what possible with Docker.

One possible avenue to explore is installing Docker rootless. This is an experimental feature and not (yet) easy to enable. But the effort required to set it up is worth it if you're concerned about security and stability.

If there are topics you want to see a similar to-the-point tutorial on (be it Docker related or otherwise) do drop a note down in the comments or via my usual e-mail.

Estimated reading time: 15 minutes

This section contains optional procedures for configuring Linux hosts to workbetter with Docker.

Manage Docker as a non-root user

The Docker daemon binds to a Unix socket instead of a TCP port. By defaultthat Unix socket is owned by the user root and other users can only access itusing sudo. The Docker daemon always runs as the root user.

If you don't want to preface the docker command with sudo, create a Unixgroup called docker and add users to it. When the Docker daemon starts, itcreates a Unix socket accessible by members of the docker group.

Warning

The docker group grants privileges equivalent to the rootuser. For details on how this impacts security in your system, seeDocker Daemon Attack Surface.

Note:

To run Docker without root privileges, seeRun the Docker daemon as a non-root user (Rootless mode).

To create the docker group and add your user:

  1. Create the docker group.

  2. Add your user to the docker group.

  3. Log out and log back in so that your group membership is re-evaluated.

    If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

    On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.

    On Linux, you can also run the following command to activate the changes to groups:

  4. Verify that you can run docker commands without sudo.

    This command downloads a test image and runs it in a container. When thecontainer runs, it prints an informational message and exits.

    If you initially ran Docker CLI commands using sudo before addingyour user to the docker group, you may see the following error,which indicates that your ~/.docker/ directory was created withincorrect permissions due to the sudo commands.

    To fix this problem, either remove the ~/.docker/ directory(it is recreated automatically, but any custom settingsare lost), or change its ownership and permissions using thefollowing commands:

Configure Docker to start on boot

Most current Linux distributions (RHEL, CentOS, Fedora, Debian, Ubuntu 16.04 andhigher) use systemd to manage which services start when the systemboots. On Debian and Ubuntu, the Docker service is configured to start on bootby default. To automatically start Docker and Containerd on boot for otherdistros, use the commands below:

To disable this behavior, use disable instead.

If you need to add an HTTP Proxy, set a different directory or partition for theDocker runtime files, or make other customizations, seecustomize your systemd Docker daemon options.

Use a different storage engine

For information about the different storage engines, seeStorage drivers.The default storage engine and the list of supported storage engines depend onyour host's Linux distribution and available kernel drivers.

Ubuntu

Configure default logging driver

Docker provides the capability tocollect and view log data from all containers running on a host via a series oflogging drivers. The default logging driver, json-file, writes log data toJSON-formatted files on the host filesystem. Over time, these log files expandin size, leading to potential exhaustion of disk resources.

To alleviate such issues, either configure the json-file logging driver toenable log rotation, use analternative logging driversuch as the 'local' logging driverthat performs log rotation by default, or use a logging driver that sendslogs to a remote logging aggregator.

Configure where the Docker daemon listens for connections

By default, the Docker daemon listens for connections on a UNIX socket to acceptrequests from local clients. It is possible to allow Docker to accept requestsfrom remote hosts by configuring it to listen on an IP address and port as wellas the UNIX socket. For more detailed information on this configuration optiontake a look at 'Bind Docker to another host/port or a unix socket' section ofthe Docker CLI Reference article.

Secure your connection

Before configuring Docker to accept connections from remote hosts it is critically important that youunderstand the security implications of opening docker to the network. If steps are not taken to secure the connection, it is possible for remote non-root users to gain root access on the host. For more information on how to use TLS certificates to secure this connection, check this article on how to protect the Docker daemon socket.

Configuring Docker to accept remote connections can be done with the docker.servicesystemd unit file for Linux distributions using systemd, such as recent versionsof RedHat, CentOS, Ubuntu and SLES, or with the daemon.json file which isrecommended for Linux distributions that do not use systemd.

systemd vs daemon.json

Configuring Docker to listen for connections using both the systemd unit file and the daemon.json file causes a conflict that prevents Docker from starting.

Configuring remote access with systemd unit file

  1. Use the command sudo systemctl edit docker.service to open an override file for docker.service in a text editor.

  2. Add or modify the following lines, substituting your own values.

  3. Save the file.

  4. Reload the systemctl configuration.

  5. Restart Docker.

  6. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

Configuring remote access with daemon.json

  1. Set the hosts array in the /etc/docker/daemon.json to connect to the UNIX socket and an IP address, as follows:

  2. Restart Docker.

  3. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

Enable IPv6 on the Docker daemon

To enable IPv6 on the Docker daemon, seeEnable IPv6 support.

Troubleshooting

Kernel compatibility

Docker cannot run correctly if your kernel is older than version 3.10 or if itis missing some modules. To check kernel compatibility, you can download andrun the check-config.shscript.

The script only works on Linux, not macOS.

Cannot connect to the Docker daemon

If you see an error such as the following, your Docker client may be configuredto connect to a Docker daemon on a different host, and that host may not bereachable.

To see which host your client is configured to connect to, check the value ofthe DOCKER_HOST variable in your environment.

If this command returns a value, the Docker client is set to connect to aDocker daemon running on that host. If it is unset, the Docker client is set toconnect to the Docker daemon running on the local host. If it is set in error,use the following command to unset it:

You may need to edit your environment in files such as ~/.bashrc or~/.profile to prevent the DOCKER_HOST variable from being seterroneously.

If DOCKER_HOST is set as intended, verify that the Docker daemon is runningon the remote host and that a firewall or network outage is not preventing youfrom connecting.

IP forwarding problems

If you manually configure your network using systemd-network with systemdversion 219 or higher, Docker containers may not be able to access your network.Beginning with systemd version 220, the forwarding setting for a given network(net.ipv4.conf..forwarding) defaults to off. This settingprevents IP forwarding. It also conflicts with Docker's behavior of enablingthe net.ipv4.conf.all.forwarding setting within containers.

To work around this on RHEL, CentOS, or Fedora, edit the .networkfile in /usr/lib/systemd/network/ on your Docker host(ex: /usr/lib/systemd/network/80-container-host0.network) and add thefollowing block within the [Network] section.

This configuration allows IP forwarding from the container as expected.

DNS resolver found in resolv.conf and containers can't use it

Linux systems which use a GUI often have a network manager running, which uses adnsmasq instance running on a loopback address such as 127.0.0.1 or127.0.1.1 to cache DNS requests, and adds this entry to/etc/resolv.conf. The dnsmasq service speeds upDNS look-ups and also provides DHCP services. This configuration does not workwithin a Docker container which has its own network namespace, becausethe Docker container resolves loopback addresses such as 127.0.0.1 toitself, and it is very unlikely to be running a DNS server on its ownloopback address.

If Docker detects that no DNS server referenced in /etc/resolv.conf is a fullyfunctional DNS server, the following warning occurs and Docker uses the publicDNS servers provided by Google at 8.8.8.8 and 8.8.4.4 for DNS resolution.

If you see this warning, first check to see if you use dnsmasq:

If your container needs to resolve hosts which are internal to your network, thepublic nameservers are not adequate. You have two choices:

  • You can specify a DNS server for Docker to use, or
  • You can disable dnsmasq in NetworkManager. If you do this, NetworkManageradds your true DNS nameserver to /etc/resolv.conf, but you lose thepossible benefits of dnsmasq.

You only need to use one of these methods.

Specify DNS servers for Docker

The default location of the configuration file is /etc/docker/daemon.json. Youcan change the location of the configuration file using the --config-filedaemon flag. The documentation below assumes the configuration file is locatedat /etc/docker/daemon.json.

  1. Create or edit the Docker daemon configuration file, which defaults to/etc/docker/daemon.json file, which controls the Docker daemonconfiguration.

  2. Add a dns key with one or more IP addresses as values. If the file hasexisting contents, you only need to add or edit the dns line.

    If your internal DNS server cannot resolve public IP addresses, include atleast one DNS server which can, so that you can connect to Docker Hub and sothat your containers can resolve internet domain names.

    Save and close the file.

  3. Restart the Docker daemon.

  4. Verify that Docker can resolve external IP addresses by trying to pull animage:

  5. If necessary, verify that Docker containers can resolve an internal hostnameby pinging it.

Disable dnsmasq

Ubuntu

If you prefer not to change the Docker daemon's configuration to use a specificIP address, follow these instructions to disable dnsmasq in NetworkManager.

Docker Post Install Ubuntu

Configure default logging driver

Docker provides the capability tocollect and view log data from all containers running on a host via a series oflogging drivers. The default logging driver, json-file, writes log data toJSON-formatted files on the host filesystem. Over time, these log files expandin size, leading to potential exhaustion of disk resources.

To alleviate such issues, either configure the json-file logging driver toenable log rotation, use analternative logging driversuch as the 'local' logging driverthat performs log rotation by default, or use a logging driver that sendslogs to a remote logging aggregator.

Configure where the Docker daemon listens for connections

By default, the Docker daemon listens for connections on a UNIX socket to acceptrequests from local clients. It is possible to allow Docker to accept requestsfrom remote hosts by configuring it to listen on an IP address and port as wellas the UNIX socket. For more detailed information on this configuration optiontake a look at 'Bind Docker to another host/port or a unix socket' section ofthe Docker CLI Reference article.

Secure your connection

Before configuring Docker to accept connections from remote hosts it is critically important that youunderstand the security implications of opening docker to the network. If steps are not taken to secure the connection, it is possible for remote non-root users to gain root access on the host. For more information on how to use TLS certificates to secure this connection, check this article on how to protect the Docker daemon socket.

Configuring Docker to accept remote connections can be done with the docker.servicesystemd unit file for Linux distributions using systemd, such as recent versionsof RedHat, CentOS, Ubuntu and SLES, or with the daemon.json file which isrecommended for Linux distributions that do not use systemd.

systemd vs daemon.json

Configuring Docker to listen for connections using both the systemd unit file and the daemon.json file causes a conflict that prevents Docker from starting.

Configuring remote access with systemd unit file

  1. Use the command sudo systemctl edit docker.service to open an override file for docker.service in a text editor.

  2. Add or modify the following lines, substituting your own values.

  3. Save the file.

  4. Reload the systemctl configuration.

  5. Restart Docker.

  6. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

Configuring remote access with daemon.json

  1. Set the hosts array in the /etc/docker/daemon.json to connect to the UNIX socket and an IP address, as follows:

  2. Restart Docker.

  3. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

Enable IPv6 on the Docker daemon

To enable IPv6 on the Docker daemon, seeEnable IPv6 support.

Troubleshooting

Kernel compatibility

Docker cannot run correctly if your kernel is older than version 3.10 or if itis missing some modules. To check kernel compatibility, you can download andrun the check-config.shscript.

The script only works on Linux, not macOS.

Cannot connect to the Docker daemon

If you see an error such as the following, your Docker client may be configuredto connect to a Docker daemon on a different host, and that host may not bereachable.

To see which host your client is configured to connect to, check the value ofthe DOCKER_HOST variable in your environment.

If this command returns a value, the Docker client is set to connect to aDocker daemon running on that host. If it is unset, the Docker client is set toconnect to the Docker daemon running on the local host. If it is set in error,use the following command to unset it:

You may need to edit your environment in files such as ~/.bashrc or~/.profile to prevent the DOCKER_HOST variable from being seterroneously.

If DOCKER_HOST is set as intended, verify that the Docker daemon is runningon the remote host and that a firewall or network outage is not preventing youfrom connecting.

IP forwarding problems

If you manually configure your network using systemd-network with systemdversion 219 or higher, Docker containers may not be able to access your network.Beginning with systemd version 220, the forwarding setting for a given network(net.ipv4.conf..forwarding) defaults to off. This settingprevents IP forwarding. It also conflicts with Docker's behavior of enablingthe net.ipv4.conf.all.forwarding setting within containers.

To work around this on RHEL, CentOS, or Fedora, edit the .networkfile in /usr/lib/systemd/network/ on your Docker host(ex: /usr/lib/systemd/network/80-container-host0.network) and add thefollowing block within the [Network] section.

This configuration allows IP forwarding from the container as expected.

DNS resolver found in resolv.conf and containers can't use it

Linux systems which use a GUI often have a network manager running, which uses adnsmasq instance running on a loopback address such as 127.0.0.1 or127.0.1.1 to cache DNS requests, and adds this entry to/etc/resolv.conf. The dnsmasq service speeds upDNS look-ups and also provides DHCP services. This configuration does not workwithin a Docker container which has its own network namespace, becausethe Docker container resolves loopback addresses such as 127.0.0.1 toitself, and it is very unlikely to be running a DNS server on its ownloopback address.

If Docker detects that no DNS server referenced in /etc/resolv.conf is a fullyfunctional DNS server, the following warning occurs and Docker uses the publicDNS servers provided by Google at 8.8.8.8 and 8.8.4.4 for DNS resolution.

If you see this warning, first check to see if you use dnsmasq:

If your container needs to resolve hosts which are internal to your network, thepublic nameservers are not adequate. You have two choices:

  • You can specify a DNS server for Docker to use, or
  • You can disable dnsmasq in NetworkManager. If you do this, NetworkManageradds your true DNS nameserver to /etc/resolv.conf, but you lose thepossible benefits of dnsmasq.

You only need to use one of these methods.

Specify DNS servers for Docker

The default location of the configuration file is /etc/docker/daemon.json. Youcan change the location of the configuration file using the --config-filedaemon flag. The documentation below assumes the configuration file is locatedat /etc/docker/daemon.json.

  1. Create or edit the Docker daemon configuration file, which defaults to/etc/docker/daemon.json file, which controls the Docker daemonconfiguration.

  2. Add a dns key with one or more IP addresses as values. If the file hasexisting contents, you only need to add or edit the dns line.

    If your internal DNS server cannot resolve public IP addresses, include atleast one DNS server which can, so that you can connect to Docker Hub and sothat your containers can resolve internet domain names.

    Save and close the file.

  3. Restart the Docker daemon.

  4. Verify that Docker can resolve external IP addresses by trying to pull animage:

  5. If necessary, verify that Docker containers can resolve an internal hostnameby pinging it.

Disable dnsmasq

Ubuntu

If you prefer not to change the Docker daemon's configuration to use a specificIP address, follow these instructions to disable dnsmasq in NetworkManager.

  1. Edit the /etc/NetworkManager/NetworkManager.conf file.

  2. Comment out the dns=dnsmasq line by adding a # character to the beginningof the line.

    Save and close the file.

  3. Restart both NetworkManager and Docker. As an alternative, you can rebootyour system.

RHEL, CentOS, or Fedora

To disable dnsmasq on RHEL, CentOS, or Fedora:

  1. Disable the dnsmasq service:

  2. Configure the DNS servers manually using theRed Hat documentation.

Allow access to the remote API through a firewall

If you run a firewall on the same host as you run Docker and you want to accessthe Docker Remote API from another host and remote access is enabled, you needto configure your firewall to allow incoming connections on the Docker port,which defaults to 2376 if TLS encrypted transport is enabled or 2375otherwise.

Two common firewall daemons areUFW (Uncomplicated Firewall) (oftenused for Ubuntu systems) and firewalld (often usedfor RPM-based systems). Consult the documentation for your OS and firewall, butthe following information might help you get started. These options are fairlypermissive and you may want to use a different configuration that locks yoursystem down more.

  • UFW: Set DEFAULT_FORWARD_POLICY='ACCEPT' in your configuration.

  • firewalld: Add rules similar to the following to your policy (one forincoming requests and one for outgoing requests). Be sure the interface namesand chain names are correct.

Your kernel does not support cgroup swap limit capabilities

On Ubuntu or Debian hosts, You may see messages similar to the following whenworking with an image.

This warning does not occur on RPM-based systems, which enable thesecapabilities by default.

Docker Post Install Ubuntu 18

If you don't need these capabilities, you can ignore the warning. You can enablethese capabilities on Ubuntu or Debian by following these instructions. Memoryand swap accounting incur an overhead of about 1% of the total available memoryand a 10% overall performance degradation, even if Docker is not running.

  1. Log into the Ubuntu or Debian host as a user with sudo privileges.

  2. Edit the /etc/default/grub file. Add or edit the GRUB_CMDLINE_LINUX lineto add the following two key-value pairs:

    Save and close the file.

  3. Update GRUB.

    If your GRUB configuration file has incorrect syntax, an error occurs.In this case, repeat steps 2 and 3.

    The changes take effect when the system is rebooted.

Next steps

Docker Post Install Ubuntu 18.04

  • Take a look at the Get started training modules to learn how to build an image and run it as a containerized application.
  • Review the topics in Develop with Docker to learn how to build new applications using Docker.

Docker Post Install Ubuntu

Docker, Docker documentation, requirements, apt, installation, ubuntu, install, uninstall, upgrade, update



broken image