A custom Docker image for Rust and Python

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.

<span>mkdir </span>rust-python <span>&&</span> <span>cd </span>rust-python
<span>mkdir </span>rust-python <span>&&</span> <span>cd </span>rust-python
mkdir rust-python && cd rust-python

Enter fullscreen mode Exit fullscreen mode

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

<span>touch </span>Dockerfile
<span>touch </span>Dockerfile
touch Dockerfile

Enter fullscreen mode Exit fullscreen mode

The Dockerfile must contain the following:

<span>FROM</span><span> rust:slim-buster</span>
<span>RUN </span>apt-get <span>-y</span> update
<span>RUN </span>apt-get <span>install</span> <span>-y</span> <span>sudo</span>
<span>RUN </span>adduser <span>--disabled-password</span> <span>--gecos</span> <span>''</span> admin
<span>RUN </span>adduser admin <span>sudo</span>
<span>RUN </span><span>echo</span> <span>'%sudo ALL=(ALL) NOPASSWD:ALL'</span> <span>>></span> /etc/sudoers
<span>RUN </span><span>chown</span> <span>-R</span> admin /home/admin
<span>USER</span><span> admin</span>
<span>RUN </span><span>sudo </span>apt-get <span>install</span> <span>-y</span> python3 make build-essential libssl-dev zlib1g-dev libbz2-dev <span>\ </span> libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev <span>\ </span> xz-utils tk-dev libffi-dev liblzma-dev python-openssl git <span>&&</span> <span>\ </span> curl https://pyenv.run | bash <span>&&</span> <span>\ </span> curl <span>-sSL</span> https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3
<span>ENV</span><span> HOME /home/admin</span>
<span>ENV</span><span> PYENV_ROOT $HOME/.pyenv</span>
<span>ENV</span><span> POETRY_ROOT $HOME/.poetry</span>
<span>ENV</span><span> CARGO_ROOT /usr/local/cargo</span>
<span>ENV</span><span> PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH</span>
<span>ENV</span><span> PATH $POETRY_ROOT/bin:$PATH</span>
<span>ENV</span><span> PATH $CARGO_ROOT/bin:$PATH</span>
<span>FROM</span><span> rust:slim-buster</span>
<span>RUN </span>apt-get <span>-y</span> update
<span>RUN </span>apt-get <span>install</span> <span>-y</span> <span>sudo</span>

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

<span>USER</span><span> admin</span>

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

<span>ENV</span><span> HOME /home/admin</span>
<span>ENV</span><span> PYENV_ROOT $HOME/.pyenv</span>
<span>ENV</span><span> POETRY_ROOT $HOME/.poetry</span>
<span>ENV</span><span> CARGO_ROOT /usr/local/cargo</span>
<span>ENV</span><span> PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH</span>
<span>ENV</span><span> PATH $POETRY_ROOT/bin:$PATH</span>
<span>ENV</span><span> PATH $CARGO_ROOT/bin:$PATH</span>
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

Enter fullscreen mode Exit fullscreen mode

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 <span>-t</span> rust-python <span>.</span>
docker build <span>-t</span> rust-python <span>.</span>
docker build -t rust-python .

Enter fullscreen mode Exit fullscreen mode

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
docker images
docker images

Enter fullscreen mode Exit fullscreen mode

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
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
rust-python            latest              7c06f720d1d3        3 hours ago         1.23GB
rust                   slim-buster         dbeae51214f7        3 weeks ago         582MB
REPOSITORY TAG IMAGE ID CREATED SIZE rust-python latest 7c06f720d1d3 3 hours ago 1.23GB rust slim-buster dbeae51214f7 3 weeks ago 582MB

Enter fullscreen mode Exit fullscreen mode

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

docker tag 7c06f720d1d3 username/rust-python:latest
docker tag 7c06f720d1d3 username/rust-python:latest
docker tag 7c06f720d1d3 username/rust-python:latest

Enter fullscreen mode Exit fullscreen mode

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
docker login
docker login

Enter fullscreen mode Exit fullscreen mode

It will ask your user and password.

And finally publish to Docker Hub:

docker push username/rust-python
docker push username/rust-python
docker push username/rust-python

Enter fullscreen mode Exit fullscreen mode

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

原文链接:A custom Docker image for Rust and Python

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
Live every day as the last day of life.
把活着的每一天看作生命的最后一天
评论 抢沙发

请登录后发表评论

    暂无评论内容