Docker
DockerFile
Containers
DevOps
Software Deployment

How do I run a docker instance from a DockerFile?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Running a Docker instance from a Dockerfile is a fundamental skill required for utilizing Docker in many development and operational environments. This article will guide you through the process, from writing a Dockerfile to executing commands that run your Docker container.

What is a Dockerfile?

A Dockerfile is a text document containing all the commands needed to assemble an image. Docker can build images automatically by reading the instructions provided in the Dockerfile. Each command in a Dockerfile creates a layer in the resulting image, making Docker images lightweight and easy to deploy.

Writing a Dockerfile

Here's how you can start writing a Dockerfile:

  1. Specify the Base Image: Your Dockerfile typically starts with the FROM instruction, which specifies the base image for the Docker image you're creating. For example:
dockerfile
   FROM ubuntu:latest
  1. Set Environment Variables: Use the ENV instruction to set environment variables. These will be available to the processes running inside the Docker container.
dockerfile
   ENV PORT=8080
  1. Install Dependencies: You can use the RUN instruction to execute commands within the container during the image-building process. This is frequently used to install software and dependencies.
dockerfile
   RUN apt-get update && apt-get install -y python3
  1. Copy Files: The COPY or ADD instructions are used to copy files from your local filesystem into the Docker image.
dockerfile
   COPY . /app
  1. Set the Working Directory: The WORKDIR instruction sets the working directory for any commands that follow.
dockerfile
   WORKDIR /app
  1. Run a Command: Finally, specify what command should be run within the container using the CMD or ENTRYPOINT instructions.
dockerfile
   CMD ["python3", "app.py"]

Example of a Simple Dockerfile

Here is an example Dockerfile for a simple Python application:

dockerfile
1# Use the official Python base image
2FROM python:3.9-slim
3
4# Set an environment variable
5ENV PYTHONDONTWRITEBYTECODE 1
6ENV PYTHONUNBUFFERED 1
7
8# Set the working directory in the container
9WORKDIR /usr/src/app
10
11# Copy the current directory contents into the container at /usr/src/app
12COPY . /usr/src/app
13
14# Install any needed packages specified in requirements.txt
15RUN pip install --no-cache-dir -r requirements.txt
16
17# Run app.py when the container launches
18CMD ["python", "app.py"]

Building a Docker Image

Once you have your Dockerfile ready, the next step is to build the Docker image. Use the following command:

bash
docker build -t my-python-app .

In this command, -t is used to specify a name for the image (my-python-app), and the dot (.) at the end indicates that the Dockerfile is in the current directory.

Running a Docker Container

To run a Docker instance (container) from the image you built, use:

bash
docker run -d -p 4000:80 my-python-app

Here's a breakdown of the command:

  • -d: Runs the container in detached mode (in the background).
  • -p 4000:80: Maps port 4000 on the host to port 80 inside the container.
  • my-python-app: The name of the image we want to run.

Key Points Summary

StepCommand/Instruction
Specify BaseFROM ubuntu:latest
Install DepsRUN apt-get update && apt-get install -y python3
Set Env VarENV PORT=8080
Copy FilesCOPY . /app
Set WorkdirWORKDIR /app
Run CommandCMD ["python3", "app.py"]
Build Imagedocker build -t my-python-app .
Run Containerdocker run -d -p 4000:80 my-python-app

Additional Details

  • Docker Ignore: Similar to .gitignore, you can use .dockerignore to avoid including unnecessary files in your Docker image. For example:
 
  node_modules
  .git
  *.md
  • Best Practices: When writing Dockerfiles, try to minimize the number of layers by combining multiple RUN statements. This helps reduce image size.
  • Docker Hub: Once your image is built, consider pushing it to Docker Hub if you want to make it publicly accessible or share it with others.

By understanding these steps and employing best practices, you will be able to efficiently utilize Docker, automate image creation, and streamline your application deployment process.


Course illustration
Course illustration

All Rights Reserved.