Docker Tutorial
1. Introduction to Docker
Docker is a platform that uses OS-level virtualization to deliver software in packages called
containers. Containers are lightweight, portable, and ensure that software runs the same
regardless of the environment.
2. Installing Docker
Windows and Mac
1. Download Docker Desktop from Docker's official website.
2. Follow the installation instructions.
Linux
Update your existing list of packages:
sh
Copy code
sudo apt-get update
1.
Install a few prerequisite packages which let apt use packages over HTTPS:
sh
Copy code
sudo apt-get install apt-transport-https ca-certificates curl
software-properties-common
2.
Add the GPG key for the official Docker repository to your system:
sh
Copy code
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo
apt-key add -
3.
Add the Docker repository to APT sources:
sh
Copy code
sudo add-apt-repository "deb [arch=amd64]
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
4.
Update the package database with the Docker packages from the newly added repo:
sh
Copy code
sudo apt-get update
5.
Finally, install Docker:
sh
Copy code
sudo apt-get install docker-ce
6.
To avoid typing sudo whenever you run the docker command, add your username to the
docker group:
sh
Copy code
sudo usermod -aG docker ${USER}
7.
3. Getting Started with Docker
Hello World
Verify your installation by running:
sh
Copy code
docker run hello-world
4. Docker Basics
Docker Images and Containers
● Images: Read-only templates that form the basis of a container.
● Containers: Running instances of Docker images.
Docker Commands
List Docker CLI commands:
sh
Copy code
docker
1.
Get help on a specific command:
sh
Copy code
docker <command> --help
2.
List all images:
sh
Copy code
docker images
3.
List all running containers:
sh
Copy code
docker ps
4.
List all containers (running and stopped):
sh
Copy code
docker ps -a
5.
Start a container:
sh
Copy code
docker start <container_id>
6.
Stop a container:
sh
Copy code
docker stop <container_id>
7.
Remove a container:
sh
Copy code
docker rm <container_id>
8.
Remove an image:
sh
Copy code
docker rmi <image_id>
9.
5. Creating a Dockerfile
A Dockerfile is a text document that contains all the commands to assemble an image.
Example: Python Application
Create a directory for your project and navigate into it:
sh
Copy code
mkdir my-python-app
cd my-python-app
1.
Create a simple Python script app.py:
python
Copy code
print("Hello, Docker!")
2.
3. Create a Dockerfile:
Dockerfile
Copy code