DEV Community

Orbit Websites
Orbit Websites

Posted on

Getting Started with Docker: Building Containerized Apps From Scratch

Getting Started with Docker: Building Containerized Apps From Scratch

As a developer, you're likely no stranger to the frustration of environment inconsistencies and deployment headaches. Docker has revolutionized the way we build, ship, and run applications, making it easier to ensure consistency across environments. In this article, we'll dive into the world of Docker and explore how to build containerized apps from scratch.

What is Docker and How Does it Work?

Before we start building, let's cover the basics. Docker is a containerization platform that allows you to package your application and its dependencies into a single container that can be run on any system that supports Docker. This is achieved through the use of a Dockerfile, which defines the build process for your container.

Here's an example of a simple Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY . .

# Expose the port the application will run on
EXPOSE 8000

# Run the command to start the application when the container launches
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile tells Docker to:

  • Use the official Python 3.9 image as a base
  • Set up the working directory in the container
  • Install the dependencies listed in requirements.txt
  • Copy the application code into the container
  • Expose port 8000 for the application to run on
  • Run the app.py script when the container starts

Building and Running Your Container

Now that we have a Dockerfile, let's build and run our container. To build the container, navigate to the directory containing your Dockerfile and run the following command:

docker build -t my-app .
Enter fullscreen mode Exit fullscreen mode

This will create a Docker image with the name my-app. You can then run the container using:

docker run -p 8000:8000 my-app
Enter fullscreen mode Exit fullscreen mode

This will start a new container from the my-app image and map port 8000 on the host machine to port 8000 in the container.

Managing Containers and Images

As you work with Docker, you'll likely end up with multiple containers and images. Here are some essential commands to help you manage them:

  • docker ps: List all running containers
  • docker stop <container_id>: Stop a running container
  • docker rm <container_id>: Remove a stopped container
  • docker images: List all available images
  • docker rmi <image_id>: Remove an image

Some other useful commands include:

  • docker logs <container_id>: View the logs for a container
  • docker exec -it <container_id> /bin/bash: Open a shell in a running container
  • docker inspect <container_id>: View detailed information about a container

Best Practices for Containerized Apps

When building containerized apps, keep the following best practices in mind:

  • Keep your Dockerfile organized and easy to read
  • Use official images as a base whenever possible
  • Minimize the number of layers in your Dockerfile
  • Use environment variables to configure your application
  • Use a .dockerignore file to exclude unnecessary files from the build process
  • Use a consistent naming convention for your images and containers

Some benefits of following these best practices include:

  • Improved build times
  • Reduced image size
  • Easier maintenance and debugging
  • Improved security

Conclusion

In this article, we've covered the basics of Docker and how to build containerized apps from scratch. We've explored the Dockerfile, built and run a container, and discussed essential commands for managing containers and images. By following best practices and using Docker effectively, you can simplify your development workflow, improve consistency across environments, and reduce deployment headaches. Whether you're working on a small side project or a large-scale enterprise application, Docker is a powerful tool that can help you build, ship, and run your applications with ease.


Professional

Top comments (0)