Docker for Beginners: A Step-by-Step Guide

Docker is a powerful tool that has revolutionized the way developers build, ship, and run applications. It provides a lightweight and portable environment for applications to run consistently across different environments. This step-by-step guide is designed for beginners who are looking to get started with Docker. By the end of this article, you will have a solid understanding of Docker and be able to create and manage your own containers.

What is Docker?

Docker is an open-source platform that uses containerization technology to package and run applications. Containers are lightweight, standalone executable packages that include everything needed to run an application, such as code, runtime, system tools, and libraries. This ensures that the application runs consistently regardless of the environment it is running in.

Why Use Docker?

  • Portability: Docker containers can run on any system that supports Docker, making it easy to move applications between different environments.
  • Consistency: Containers ensure that the application runs the same way on your local machine, in development, and in production.
  • Isolation: Each container runs in its own isolated environment, which prevents conflicts between different applications.
  • Efficiency: Containers are lightweight and start up quickly, making them ideal for development and testing.

Installing Docker

Before you can start using Docker, you need to install it on your system. Docker supports various operating systems, including Windows, macOS, and Linux. Follow the steps below to install Docker on your machine.

Installing Docker on Windows

  1. Go to the Docker Desktop for Windows page and download the installer.
  2. Run the installer and follow the on-screen instructions.
  3. Once the installation is complete, open Docker Desktop to ensure it is running correctly.

Installing Docker on macOS

  1. Go to the Docker Desktop for Mac page and download the installer.
  2. Run the installer and follow the on-screen instructions.
  3. Once the installation is complete, open Docker Desktop to ensure it is running correctly.

Installing Docker on Linux

  1. Open a terminal and update your package list:
    sudo apt-get update
  2. Install the required packages to allow apt to use a repository over HTTPS:
    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  3. Add the Docker GPG key:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Add the Docker repository to your system:
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  5. Update your package list again:
    sudo apt-get update
  6. Install Docker:
    sudo apt-get install docker-ce
  7. Verify that Docker is installed correctly by running:
    sudo docker run hello-world

Getting Started with Docker

Now that Docker is installed, let’s get started with some basic commands and concepts.

Understanding Docker Commands

Docker provides a command-line interface (CLI) for managing containers and images. Here are some of the most commonly used commands:

  • docker run: Run a container from an image.
    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • docker ps: List running containers.
    docker ps
  • docker images: List images.
    docker images
  • docker stop: Stop a running container.
    docker stop CONTAINER_ID
  • docker rm: Remove a container.
    docker rm CONTAINER_ID
  • docker rmi: Remove an image.
    docker rmi IMAGE_ID
  • docker exec: Run a command in a running container.
    docker exec -it CONTAINER_ID /bin/bash

Creating Your First Docker Container

To create your first Docker container, we will use the official Python image and run a simple Python script.

  1. Pull the Python image from Docker Hub:
    docker pull python:3.9
  2. Create a new directory for your project and navigate into it:
    mkdir my_project
    cd my_project
  3. Create a simple Python script named app.py with the following content:
    print("Hello, Docker!")
  4. Create a Dockerfile in the same directory with the following content:
    FROM python:3.9
    COPY . /app
    WORKDIR /app
    CMD ["python", "app.py"]
  5. Build the Docker image:
    docker build -t my_python_app .
  6. Run the Docker container:
    docker run my_python_app

If everything is set up correctly, you should see the output Hello, Docker! in your terminal.

Press  Kubernetes 101: Understanding the Basics

Managing Docker Containers

Once you have created a container, you can manage it using various Docker commands. Here are some common tasks you might need to perform:

Starting and Stopping Containers

To start a container, use the docker start command followed by the container ID or name:
docker start CONTAINER_ID

To stop a running container, use the docker stop command followed by the container ID or name:
docker stop CONTAINER_ID

Inspecting Containers

To get detailed information about a container, use the docker inspect command followed by the container ID or name:
docker inspect CONTAINER_ID

Accessing the Container Shell

To access the shell of a running container, use the docker exec command:
docker exec -it CONTAINER_ID /bin/bash

Working with Docker Images

Docker images are the building blocks of containers. You can create, manage, and share images using Docker commands.

Building Docker Images

To build a Docker image, you need a Dockerfile. The Dockerfile is a text file that contains instructions for building the image. Once you have a Dockerfile, you can build the image using the docker build command:

docker build -t my_image .

Publishing Docker Images

To share your Docker image with others, you can publish it to Docker Hub or a private registry. First, you need to tag the image:

docker tag my_image username/my_image:tag

Then, log in to Docker Hub:

docker login

Finally, push the image to Docker Hub:

docker push username/my_image:tag

Advanced Docker Concepts

Once you are comfortable with the basics, you can explore more advanced Docker concepts to optimize your workflow and improve your applications.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use a docker-compose.yml file to configure your application’s services. Compose then creates and starts all the services from your configuration.

To get started with Docker Compose, install it on your system and create a docker-compose.yml file. Here is an example of a simple docker-compose.yml file for a Python application:

version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"

Save the file and run the following command to start your services:

docker-compose up

Docker Volumes

Docker volumes are a way to persist data in Docker containers. By default, data in a container is ephemeral, meaning it is lost when the container is stopped. Volumes allow you to store data outside the container’s filesystem, making it persist even if the container is removed.

To use a volume, specify the -v flag when running a container:

docker run -v /host/path:/container/path my_image

You can also create and manage volumes using the docker volume command:

docker volume create my_volume
docker run -v my_volume:/container/path my_image

Conclusion

Congratulations! You have now completed this step-by-step guide to Docker for beginners. You should have a solid understanding of how to install Docker, create and manage containers, and work with Docker images. Docker is a powerful tool that can greatly improve your development workflow and help you build and deploy applications more efficiently.

As you continue to work with Docker, you will discover more advanced features and best practices. Don’t be afraid to experiment and explore the vast ecosystem of Docker tools and resources. Happy coding!

Written By

Avatar photo
John Carter

John Carter is a tech journalist with 15+ years of experience, specializing in AI, cloud computing, and data security. He’s passionate about how technology shapes society and its future.

Don't Miss