📅  最后修改于: 2023-12-03 15:14:45.693000             🧑  作者: Mango
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.
Before we dive into the tutorial, you should have the following prerequisites.
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:
FROM elixir:latest
- Sets the base image for the Docker file as the latest version of ElixirRUN apt-get update
- Updates the packages in the imageRUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
- Installs Node.js and YarnRUN apt-get install -y nodejs
- Installs Node.jsRUN npm install -g yarn
- Installs Yarn globallyWORKDIR /app
- Sets the working directory to /app
COPY mix.exs mix.lock ./
- Copies the mix.exs
and mix.lock
file to the /app
directoryCOPY config config
- Copies the config
directory to the /app
directoryRUN mix deps.get --only prod
- Fetches the Elixir dependencies required for productionRUN mix deps.compile
- Compiles the Elixir dependenciesCOPY assets assets
- Copies the assets
directory to the /app
directoryCOPY priv priv
- Copies the priv
directory to the /app
directoryRUN yarn install --check-files
- Installs the Node.js dependencies using YarnRUN cd assets && npm run deploy
- Compiles the assets using Node.jsRUN mix phx.digest
- Compiles the Phoenix assetsCOPY lib lib
- Copies the lib
directory to the /app
directoryCOPY priv/gettext priv/gettext
- Copies the priv/gettext
directory to the /app
directoryRUN mix compile
- Compiles the Elixir codeCMD ["mix", "phx.server"]
- Sets the default command to run when the container startsWith 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
.
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.
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.