Guide to Docker

Posted on Jan 2, 2023

I. Introduction to Docker

  • What is Docker?
  • How does Docker work?
  • Why is Docker useful?

What is Docker?

Docker is an open-source containerisation platform that allows you to package an application with all of its dependencies into a single container. This container can then be easily deployed and run on any platform that supports Docker, making it a great tool for building, shipping, and running applications.

How does Docker work?

Docker works by using the host operating system’s kernel and automating the creation, deployment, and management of containers. A container is a lightweight, standalone, and executable package that includes everything an application needs to run, such as code, libraries, and runtime. Containers allow you to package and distribute an application as a single unit, making it easy to run and scale applications in different environments.

Why is Docker useful?

Docker is useful for a number of reasons:

  • Isolation of applications: Containers allow you to isolate an application from the rest of the system, ensuring that the application runs consistently across different environments.
  • Portability: Containers can be easily moved between different environments, making it easy to deploy applications on different platforms.
  • Lightweight containers: Containers are lightweight, making it easy to run multiple applications on a single host.
  • Ease of use: Docker provides a simple and easy-to-use platform for building, shipping, and running applications.

II. Advantages of using Docker

  • Isolation of applications
  • Portability
  • Lightweight containers
  • Ease of use

Docker offers a number of advantages that make it a popular choice for developers and system administrators:

Isolation of applications:

Containers allow you to isolate an application from the rest of the system, ensuring that the application runs consistently across different environments. This is useful for ensuring that an application works as intended, without being affected by other applications or the underlying system.

Portability:

Containers can be easily moved between different environments, making it easy to deploy applications on different platforms. This is useful for developers who need to test their applications on different systems, or for system administrators who need to deploy applications on multiple servers.

Lightweight containers:

Containers are lightweight, making it easy to run multiple applications on a single host. This is useful for conserving resources and maximizing the utilization of a single host.

Ease of use:

Docker provides a simple and easy-to-use platform for building, shipping, and running applications. This is useful for developers who want to focus on writing code, rather than spending time setting up and configuring environments.

III. Common use cases for Docker

  • Microservices
  • Development environments
  • Continuous integration and delivery
  • Deploying applications

Docker is a versatile tool that has a number of different use cases. Some common use cases for Docker include:

Microservices:

Docker is often used to deploy microservices-based architectures, which allow you to break down a monolithic application into smaller, independent components. This makes it easier to develop and maintain the application, as well as scale individual components as needed.

Development environments:

Docker can be used to set up consistent and reproducible development environments, making it easier for developers to work on projects together. This is useful for ensuring that everyone is working with the same version of the application and its dependencies.

Continuous integration and delivery:

Docker can be used to automate the build, test, and deployment process for applications. This is useful for ensuring that applications are tested and deployed quickly and consistently.

Deploying applications:

Docker makes it easy to deploy applications in different environments, whether it be on a local machine, a staging server, or a production server. This is useful for quickly and easily deploying applications in different environments.

IV. Docker commands

  • docker build: build an image from a Dockerfile
  • docker run: run a command in a new container
  • docker exec: run a command in an existing container
  • docker commit: create a new image from a container’s changes
  • docker save: save an image to a tar archive
  • docker network: create and manage networks

docker build:

-   Syntax: `docker build [OPTIONS] PATH | URL | -`
-   Options:
    -   -f, --file: name of the Dockerfile (default is 'PATH/Dockerfile')
    -   --build-arg: set build-time variables
-   Examples:
    -   `docker build .`: build an image from the current directory
    -   `docker build -t myimage:latest .`: build an image and tag it as 'myimage:latest'

docker run:

-   Syntax: `docker run [OPTIONS] IMAGE [COMMAND] [ARG...]`
-   Options:
    -   -d, --detach: run the container in the background
    -   -p, --publish: publish a container's port(s) to the host
    -   --name: specify a name for the container
-   Examples:
    -   `docker run ubuntu:latest`: run an Ubuntu container
    -   `docker run -d --name mycontainer -p 80:80 nginx:latest`: run an Nginx container in the background and name it 'mycontainer', publishing its port 80 to the host's port 80

docker exec:

-   Syntax: `docker exec [OPTIONS] CONTAINER COMMAND [ARG...]`
-   Options:
    -   -d, --detach: run the command in the background
    -   -i, --interactive: keep STDIN open even if not attached
-   Examples:
    -   `docker exec mycontainer ls`: run the 'ls' command in the 'mycontainer' container
    -   `docker exec -i -t mycontainer bash`: open an interactive bash shell in the 'mycontainer' container

docker commit:

-   Syntax: `docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]`
-   Options:
    -   -a, --author: author of the created image
    -   -m, --message: commit message
-   Examples:
    -   `docker commit mycontainer myimage:latest`: create an image from the 'mycontainer' container and tag it as 'myimage:latest'
    -   `docker commit -a "John Doe" -m "Added new feature" mycontainer myimage:latest`: create an image with the specified author and commit message

docker save:

-   Syntax: `docker save [OPTIONS] IMAGE [IMAGE...]`
-   Options:
    -   -o, --output: write to a file, instead of STD

docker network:

  • Syntax: docker network [OPTIONS] COMMAND
  • Commands:
    • create: create a new network
    • rm: remove one or more networks
    • ls: list networks
  • Examples:
    • docker network create mynetwork: create a new network named ‘mynetwork’
    • docker network rm mynetwork: remove the ‘mynetwork’ network
    • docker network ls: list all networks

V. Sample Dockerfile

# specify the base image
FROM python:3.7

# set the working directory
WORKDIR /app

# copy the application code
COPY . .

# install the dependencies
RUN pip install -r requirements.txt

# specify the command to run the application
CMD ["python", "app.py"]

This Dockerfile does the following:

  1. Specifies the base image to use (Python 3.7 in this case).
  2. Sets the working directory to ‘/app’.
  3. Copies the application code to the working directory.
  4. Installs the dependencies specified in the ‘requirements.txt’ file.
  5. Specifies the command to run the application (python app.py in this case).