📜  docker for elixir - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:14:45.693000             🧑  作者: Mango

Docker for Elixir - Shell-Bash

Docker is a powerful tool for software development and deployment, and it provides a great way to package your Elixir applications. In this tutorial, we will cover the basics of Docker for Elixir applications using Shell-Bash.

Prerequisites

Before we dive into the tutorial, you should have the following prerequisites.

  1. Docker installed on your computer
  2. Basic understanding of Elixir and Docker
Creating a Dockerfile

The Dockerfile is a blueprint for building Docker images. It contains instructions on how to build and configure your Docker image.

Create a new file named Dockerfile in the root directory of your Elixir project, and add the following content:

FROM elixir:latest

RUN apt-get update

# Set up Node.js and Yarn
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g yarn

# Cache elixir deps
WORKDIR /app
COPY mix.exs mix.lock ./
COPY config config
RUN mix deps.get --only prod
RUN mix deps.compile

# Build assets
COPY assets assets
COPY priv priv
RUN yarn install --check-files
RUN cd assets && npm run deploy
RUN mix phx.digest

# Copy source code
COPY lib lib
COPY priv/gettext priv/gettext
RUN mix compile

CMD ["mix", "phx.server"]

Let's go over what each of these instructions does:

  1. FROM elixir:latest - Sets the base image for the Docker file as the latest version of Elixir
  2. RUN apt-get update - Updates the packages in the image
  3. RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - - Installs Node.js and Yarn
  4. RUN apt-get install -y nodejs - Installs Node.js
  5. RUN npm install -g yarn - Installs Yarn globally
  6. WORKDIR /app - Sets the working directory to /app
  7. COPY mix.exs mix.lock ./ - Copies the mix.exs and mix.lock file to the /app directory
  8. COPY config config - Copies the config directory to the /app directory
  9. RUN mix deps.get --only prod - Fetches the Elixir dependencies required for production
  10. RUN mix deps.compile - Compiles the Elixir dependencies
  11. COPY assets assets - Copies the assets directory to the /app directory
  12. COPY priv priv - Copies the priv directory to the /app directory
  13. RUN yarn install --check-files - Installs the Node.js dependencies using Yarn
  14. RUN cd assets && npm run deploy - Compiles the assets using Node.js
  15. RUN mix phx.digest - Compiles the Phoenix assets
  16. COPY lib lib - Copies the lib directory to the /app directory
  17. COPY priv/gettext priv/gettext - Copies the priv/gettext directory to the /app directory
  18. RUN mix compile - Compiles the Elixir code
  19. CMD ["mix", "phx.server"] - Sets the default command to run when the container starts
Building the Docker Image

With the Dockerfile in place, we can now build the Docker image for our Elixir application.

Run the following command in your terminal:

docker build -t elixir-app .

This command will build a Docker image with the tag elixir-app.

Running the Docker Container

Now that we have the Docker image, we can run it as a Docker container.

Run the following command in your terminal:

docker run -it --rm -p 4000:4000 elixir-app

This command will run a Docker container from the elixir-app image, and expose port 4000 to the host.

You can now access your Elixir application by navigating to http://localhost:4000 in your web browser.

Conclusion

In this tutorial, you have learned how to use Docker with Elixir applications using Shell-Bash. We have covered how to create a Dockerfile, build a Docker image, and run a Docker container. With Docker, you can package your Elixir applications with all of its dependencies and deploy them easily.