Managing application dependencies with Docker can be achieved through the creation of a Docker image. Here’s a step-by-step guide.
1. Create a Dockerfile: A Dockerfile is a text document that contains all the commands a user could call on the command line to generate an image. This file is used by the Docker CLI to create the Docker image.
1. Specify Base Image: The first instruction in a Dockerfile should always be FROM, followed by the name of the image you want to base your new image off.
```
FROM python:3.6
```
This will pull the Python version 3.6 image that will provide the Python runtime for your application.
1. Specify Dependencies: After specifying your base image, you need to define all your dependencies needed for your application to run properly. This is usually done by copying the `requirements.txt` or `package.json` file (or the respective file of your programming language) into the Docker image and then running the appropriate installation commands.
Python example:
```
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
```
Node.js example:
```
COPY package.json /app/package.json
WORKDIR /app
RUN npm install
```
1. Copy Source Code: After defining and installing your dependencies, you need to copy your source code into the Docker image.
```
COPY . /app
```
1. Define the Entry Point: The ENTRYPOINT command allows you to configure a container that will run as an executable. It has two formats – exec form which is preferred (ENTRYPOINT [“executable”, “param1”, “param2”]) and shell form (ENTRYPOINT command param1 param2).
```
ENTRYPOINT [“python”, “app.py”]
```
This ensures that every time a container is created from your image, the defined command gets executed.
1. Build Docker Image: Use the command `docker build` followed by a name (tag) for your image, and the directory where your Dockerfile is located.
```
docker build -t your-image-name .
```
1. Create and Manage Containers: Now that you have created your Docker image, you can now create Docker containers from this image. Each container will have all the specified dependencies needed for your app to run properly.
Once a Docker container has been created from your Docker image, that container will contain all the dependencies it needs to run your application. This removes the problem of having mismatched, or missing dependencies when your application is running in different environments.
By using Docker images, you are ensuring that every time your application is deployed, it is done so in a consistent state, with all the dependencies it needs.