How to use containers for Linux development
Want to simplify your Linux development? Find out how Linux containers can make your work easier by creating a special developer workspace with this in-depth guide.
How to Choose a Container Platform
When setting up a development environment on Linux, there are numerous containerization platforms to consider. This guide will help you get started with Docker, Podman, and LXC.
Deciding on a Container Platform
If you’re new to container technology, starting with Docker might be your best bet due to its widespread support and user-friendly nature. However, Podman and LXC also offer unique advantages. Here’s a quick rundown of each:
Docker:
As the most popular container tool, Docker is known for its ease of use and comprehensive support, making it an ideal choice for beginners.
Podman:
Podman, a container engine for managing and running OCI Containers on Linux, supports “rootless” operation, eliminating the need for root access and opening up more use cases.
LXC:
LXC stands out for its lightweight nature. It offers an experience akin to a lightweight VM, providing a more traditional Linux environment.
Setting Up Your Container Platform
After selecting your preferred container engine (Docker, Podman, or LXC), the next step is installation. Open a terminal window on your Linux system and follow the instructions for your chosen platform.
Docker Installation
Install Docker on Linux using your distribution’s package manager.
Ubuntu/Debian/etc:
sudo apt update
sudo apt upgrade
sudo apt-get install ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get install docker-ce docker-ce-cli containerd.io
Arch Linux/Manjaro/etc:
sudo pacman -Syu
sudo pacman -S docker
sudo systemctl enable --now docker
Fedora:
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io
OpenSUSE:
sudo zypper addrepo https://download.docker.com/linux/opensuse/docker-ce.repo
sudo zypper install docker-ce
sudo systemctl enable --now docker
Podman Installation
Set up Podman on your Linux system with these commands.
Ubuntu/Debian/etc:
sudo apt install podman
Arch Linux/Manjaro/etc:
sudo pacman -S podman
Fedora:
sudo dnf install -y podman
OpenSUSE:
sudo zypper ar -f obs://devel:kubic:libcontainers:stable podman
sudo zypper in podman
LXC Installation
Follow these steps to install LXC on your system.
Ubuntu/Debian/etc:
sudo apt update
sudo apt upgrade
sudo apt install lxc lxc-templates bridge-utils
sudo systemctl enable --now lxc.service
Arch Linux/Manjaro/etc:
sudo pacman -Syu
sudo pacman -S lxc
sudo systemctl enable --now lxc.service
Fedora:
sudo dnf install lxc lxc-templates lxc-extra
sudo systemctl enable --now lxc.service
OpenSUSE:
sudo zypper install lxc
sudo systemctl enable --now lxc.service
How to create your container development environment
With your container platform of choice installed, it is time to create your container development environment. In this example, we’ll focus on setting up a Rust development environment in your container system. However, this is just an example.
Docker
To create a Rust container developer environment in Docker, you need to first create a Dockerfile. You can create a Dockerfile by using the touch
command below.
touch Dockerfile
After creating your Dockerfile, open it up for editing in the Nano text editor.
nano -w Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl build-essential
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
Once you’ve created your Dockerfile, you must save it by pressing Ctrl + O on the keyboard. Then, exit Nano with Ctrl + X. From here, you can build your developer container using the docker build
command.
sudo docker build -t ubuntu-rust .
When your container is finished building, you can access it in Docker with the following command.
sudo docker run -it ubuntu-rust
Podman
To create a Rust container developer environment in Podman, start by pulling the latest Ubuntu image. You can do this with the following podman
command.
podman pull ubuntu:latest
After pulling the latest Ubuntu image, you’ll need to create a new “Podmanfile.” This file is very similar to a “Dockerfile,” as in it is an instruction manual to build your container. To create a “Podmanfile” use the following touch
command.
touch Podmanfile
Open up the “Podmanfile” in the Nano text editor.
nano -w Podmanfile
Paste the following code into the text editor to create your Podman instruction manual.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl build-essential
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
You can then build your developer container with the following command below.
podman build -t ubuntu-rust .
Once the developer container is built, access your developer container with:
podman run -it ubuntu-rust
LXC
To create your Rust LXC developer container environment, start by using the lxc-create
command.
lxc-create -n rustcontainer -t download -- -d ubuntu -r focal -a amd64
You can then start the container using the lxc-start
command.
lxc-start -n rustcontainer
You must now attach to the container using the lxc-attatch
command.
lxc-attach -n rustcontainer
Once you’ve attached to the environment, run the following commands to configure your LXC Rust container developer environment.
sudo apt update
sudo apt install -y curl build-essential
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
When you’ve installed everything, your LXC Rust developer container is ready to use.