Docker - Basics
Docker is a platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Here's a breakdown of Docker basics:
1. What is Docker?
Docker is an open-source platform that uses OS-level virtualization to deliver software in packages called containers. Containers are lightweight, stand-alone, executable packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
2. Key Concepts
- Images: Read-only templates that define the contents and setup of a container. An image is built from a Dockerfile and provides all the necessary instructions to create a container.
- Containers: Instances of Docker images that run applications. A container is a runnable instance of an image.
- Dockerfile: A text file containing instructions on how to build a Docker image. It includes commands for setting up the environment, installing software, copying files, and defining environment variables.
- Docker Hub: A cloud-based registry service that allows you to find and share container images with your team. It is the default registry where Docker looks for images.
3. Basic Commands
- docker run: Runs a container from an image.
>docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- docker pull: Pulls an image from a registry.
>docker pull IMAGE_NAME
- docker build: Builds an image from a Dockerfile.
>docker build -t IMAGE_NAME .
- docker ps: Lists running containers.
>docker ps
- docker stop: Stops a running container.
>docker stop CONTAINER_ID
- docker rm: Removes a container.
>docker rm CONTAINER_ID
- docker rmi: Removes an image.
>docker rmi IMAGE_ID
>docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
>docker pull IMAGE_NAME
>docker build -t IMAGE_NAME .
>docker ps
>docker stop CONTAINER_ID
>docker rm CONTAINER_ID
>docker rmi IMAGE_ID
4. Creating a Dockerfile
A Dockerfile is a script that contains a series of instructions on how to build a Docker image. Here is a simple example:
Dockerfile# 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"]
5. Building and Running a Docker Container
To build an image from the Dockerfile, navigate to the directory containing the Dockerfile and run:
>docker build -t my-python-app .
To run the newly created image as a container:
>docker run -p 4000:80 my-python-app
This command maps port 80 inside the container to port 4000 on your host machine.
6. Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
Example docker-compose.yml
:
yaml
version: '3'
services:
web:
image: my-python-app
build: .
ports:
- "4000:80"
redis:
image: "redis:alpine"
To start the application:
>docker-compose up
This command starts the services defined in the docker-compose.yml
file.
Summary
Docker simplifies the process of managing dependencies and deploying applications by encapsulating everything in containers. This makes it easier to ensure consistent environments from development to production, enhancing the reproducibility and reliability of software deployments.