Setting up a Docker Engine and running as a rootless user (Ubuntu)

Setting up a Docker Engine and running as a rootless user (Ubuntu)

Rootless user (Ubuntu)

What is Docker

Installing Docker Engine.

You first need to set up a repository for first-time usage. You can subsequently install and update Docker from the repository

Setting up a repository

  1. Update apt package index.
    sudo apt-get update
    
  2. Install packages to allow apt to use a repository over HTTPS.
    sudo apt-get install \
     ca-certificates \
     curl \
     gnupg \
     lsb-release
    
  3. Add Docker’s official GPG key:
    sudo mkdir -p /etc/apt/keyrings
    
    then:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    
  4. Setup the repository
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    

Install Docker Engine

  1. Update apt package index:
    sudo apt-get update
    
  2. Install the latest version of Docker:
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
    
  3. Make sure the engine is running: sudo docker run hello-world.

    Note: This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits as shown below.

runhelloworldimage.PNG

Welldone, Docker Engine is installed and running.

Running as a rootless user

Requirements

Rootless mode allows running the Docker daemon and containers as a non-root user to mitigate potential vulnerabilities in the daemon and the container runtime.

  1. Open a terminal ( CTRL + ALT + T )

  2. Install dbus-user-session

    (Simple Interprocess Messaging system) used for sending messages between applications.

sudo apt-get install -y dbus-user-session
  1. Run:
    dockerd-rootless-setuptool.sh install
    
    This sets up the daemon. Make sure the system-wide docker daemon is not running. If so, kill the process using:
    sudo systemctl disable --now docker.service docker.socket
    
    Your output should be similar to the one in the image below.

Capture.PNG

If you get an error, if dockerd-rootless-setuptool.sh is not present, you may need to install the docker-ce-rootless-extras package manually

sudo apt-get install -y docker-ce-rootless-extras
  1. Set up the environmental variables
    export PATH=/usr/bin:$PATH
    export DOCKER_HOST=unix:///run/user/1000/docker.sock
    

Setting up the client

Note. Use systemctl --user to manage the lifecycle of the daemon:

systemctl --user start docker

Alternatives

  1. You must specify either the socket path or the CLI context explicitly. a. To specify the socket path using $DOCKER_HOST:
    export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
    
    b. To specify the CLI context using docker context:
    docker context use rootless
    
  2. Run a Nginx open-source in a Docker container:
    docker run -d -p 8080:80 --name nginx_server nginx
    
  3. The -d option specifies that the container runs in detached mode: the container continues to run until stopped but does not respond to commands run on the command line.
  4. The -p (alternative--publish) flag exposes the port outside of Docker, to make the port available outside of Docker. See Networking in containers
  5. You can assign memorable names to your docker containers when you run them, using the --name flag. Here we choose nginx_server The first port number is associated with the host running the container. In the nginx example, 8080 is exposed on the host; in our case, that would be localhost:8080.

  6. Now our Nginx is up and running. Verify Either on in your browser or in the terminal using the command below:

    • Terminal:
      curl http://localhost:8080
      

nginxrunning.PNG

  • In a Browser. Open a new tab and navigate to:
    http://localhost:8080
    

nginxrunningbrowser.PNG

  1. Shutting down a container. We aliased our container with the name nginx_server. Shutdown the container using:
    docker stop nginx_server
    
  2. Removing the container:
    docker rm -f nginx_server
    
  3. Shutdown all docker containers:
    systemctl stop docker
    

Useful reference

Thanks for reading. Let us know your suggestions in the comments below