Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
A Dockerfile is a script that contains instructions for building a Docker image. Docker images are the basis for creating containers that can run applications in a consistent and portable manner. In this tutorial, we will cover the basics of creating a simple Dockerfile and building an image from it.
Prerequisites:
Create a new directory for your Dockerfile:
mkdir my-docker-project cd my-docker-project
Create a new file named Dockerfile
in the newly created directory:
touch Dockerfile
Open the Dockerfile
in a text editor and add the following lines:
# Use an official Python runtime as a parent image FROM python:3.8-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --trusted-host pypi.python.org -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
Here's a breakdown of the Dockerfile:
FROM
specifies the base image you want to build from (in this case, the official Python 3.8 image).WORKDIR
sets the working directory for any subsequent instructions.COPY
copies files from the source on the host machine to the destination in the container.RUN
executes a command during the build process.EXPOSE
opens a specific port for external access.ENV
sets environment variables.CMD
specifies the command to run when the container starts.Create a simple Python web app and requirements.txt file:
app.py:
from flask import Flask import os app = Flask(__name__) @app.route('/') def hello(): return f'Hello {os.environ.get("NAME")}!' if __name__ == '__main__': app.run(host='0.0.0.0', port=80)
requirements.txt:
Flask==1.1.2
Build the Docker image:
docker build -t my-docker-image .
The -t
flag assigns a tag or name to the image. The .
at the end specifies the build context, which is the current directory.
Run the Docker container:
docker run -p 4000:80 my-docker-image
This command maps port 4000 on the host machine to port 80 on the container.
Access the app in your browser by navigating to http://localhost:4000
.
Congratulations! You've successfully created a simple Dockerfile, built an image from it, and run a container using the image. This example just scratches the surface of what you can do with Dockerfiles, but it should provide a good starting point for further exploration.
Dockerfile FROM Instruction:
FROM
instruction in a Dockerfile specifies the base image for the subsequent instructions. It is the starting point for building your Docker image.FROM ubuntu:20.04 # Additional instructions go here
Dockerfile ENV vs ARG:
ENV
sets environment variables that are persistent in the image, while ARG
defines build-time variables that are only accessible during the build process.ARG version=latest ENV APP_VERSION=$version
Dockerfile COPY vs ADD:
COPY
is preferred for simple file copying, while ADD
has additional features like URL support and auto-extraction of compressed files.COPY src/ /app/src/ # or ADD http://example.com/file.tar.gz /app/
Multi-stage Builds in Dockerfile:
FROM
instructions in a single Dockerfile to create smaller, optimized images.FROM builder as build # Build stage FROM base COPY --from=build /app /app
Dockerfile CMD vs ENTRYPOINT:
CMD
provides default arguments that can be overridden, while ENTRYPOINT
is intended for setting a fixed command with optional arguments.CMD ["python", "app.py"] # or ENTRYPOINT ["java", "-jar", "app.jar"]
Configuring Ports in Dockerfile:
EXPOSE 80/tcp
Setting Up Volumes in Dockerfile:
VOLUME /data
Dockerfile for Python Application:
FROM python:3.8 WORKDIR /app COPY . /app CMD ["python", "app.py"]