DEV Community

Cover image for Intro to Docker
brownian77
brownian77

Posted on

Intro to Docker

What is Docker?

Docker is a containerization platform that allows you to package your code and dependencies into isolated containers.

What are containers?

Containers are lightweight, standalone, executable packages of software that include everything needed to run an application: code, runtime, system tools, system libraries, and settings.

How do Docker containers work?

When you run a Docker container, Docker creates a lightweight virtual machine that runs your application. The virtual machine is isolated from the host machine (regardless of the type of operating system you use) and from other containers, so each container has its own filesystem, network, and process space.

What are the benefits of using Docker?

Docker containers offer many benefits, including:

  • Portability: Docker containers can be run on any machine with Docker installed, making them easy to deploy and share.
  • Reproducibility: Docker containers are reproducible, meaning you can create the exact same environment on any machine. This helps debug and test your code.
  • Isolation: Docker containers isolate your applications from each other and the host machine, helping to prevent conflicts and security vulnerabilities.
  • Efficiency: Docker containers are lightweight and efficient, taking up less space and resources than traditional virtual machines.
  • Scalability: Docker containers are scalable, making it easy to add or remove containers as needed.

What is Docker used for?

There are many ways to use Docker, but some of the most common tasks include:

  • Building Docker images: A Docker image is a blueprint for a Docker container. You can build a Docker image from a Dockerfile, which is a text file that describes the contents of the image.
  • Running Docker containers: Once you have built a Docker image, you can run it as a Docker container. To do this, you use the docker run command.
  • Managing Docker containers: Once you have run a Docker container, you can manage it using the docker ps, docker stop, and docker start commands.
  • Networking Docker containers: Docker containers can communicate with each other using Docker networks. You can create a Docker network using the docker network create command.
  • Storing Docker images: Docker images can be stored in Docker registries. You can push and pull Docker images from Docker registries using the docker push and docker pull commands.

Understanding important concepts about Docker

  • Docker images: A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.
  • Docker containers: A Docker container is a runnable instance of a Docker image. It includes the application code and all of its dependencies, but it runs isolated from other containers on the same host.
  • Dockerfile: A Dockerfile is a text file that contains the instructions for building a custom Docker image. It tells Docker what software to install, what configuration settings to use, and so on.
  • Docker Hub: Docker Hub is a public registry of Docker images. It allows you to find and share images with other users.
  • Docker Compose: Docker Compose is a tool that allows you to define and run multi-container Docker applications. It makes it easy to manage complex applications that require multiple containers.

Basic Docker Commands You Need to Know

Docker provides a powerful set of commands to manage containers, images, networks, and volumes. Whether you're new to Docker or looking to refresh your knowledge, mastering these basic commands is essential for working efficiently with Docker. Below are some of the most commonly used Docker commands:

1. docker pull

  • Description: Pulls an image or a repository from a registry.
  • Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]

2. docker run

  • Description: Runs a command in a new container.
  • Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

3. docker ps

  • Description: Lists containers.
  • Usage: docker ps [OPTIONS]

4. docker stop

  • Description: Stops one or more running containers.
  • Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

5. docker start

  • Description: Starts one or more stopped containers.
  • Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

6. docker exec

  • Description: Runs a command in a running container.
  • Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

7. docker rm

  • Description: Removes one or more containers.
  • Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

8. docker images

  • Description: Lists images.
  • Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

9. docker rmi

  • Description: Removes one or more images.
  • Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

10. docker logs

  • Description: Fetches the logs of a container.
  • Usage: docker logs [OPTIONS] CONTAINER

11. docker inspect

  • Description: Returns low-level information on Docker objects.
  • Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]

12. docker build

  • Description: Builds an image from a Dockerfile.
  • Usage: docker build [OPTIONS] PATH | URL | -

13. docker push

  • Description: Pushes an image or a repository to a registry.
  • Usage: docker push [OPTIONS] NAME[:TAG]

14. docker-compose

  • Description: Builds, runs, and manages multi-container applications.
  • Usage: docker-compose [OPTIONS] [COMMAND] [ARGS...]

Docker cheatsheet.

Conclusion

These basic Docker commands are fundamental for working with Docker containers and images. Whether you're building, running, or managing Dockerized applications, mastering these commands will streamline your development workflow. As you become more familiar with Docker, you'll discover additional commands and options to suit your specific needs. Happy containerizing!

Top comments (0)