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

[What is Docker](https://docs.docker.com/get-started/overview/)

# 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1655146164097/OHARoAcLz.PNG align="left")

Welldone, Docker Engine is installed and running.


# Running as a rootless user
### [Requirements](https://docs.docker.com/engine/security/rootless/#prerequisites)
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
```

3. 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1655144451618/oSEBiJzwI.PNG align="left")

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
```
4. 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](https://docs.docker.com/engine/security/rootless/#usage)

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 
```
- 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.
- The ```-p``` (alternative```--publish```) flag exposes the port outside of Docker, to make the port available outside of Docker. See [Networking in containers](https://docs.docker.com/config/containers/container-networking/)
- 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 http://localhost:8080.

3. 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1655148181555/PyoBXyaCL.PNG align="left")
- In a Browser.  Open a new tab and navigate to:
```
http://localhost:8080
```

![nginxrunningbrowser.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1655148334303/xga7lcypJ.PNG align="left")

4. Shutting down a container. We aliased our container with the name ```nginx_server```. Shutdown the container using: 
```
docker stop nginx_server
```
5. Removing the container: 
```
docker rm -f nginx_server
```
6. Shutdown all docker containers: 
```
systemctl stop docker 
```

Useful reference
- Learn more at [Docker docs](https://docs.docker.com/)
- [Using port mappings](https://www.baeldung.com/linux/assign-port-docker-container#why-we-use-port-mapping)

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