Docker containers start: A beginner’s guide to docker basics

Leandro Padula

Share this article

If you’re new to the world of Docker, you may be wondering what Docker containers are and how to get started with them. Docker is a powerful tool that allows you to create and run applications in a portable and efficient way. In this article, we’ll cover the basics of Docker containers and show you how to get started with them.

What are Docker containers?

Docker containers are lightweight, portable, and self-contained environments that allow you to package and run applications with all their dependencies. Containers are similar to virtual machines (VMs), but they are much more efficient and easier to use. Containers don’t require a separate operating system, so they can run faster and with less overhead than VMs.

Docker containers are based on images, which are pre-built templates that contain everything needed to run an application. Images include the application code, libraries, and any other dependencies needed to run the application. You can think of images as the building blocks for Docker containers.

A few more concetps

Docker Registry: A Docker registry is a central repository where Docker images are stored and distributed. The most commonly used registry is Docker Hub, which is a public registry that hosts millions of pre-built images. You can also set up your own private registry to store and share images within your organization.

Dockerfile: A Dockerfile is a text file that contains a set of instructions for building a Docker image. Each instruction in the Dockerfile creates a new layer in the image. By using a Dockerfile, you can automate the process of building and configuring your images, making it easy to reproduce and share them.

Volume: A Docker volume is a way to store and manage data in a container. Volumes are created and managed outside of the container, and can be shared between containers. Volumes are often used to persist data between container restarts, and to share data between containers.

Networking: Docker provides a powerful networking system that allows containers to communicate with each other, as well as with the host system and other external networks. By default, each container is connected to a virtual network, which provides isolation and security.

Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you can define the services, networks, and volumes for your application in a single YAML file, and then use a single command to start and stop the entire application. This makes it easy to manage complex applications that consist of multiple containers.

How to start Docker containers?

To start a Docker container, you need to follow these basic steps:

Install docker

The first step is to install Docker on your system. Docker is available for Windows, macOS, and Linux. You can download the appropriate version for your system from the Docker website.

To install Docker on Ubuntu, you can follow these steps:

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Use the following command to set up the stable Docker repository:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update the apt package index and install the latest version of Docker CE and containerd:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Install docker-compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. You can install Docker Compose on Ubuntu by following these steps:

Download the current stable release of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

Verify that the installation was successful by checking the version:

docker-compose --version

Define a service in the Docker Compose file

To begin with, let’s create a docker-compose.yml file. For instance, we can define a service for the Nginx container within it using the official Nginx image from Docker Hub. We need to specify the mount point for the web server files, which will be located in the /var/www/html directory.

version: '3'

services:

  web:
    image: nginx
    volumes:
      - ./webserver-files:/var/www/html
    ports:
      - "80:80"

Build or pull the image

Since we are using the official Nginx image from Docker Hub, we do not need to build the image. We can simply pull it by running the following command:

docker-compose pull

Start the container

Start the Nginx container by running the following command:

docker-compose up -d

Once the container is running, you can connect to the webserver by opening a web browser and navigating to http://localhost. You should see the default Nginx web page. Any files placed in the ./webserver-files directory on your local machine will be served by the webserver, since it is mounted as the webserver’s root directory.

Conclusion

Docker containers are a powerful tool for building, deploying, and running applications in a portable and efficient way. With Docker, you can package your applications and their dependencies into lightweight and portable containers that can run anywhere. By following the basic steps outlined in this article, you can start using Docker containers today and take advantage of their many benefits.

Leandro Padula

Software development expert with a proven track record of delivering successful solutions for companies of all sizes.

Share this article

Leave a Comment

Related articles

Ready to get your project started?

Book a call