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:
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.Navigate to Your New Folder: Change your current directory to the newly created folder by entering
cd your-folder-name
.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.
Navigate to the created directory.
Use the touch command to create a file.
Create a docker file
Now use the vi editor to edit the file.
Grant user access
Build an image using the docker file
This Dockerfile provides a clear, reproducible method for building a containerized Python application.
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.
Manage Images Commands
To remove the images
Lists images
Tags an image
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