Creating Docker Images with Dockerfile
A Dockerfile is a text file containing instructions for building a Docker image.
It automates the process of creating containers, making it reproducible and easy to manage.
Docker allows you to create custom images using a special file called a Dockerfile.
In this guide, we’ll go over the basics of working with a Dockerfile and create a simple image based on Ubuntu 22.04 with nginx.
Main Dockerfile Instructions
- FROM — sets the base image (e.g.,
FROM ubuntu:22.04). - RUN — executes commands inside the container (e.g.,
RUN apt-get update). - COPY / ADD — copies files from the host machine into the container.
- WORKDIR — sets the working directory.
- EXPOSE — specifies which ports will be available.
- CMD / ENTRYPOINT — defines the command that will run when the container starts.
Example Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]
Building an Image
docker build -t my-app:latest .
Optimization Tips
- Use
.dockerignoreto exclude unnecessary files. - Combine
RUNcommands to reduce image layers. - Choose minimal base images (e.g.,
alpine).
1. Installing Docker
Before getting started, install Docker:
sudo apt update
sudo apt install -y docker.io
Verify installation:
docker --version
2. Creating a Dockerfile
Create a project directory and a Dockerfile:
mkdir mydocker
cd mydocker
nano Dockerfile
Add the following code to your Dockerfile:
# Load the base image Ubuntu 22.04
FROM ubuntu:22.04
# Update Ubuntu package repository
RUN apt-get update
# Install nginx
RUN apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/*
# Set environment variables
ENV nginx_conf /etc/nginx/nginx.conf
# Configure nginx
COPY default /etc/nginx/sites-available/default
RUN echo "\ndaemon off;" >> ${nginx_conf}
# Configure volumes
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]
# Configure services and ports
CMD ["nginx", "-g", "daemon off;"]
# Expose nginx ports
EXPOSE 80 443
3. Building the Docker Image
Now build the image with the name mynginx:
docker build -t mynginx .
Check the list of images:
docker images
4. Running the Container
Once the image is created, run the container:
docker run -d -p 8080:80 -p 8443:443 mynginx
Test it:
curl http://localhost:8080
Or open in a browser: http://localhost:8080
5. Managing the Container
Stop a container:
docker stop <container_id>
Remove a container:
docker rm <container_id>
Remove the image:
docker rmi mynginx
Conclusion
We created an image based on Ubuntu 22.04, installed nginx, and set up its configuration.
Using a Dockerfile allows you to automate the build and deployment process.
A Dockerfile simplifies application deployment and ensures a consistent environment across systems.
🚀 Explore more guides on our blog 👉 blog.1it.pro
📧 Contact us: admin@1it.pro for expert IT guidance.
🌐 Explore more: Visit 1it.pro for top-tier IT solutions.