Install docker on ubuntu20.04 virtual machine

Log in to computer

We will enable the Docker repository, import the GPG key, and install the package.

First, update the package index and install the necessary dependencies to add a new HTTPS repository:

lsb_release-a View ubuntu version

sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Use the following curl to import the GPG key of the source repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker APT software repository to your system:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Now that the Docker repository is enabled, you can install any Docker version available in the repository.

ps -p 1 to identify the init system

Install the latest version of Docker Engine-Community

sudo apt install docker-ce

Now that the installation is complete, you can enter sudo docker –version or sudo docker run hello-world to test whether the installation is successful.

Question 1:

sudo docker –version

Return: Docker version 24.0.7, build afdd53b
sudo docker run hello-world
An error occurred: docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

This error indicates that the Docker Daemon is not running on the system. The Docker daemon is a service that runs in the background and is used to manage Docker containers and images.

The solution to this problem is to ensure that the Docker daemon is running. You can check and start the Docker daemon by following these steps:

  1. Check Docker service status:

    Check if the Docker service is running using the following command:

    
    

    Copy code

    sudo service docker status

    If the Docker service is not started, you can start it using the following command:

    
    

    Copy code

    sudo service docker start

    Alternatively, if your system uses systemd, you can use the following command:

    
    

    Copy code

    sudo systemctl start docker

  2. Add user to Docker user group (optional):

    You can also add your user to the Docker user group so that you don’t need to use sudo every time to run Docker commands. Add your user to the Docker group using the following command:

    
    

    Copy code

    sudo usermod -aG docker $USER

    Note that after adding a user to a Docker group, you need to log out and back in for the changes to take effect.

  3. Restart the terminal:

    Restarting the terminal will ensure that your user has been successfully added to the Docker group or that the Docker service has been started.

After completing the above steps, you should be able to successfully run the Docker container using the sudo docker run hello-world command without encountering this error again. If the issue persists, please provide more information about your system and configuration to better help you resolve the issue.

The installation is now complete. You can enter sudo docker –version or sudo docker run hello-world to test whether the installation is successful.

If you want to enter docker without sudo, directly enter the following command and test it yourself:

The following command means to add the user to the new docker group, restart docker, and switch the current session to the new group.

sudo groupadd docker

sudo gpasswd -a ${USER} docker

sudo service docker restart

newgrp-docker
Note: The last step is necessary, otherwise because the groups command obtains the cached group information, the newly added group information will not take effect, so there will be errors when executing docker images.

How to check which initialization system is used in Ubuntu?

In Ubuntu, you can use the following command to see which init system is:

bashcopy code

ps -p 1 -o comm=

This command will display the name of the process with PID 1, which is usually the process that initializes the system. Based on the output, you can determine which initialization system your system is using. Common initialization systems include:

  1. Systemd: Most modern versions of Ubuntu use Systemd as the init system.

  2. Upstart: Upstart was used in some older Ubuntu versions, but it has been replaced by Systemd.

  3. SysV Init: SysV Init may still be used in some very old Ubuntu versions.

Please note that the initialization system may change as Ubuntu versions are updated, so make sure to check the correct version information.

How to open docker when using SysVinit as the initialization system?

When using SysVinit as the initialization system, you can start the Docker service by writing a startup script. Here are some general steps that you can adjust slightly based on your system configuration:

  1. Create a new startup script: Use a text editor to create a new startup script, usually located in the /etc/init.d/ directory. You can create this file using sudo permissions, for example:

    sudo nano /etc/init.d/docker

  2. Write startup script content: In this newly created file, you need to define how to start and stop the Docker service. Here is a sample SysVinit startup script that you can modify as needed:

    #!/bin/sh ### BEGIN INIT INFO # Provides: docker # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start Docker at boot time # Description: Enable Docker container runtime ### END INIT INFO # Path to the Docker binary DOCKER_BINARY="/usr/bin/docker" case "$1 " in start) echo "Starting Docker..." $DOCKER_BINARY daemon ;; stop) echo "Stopping Docker..." $DOCKER_BINARY stop ;; restart) echo "Restarting Docker..." $DOCKER_BINARY restart ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac exit 0

    Make sure to set the DOCKER_BINARY variable to the actual path to the Docker binary.

  3. Save and exit the editor.

  4. Set file permissions: Set the newly created script file as executable:

    sudo chmod + x /etc/init.d/docker

  5. Enable the service: Use the following command to add the Docker service to the runlevel so that it starts at system startup:

    sudo update-rc.d docker defaults

  6. Start the Docker service: Use the following command to start the Docker service:

    sudo service docker start

The Docker service should now be started in the SysVinit initialization system. You can use sudo service docker stop, sudo service docker restart and other related commands to manage Docker services. Note that SysVinit is not common on modern Linux distributions, most systems have migrated to Systemd or other init systems. If possible, it is recommended to upgrade to a more modern system for better support and security.