Working with Docker Containers

Introduction

Docker is a platform that enables developers to package, ship, and run applications in containers. Containers are lightweight virtualized environments that provide a consistent runtime environment for applications. Docker containers can be used to develop, deploy, test, and run applications in any environment, on any machine, with minimal overhead. This tutorial will cover the basics of working with Docker containers, including creating, building, and running Docker images.

Prerequisites

To follow along with this tutorial, you will need the following:

  • Docker installed on your local machine
  • Basic knowledge of Linux/Unix commands
  • A text editor of your choice

Creating a Docker Container

To create a new Docker container, we need to use a Dockerfile. A Dockerfile is a simple text file that contains a set of instructions that Docker uses to build a Docker image. Let’s create a Dockerfile for our application.

Create a new file named Dockerfile in your project directory and paste the following code:

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

# Set the working directory
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

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

# Copy the rest of the application code
COPY . .

# Expose port 8080 for our application
EXPOSE 8080

# Start the application
CMD [ "python", "./app.py" ]

This Dockerfile describes the steps required to build a Docker image for a Python application. Here’s a brief explanation of what’s happening in each step:

  • FROM python:3.7-slim: This instruction is used to specify the base image for our Docker container. In this case, it’s the official Python 3.7 image.
  • WORKDIR /app: This instruction sets the working directory for our application to /app.
  • COPY requirements.txt .: This instruction copies the requirements.txt file from our local machine to the /app directory in the container.
  • RUN pip install --no-cache-dir -r requirements.txt: This instruction installs the dependencies specified in the requirements.txt file.
  • COPY . .: This instruction copies the rest of the application code from our local machine to the /app directory in the container.
  • EXPOSE 8080: This instruction exposes port 8080 for our application.
  • CMD [ "python", "./app.py" ]: This instruction specifies the command to start the application.

Now that we’ve created our Dockerfile, we’re ready to build the Docker image. Run the following command in your project directory:

docker build -t myapp .

This command will build a Docker image with the tag myapp from the Dockerfile located in the current directory (.). You should see output similar to the following:

Sending build context to Docker daemon  5.632kB
Step 1/6 : FROM python:3.7-slim
...
Successfully built 1234567890ab
Successfully tagged myapp:latest

This output indicates that the Docker image was successfully built with the name myapp and the tag latest.

Running a Docker Container

Now that we’ve built our Docker image, we can use it to create a Docker container. Run the following command:

docker run -p 8080:8080 myapp

This will start a new Docker container from the myapp image we just built. The -p option is used to map port 8080 in the container to port 8080 on your local machine. You should see output similar to the following:

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO:werkzeug: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

This output indicates that the application is running and can be accessed at `http://localhost:8080` in your web browser.

Stopping a Docker Container

To stop a Docker container, run the following command:

docker stop <container_id>

You can get the container_id by running docker ps. This will list all running Docker containers along with their container IDs. Simply copy the ID of the container you want to stop and paste it into the command.

Cleaning Up

To remove a Docker container, run the following command:

docker rm <container_id>

You can get the container_id by running docker ps -a, which lists all Docker containers, including those that are stopped.

To remove a Docker image, run the following command:

docker rmi <image_name>:<tag>

You can get the <image_name> and <tag> by running docker images, which lists all Docker images on your local machine.

Conclusion

Docker containers provide a simple, consistent runtime environment for applications that can be used to develop, deploy, test, and run applications in any environment. In this tutorial, we covered the basics of working with Docker containers, including creating a Dockerfile, building a Docker image, and running a Docker container. We also covered how to stop and remove Docker containers and images. With these skills, you should be well on your way to working with Docker containers in your own projects.

Related Post