Create a Docker File and using Docker file Build Images

Create a Dockerfile and Build Images

A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image. This Dockerfile example sets up a lightweight Python application container. It performs several key actions:

  • Uses the python:3.9-slim image as a base.

  • Sets the working directory inside the container to /app.

  • Copies the current directory's contents into the container at /app.

  • Makes port 80 available to the container's external network.

  • Specifies that the container runs app.py with Python when it launches.

Performing Tasks with Docker in a New Folder

To streamline your Docker workflows, follow these steps to create a folder where you can perform all related tasks:

  1. Create a New Folder: Open your terminal and use the command mkdir your-folder-name to create a new folder where you will carry out your Docker tasks.

  2. Navigate to Your New Folder: Change your current directory to the newly created folder by entering cd your-folder-name.

  3. Initialize Your Docker Project: Inside the folder, you can now perform all Docker-related tasks, such as initializing Docker projects, creating Docker files, and managing Docker containers and images.

Create a separate folder

By organizing your Docker tasks within a specific folder, you maintain a clean and efficient workflow.

Use the mkdir command to create a directory.

mkdir docker_config

Navigate to the created directory.

cd docker_config 

Use the touch command to create a file.

touch Dockerfile

Create a docker file

Now use the vi editor to edit the file.

vi Dockerfile
# Use a base image from Docker Hub
FROM alpine:latest

# Set the working directory in the container
WORKDIR /app

# Copy files from the host machine to the container
COPY . /app

# Install necessary dependencies
RUN apk add --no-cache python3

# Expose a port
EXPOSE 80

# Define a default command to run when the container starts
CMD ["python3", "app.py"]

Grant user access

chown $USER /var/run/docker.sock

Build an image using the docker file

This Dockerfile provides a clear, reproducible method for building a containerized Python application.

docker build -t my-image .

This command will build your Docker container based on the instructions in the Docker file you created. Make sure you run this command within the docker_config directory where your Dockerfile is located. The -t flag tags your image my-image for easy reference.

To verify that your Docker image has been successfully built, use the docker images command, which will list all Docker images on your system, including your newly created my_image image. This allows you to confirm that your image is ready for use.

docker images

Manage Images Commands

To remove the images

docker rmi image_name/imgae_id

Lists images

docker image ls

Tags an image

docker tag image_name/image_id  tag_name

More commands->

Common Dockerfile Instructions

FROM:- Specifies the base image for the Docker image

RUN:- Executes a command during the build process

COPY:- Copies files from the build context to the Docker image.

ENV:- Sets an environment variable in the Docker image

EXPOSE:- Exposes a port in the Docker image.

CMD:- Specifies the default command to run when the Docker container is started.

WORKDIR:- Sets the working directory in the docker image.

VOLUME:- Creates a volume in the Docker image.

USER:- Sets the user and group ID for the Docker image.

ENTRYPOINT:- Specifies the entry point for the Docker container.

Last updated