Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Docker Commands & Usage

1. Introduction

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers are isolated environments that share the host OS kernel, allowing for efficient resource usage.

2. Installation

Docker can be installed on various operating systems. Below are the steps for installing Docker on Ubuntu:


sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
        
**Note**: After installation, you can verify Docker is running by executing sudo systemctl status docker.

3. Basic Docker Commands

Here are some fundamental Docker commands that every user should know:

  • docker --version - Check the installed Docker version.
  • docker pull <image-name> - Download an image from Docker Hub.
  • docker images - List all downloaded images.
  • docker rmi <image-id> - Remove an image.
  • docker run <options> <image-name> - Create and start a container from an image.

4. Advanced Docker Commands

For more complex operations, consider using these commands:

  • docker ps - List all running containers.
  • docker exec -it <container-id> /bin/bash - Access a running container's shell.
  • docker-compose up - Start services defined in a docker-compose.yml file.

5. Best Practices

Follow these best practices for efficient Docker usage:

  1. Use official images from Docker Hub.
  2. Keep images small by minimizing layers.
  3. Regularly update your images to incorporate security patches.
  4. Use .dockerignore to exclude unnecessary files from the build context.

6. FAQ

What is a Docker image?

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including code, runtime, libraries, and environment variables.

How do I remove a stopped container?

You can remove a stopped container using the command docker rm <container-id>.

Can Docker run on Windows?

Yes, Docker can run on Windows, but it requires Windows 10 Pro or Enterprise, or Windows Server 2016 or later.