blue and red cargo ship on sea during daytime

Using Docker for Beginners: An Easy Tutorial

blue and red cargo ship on sea during daytime

Docker has revolutionized software development by making it easier to create, deploy, and run applications using containers. Containers allow developers to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. This guide provides an easy tutorial for beginners to start using Docker, explaining its fundamental concepts and walking through the steps to set up and run your first container.

Understanding Docker

Docker is a platform for developing, shipping, and running applications inside containers. This tool allows developers to isolate their applications from their infrastructure so they can deliver software quickly. Docker automates the repetitive tasks of setting up and configuring development environments so that developers can focus on what matters: building great software.

Key Concepts of Docker

  1. Images: An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
  2. Containers: A container is a runtime instance of an image—what the image becomes in memory when executed (i.e., an image with state, or a user process). You can have many running containers of the same image.
  3. Dockerfile: A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Setting Up Docker

  1. Installation:
    • Visit Docker’s official website and download Docker Desktop for your operating system (Windows, macOS, or Linux).
    • Follow the installation instructions provided on the site.
  2. Verify Installation:
    • Open a terminal or command prompt.
    • Type docker --version to ensure Docker was installed without errors.

Running Your First Container

  1. Pull an Image:
    • Open your command line or terminal.
    • Pull a sample image from Docker Hub by running: docker pull hello-world
  2. Run a Container:
    • To run the image inside a container, type: docker run hello-world
    • This command tells Docker to run the “hello-world” image. If Docker can’t find the image locally, it pulls it from Docker Hub.
  3. List Docker Containers:
    • After running the container, you can see the active containers by typing: docker ps
    • To see all containers (active and inactive), type: docker ps -a

Building Your Own Docker Image

  1. Create a Dockerfile:
    • Create a directory on your computer.
    • Inside this directory, create a file named Dockerfile.
    • Open the Dockerfile in a text editor, and write the following:
# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
  • This Dockerfile sets up a simple Python application.

Build the Image:

  • In your terminal, navigate to the directory containing your Dockerfile.
  • Run the command: docker build -t python-app .
  • This command builds the image with the tag “python-app”.

Run Your New Image:

  • Run your new image by typing: docker run -p 4000:80 python-app
  • This tells Docker to run your “python-app” image and map port 4000 on your local machine to port 80 on the container.

    Conclusion

    Docker simplifies the complexities of application development by encapsulating the environment in which an application runs. This not only makes it easier to develop and test applications in consistent environments but also facilitates collaboration by eliminating the “it works on my machine” problem.

    Similar Posts