[Rust Docker] Run Rust on Docker Container with Docker and Docker Compose

Rust Docker and Docker Compose

Rust is a language empowering everyone to build reliable and efficient software.

This article is about how to run Rust on Docker Container with Docker and Docker Compose.

Prerequisites

  • Docker - https://www.docker.com/

    Developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage creates enormous complexity.

    Docker simplifies and accelerates your workflow, while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.

    To learn more about all the features of Docker, see Docker - https://www.docker.com/.

  • Docker Compose - https://docs.docker.com/compose/

    Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

    To learn more about all the features of Docker Compose, see Docker Compose - https://docs.docker.com/compose/.

Dockerfile

First, make the Dockerfile file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Dockerfile

# rust
# https://hub.docker.com/_/rust

ARG IMAGE=rust:1.52

FROM ${IMAGE}

WORKDIR /app
COPY . .

# Use exist Cargo.toml or initialize it.
RUN if [ ! -f "Cargo.toml" ]; then cargo init . ; fi
RUN cargo install --path .

CMD ["/app/target/release/app"]

Then, build image and run container.

1
2
3
4
5
6
7
8
# Build image with tag.
$ docker build . -t cloudolife/col-rust-docker

# Run container.
$ docker run -it --rm --name col-rust-docker cloudolife/col-rust-docker:latest

# Entry into container.
$ docker run -it --rm --name col-rust-docker cloudolife/col-rust-docker:latest bash

Docker Compose

Make the docker-compose.yml file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Compose file version 3 reference | Docker Documentation
# https://docs.docker.com/compose/compose-file/
version: '3'

services:
# docker-compose run app bash
# docker-compose run --service-ports app bash
app:
build:
context: .
image: cloudolife/col-rust-docker
# ports:
# - "8080:8080"
# restart: on-failure
volumes:
- ".:/app"

Run docker-compose sub commands:

1
2
3
4
5
6
7
8
# Build image.
$ docker-compose build

# Run container.
$ docker-compose up

# Entry into container.
$ docker-compose run app bash

References

[1] rust - https://hub.docker.com/_/rust

[2] Rust Programming Language - https://www.rust-lang.org/

[3] Docker - https://www.docker.com/

[4] Docker Compose - https://docs.docker.com/compose/