How to Install Docker on a Azure Linux(a.k.a Mariner) VM and Run Docker Containers
Docker Journey with Microsoft CBL-Mariner
Docker is a popular tool for creating, deploying, and running applications using containers. Containers allow you to package up your application with all its dependencies and run it anywhere, from your laptop to the cloud. Docker makes it easy to manage your containers and orchestrate them with other services.
In this blog post, I will show you how to install Docker on a Mariner VM and run docker containers. Mariner is a lightweight Linux distribution built by Microsoft for use in Azure and other cloud environments. It is optimized for security, performance, and compatibility with Azure agents and services.
Before we begin, I want to remind you that this blog post is not a professional advice and you should always test your setup before using it in production. Also, make sure you have a Mariner VM ready to use. You can follow this guide to create one.
Step 1: Install the Moby Engine and Moby CLI Packages
The first step is to install the moby engine and moby cli packages on your Mariner VM. These are the core components of Docker that allow you to create and run containers. To install them, you can use the following command:
sudo tdnf install moby-engine moby-cli ca-certificates -y
This command will install the latest version of moby engine and moby cli from the official Mariner package feed. It will also install the ca-certificates package, which is required for secure communication with Docker registries.
Step 2: Enable and Start the Docker Service
The next step is to enable and start the docker service on your Mariner VM. This will allow you to use the docker command-line interface (CLI) to interact with your containers. To enable and start the docker service, you can use the following commands:
sudo systemctl enable docker.service
sudo systemctl start docker.service
These commands will enable the docker service to start automatically on boot and start it immediately. You can verify that the docker service is running by using the following command:
sudo systemctl status docker.service
You should see something like this:
● docker.service — Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2023–05–22 20:15:23 IST; 2min 15s ago
Docs: https://docs.docker.com
Main PID: 1234 (dockerd)
Tasks: 12
Memory: 50.0M
CGroup: /system.slice/docker.service
└─1234 /usr/bin/dockerd -H fd:// — containerd=/run/containerd/containerd.sockMay 22 20:15:21 mariner-vm dockerd[1234]: time=”2023–05–22T20:15:21.123456789+05:30" level=info msg=”Docker daemon” commit=abcdefg graphdriver(s)=overlay2 version=20.10.6
May 22 20:15:21 mariner-vm dockerd[1234]: time=”2023–05–22T20:15:21.123456789+05:30" level=info msg=”Daemon has completed initialization”
May 22 20:15:23 mariner-vm systemd[1]: Started Docker Application Container Engine.
May 22 20:15:23 mariner-vm dockerd[1234]: time=”2023–05–22T20:15:23.123456789+05:30" level=info msg=”API listen on /var/run/docker.sock”
Step 3: Run a Docker Container
The final step is to run a docker container on your Mariner VM. A docker container is an instance of an image that contains your application code and its dependencies. You can run a docker container with the following command:
docker run -it -d — name container_name image_name bash
This command will create and run a new container from an image in the background. The `-it` option means that it will be interactive and allocate a pseudo-TTY. The `-d` option means that it will run as a daemon. The ` — name` option allows you to specify a name for the container. The `bash` command will run a bash shell inside the container.
For example, if you want to run a container with Ubuntu as the base image and name it my_container, you can use the following command:
docker run -it -d — name my_container ubuntu bash
This command will pull the ubuntu image from Docker Hub if it is not already present on your Mariner VM and create a container named my_container from it.
If you want to enter the running container and execute commands interactively, you can use the following command:
docker exec -it container_ID_or_name /bin/bash
This command will attach to the running container and open a bash shell. The `-it` option is the same as before. The `container_ID_or_name` is either the ID or the name of the container you want to enter. The `/bin/bash` command is the same as before.
For example, if you want to enter the my_container container that we created earlier, you can use the following command:
docker exec -it my_container /bin/bash
This command will open a bash shell inside the my_container container.
You can also use other options and commands with docker run and docker exec depending on your needs. You can refer to the official documentation for more details.
Conclusion
In this blog post, I showed you how to install Docker on a Mariner VM and run docker containers. I hope you found this tutorial helpful and learned something new.
If you have any questions or feedback, please leave them in the comments below. I would love to hear from you.
Happy blogging!