A Quick Guide to Docker Basics

A Quick Guide to Docker Basics

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It does this by encapsulating applications into containers, which are lightweight and portable execution environments.

What are Docker Containers?

Docker containers are similar to virtual machines, but they are more resource-friendly. A container includes an application and all of its dependencies, but it shares the host system’s kernel with other containers. It runs as an isolated process in user space on the host operating system.

Why Use Docker?

Docker is a popular tool for running applications inside containers, which package all the dependencies and code your app needs to run into a single file. Here are some reasons why Docker is widely used:

  1. Portability: Docker containers can run on any machine that has Docker installed, regardless of the underlying operating system.

  2. Efficiency: Docker containers are lightweight and start quickly, making them ideal for deploying applications at scale.

  3. Isolation: Each Docker container runs in its own environment, so you can run different applications with different requirements on the same machine.

  4. Version Control: Docker has built-in version control, which makes it easy to track changes, roll back to previous versions, and ensure consistency across environments.

  5. Scalability: Docker makes it easy to scale your applications by simply creating more instances of your application containers.

  6. Integration with Orchestration Tools: Docker can be integrated with orchestration engines like Kubernetes and Docker Swarm, which makes managing multiple containers easier.

  7. Clean and Easy Setup: Docker allows you to keep your PC clean from the clutter of multiple software installations needed for different projects.

  8. Consistency: Docker ensures your development environment is exactly the same as your production environment. This helps to alleviate the problem of "it’s broken on my machine!".

Docker Architecture

Docker uses a client-server architecture. The Docker client communicates with the Docker daemon, which does the heavy lifting of building, running, and distributing Docker containers. Both the Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. They communicate using a REST API over UNIX sockets or a network interface.

Key Components of Docker

  1. Docker Daemon (dockerd): The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

  2. Docker Client (docker): The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The Docker client can communicate with more than one daemon.

  3. Docker Registries: A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry.

  4. Docker Objects: When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects.

    • Images: An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customisation. You might create your own images or you might only use those created by others and published in a registry.

    • Containers: A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

    • Services: Services allow you to scale containers across multiple Docker daemons, which all work together as a swarm with multiple managers and workers.

This is a high-level overview of Docker’s architecture and its components. Each of these components plays a crucial role in deploying and managing applications within Docker containers. For more detailed information, you can refer to the official Docker documentation.

Docker Images and Dockerfiles

A Docker image is a read-only template used to create Docker containers. Docker images are created from Dockerfiles, which are simple text files that specify the base image and the steps to create a new image. Basically, it provides instructions to the image builder on the commands to run, files to copy, startup command, and more. Here is a brief overview of the structure of a Dockerfile:

  1. FROM <image>: This specifies the base image that the build will extend.

  2. WORKDIR <path>: This instruction specifies the “working directory” or the path in the image where files will be copied and commands will be executed.

  3. COPY <host-path> <image-path>: This instruction tells the builder to copy files from the host and put them into the container image.

  4. RUN <command>: This instruction tells the builder to run the specified command.

  5. ENV <name> <value>: This instruction sets an environment variable that a running container will use.

  6. EXPOSE <port-number>: This instruction sets configuration on the image that indicates a port the image would like to expose.

  7. USER <user-or-uid>: This instruction sets the default user for all subsequent instructions.

  8. CMD [“<command>”, “<arg1>”]: This instruction sets the default command a container using this image will run.

Docker Commands

Docker provides a command-line interface (CLI) that allows you to interact with Docker containers. Some common Docker commands include docker run to start a new container, docker ps to list running containers, and docker rm to remove a container.

Commonly used docker commands

Here are some of the most commonly used Docker commands:

  1. docker -d: This command helps in starting the docker daemon

  2. docker build: This command helps in building the docker image.

    Build an Image from a Dockerfile

    docker build -t <image-name>

    Build an Image from a Dockerfile without the cache

    docker build -t <image-name> . –no-cache

  3. docker run: This command helps in running a container

    Create and run a container from an image, with a custom name:

    docker run --name <container-name> <image-name>

    Run a container with and publish a container’s port(s) to the host:

    docker run -p <host-port>:<container-port> <image-name>

    Run a container in the background:

    docker run -d <image-name>

  4. docker start / docker stop: These commands for starting and stopping a container.

    docker start|stop <container-name> or <container-id>

  5. docker system: This command helps inspect and manage the Docker environment.

  6. docker context: This command helps you navigate and configure different contexts.

  7. docker pause and docker unpause: These commands freeze and resume a container’s active processes, respectively.

  8. docker rm: This command is used to remove a Docker container.

    Remove a stopped container:

    docker rm <container-name>

  9. docker rmi: This command removes an image.

    docker rmi <image-name>

  10. docker volume: Manages Docker volumes.

  11. docker search: Searches Docker Hub for images.

  12. docker push: Pushes an image or a repository to a registry.

    docker push <username>/<image-name>

  13. docker pull: Pulls an image or a repository from a registry.

    docker pull <image-name>

  14. docker ps: Lists running containers.

    List all docker containers (running and stopped):

    docker ps --all

  15. docker tag: Assigns a tag to an image.

  16. docker rename: Renames a container.

  17. docker commit: Creates a new image from a container’s changes.

  18. docker network: Manages Docker networks.

  19. docker history: Shows the history of an image.

  20. docker update: Updates configuration of one or more containers.

  21. docker plugin install: Installs a plugin.

  22. docker container: Manages Docker containers.

    View resource usage stats

    docker container stats

  23. docker logs: Fetches the logs of a container.

    docker logs -f <container-name>

  24. docker swarm: Manages Docker Swarm.

  25. docker exec: Open a shell inside a running container

    docker exec -it <container-name> sh

Remember, each of these commands has additional options that you can use to perform specific tasks. You can find more details about these commands and their options in the Docker documentation or by using the --help option with each command. For example, docker pull --help will give you more information about the docker pull command.

Conclusion

Docker has revolutionised the way we develop, package, and deploy applications. By using Docker, developers can ensure that their applications will run the same way, no matter where they are deployed.

Remember, this is just a brief introduction. Docker is a powerful tool with many more features and capabilities. For more detailed information, you can refer to the official Docker documentation.