loading...

A custom Docker image for Rust and Python

mattdark profile image Mario García ・2 min read

From Zero to Production with Rust, Python and GitLab (4 Part Series)

1) Development Environment for Rust and Python on Linux 2) A web app built with Rust and Python 3) A custom Docker image for Rust and Python 4) From GitLab to Heroku with Docker

If you're planning to deploy your Rust and Python project to Heroku or other cloud platform, you will require a Docker image, that you can also use for your local development environment, as Heroku doesn't support Python built with shared libraries, required to integrate it with Rust.

There are official Docker images of Rust and Python available from Docker Hub, here and here. None of them comes with all the required tools, so a custom Docker image must be created.

The Debian Buster based Docker image will be used for the custom one, as it is already configured for Rust, Poetry and pyenv should be installed. At first a Dockerfile is created.

Create a directory and change to it.

mkdir rust-python && cd rust-python

Create an empty Dockerfile and then edit it with your favorite text editor.

touch Dockerfile

The Dockerfile must contain the following:

FROM rust:slim-buster
RUN apt-get -y update
RUN apt-get install -y sudo

RUN adduser --disabled-password --gecos '' admin
RUN adduser admin sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
RUN chown -R admin /home/admin

USER admin

RUN sudo apt-get install -y python3 make build-essential libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
    xz-utils tk-dev libffi-dev liblzma-dev python-openssl git && \
    curl https://pyenv.run | bash && \
    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

ENV HOME /home/admin
ENV PYENV_ROOT $HOME/.pyenv
ENV POETRY_ROOT $HOME/.poetry
ENV CARGO_ROOT /usr/local/cargo
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
ENV PATH $POETRY_ROOT/bin:$PATH
ENV PATH $CARGO_ROOT/bin:$PATH

This custom image is based on rust:slim-buster. Then update the list of packages in the repositories and install sudo.

A new user admin without password is created and added to the /etc/sudoers file and change to this new user.

The Poetry and pyenv are installed along with their dependencies, then Poetry, Pyenv and Cargo's directories are added to the PATH environment variable.

Build and Publish to Docker Hub

Now it's time to build the new image by running the following command:

docker build -t rust-python .

rust-python is the name of the new image created.

After the build operation is finished, list the new image and get the ID:

docker images

It will show something similar to the following:

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
rust-python            latest              7c06f720d1d3        3 hours ago         1.23GB
rust                   slim-buster         dbeae51214f7        3 weeks ago         582MB

Before publishing the image to Docker Hub, tag it as follows:

docker tag 7c06f720d1d3 username/rust-python:latest

Where username is your user at Docker Hub, rust-python is the name of the image as it will be listed on the account and latest is the tag assigned to this image.

Login to your Docker Hub account:

docker login

It will ask your user and password.

And finally publish to Docker Hub:

docker push username/rust-python

Your new image is ready to be used.

From Zero to Production with Rust, Python and GitLab (4 Part Series)

1) Development Environment for Rust and Python on Linux 2) A web app built with Rust and Python 3) A custom Docker image for Rust and Python 4) From GitLab to Heroku with Docker

Posted on Apr 24 by:

mattdark profile

Mario García

@mattdark

Free Software Enthusiast | Speaker | Mozillian | Mozilla Reps | Mozilla Open Leaders | Python & Rust Developer

Discussion

markdown guide