Welcome to the episode 3 of “About SWE Weekly” : A Weekly newsletter designed to stay up-to-date on the latest trend in software engineering, covering tech stacks and case studies from SWE world !
This episode will be a tech Deep dive dedicated for Beginners in Docker ! We will be covering everything you need to know to get started with a technology, and provide you a roadmap to enhance your technical skillset of that technology.
Introduction to Docker :
Docker is a software platform that enables developers to build, deploy, and run applications in a consistent manner across different environments. Docker does this by using containers, which are lightweight, isolated, and portable units of software that contain everything needed to run an application. Containers are the running instance of images.
Docker images are the building blocks of containers. An image is a snapshot of a container's filesystem, including the application code, libraries, and configuration files. Images are immutable, meaning that they cannot be changed once they are created. This makes them ideal for building and distributing applications, as you can be sure that the image you deploy will always be the same.
Getting Started :
To use Docker, you first need to install the Docker Engine on your machine. Once the Engine is installed, you can create images by using the Dockerfile syntax. Instead you can also pull an existing image from Docker Hub. Once you have created an image, you can run it by using the docker run command.
DockerFile :
A Dockerfile is a text file that contains instructions for building a Docker image. The instructions are executed in order, and each instruction creates a layer in the image. The layers are stacked on top of each other, and the final image is the union of all the layers.
Dockerfiles are written in a simple text format. The following are the most common instructions:
FROM: This instruction specifies the base image for the build. The base image can be a publicly available image from Docker Hub, or it can be a private image that you have created.
RUN: This instruction runs a command in the container. The command can be used to install software, copy files, or perform other tasks.
COPY: This instruction copies files from the host machine to the container.
ENV: This instruction sets an environment variable in the container.
EXPOSE: This instruction exposes a port in the container.
CMD: This instruction specifies the command that will be run when the container starts.
Here is an example of a Dockerfile that builds a simple web application:
FROM python:3.6
RUN pip install flask
COPY app.py /app/app.py
EXPOSE 5000
CMD ["python", "app.py"]
Once you have written a Dockerfile, you can build an image from it using the docker build
command.
docker build -t my-app .
The -t
flag specifies the name of the image to build. The .
at the end of the command tells Docker to build the image from the current directory.
Once the image is built, you can run it using the docker run
command.
docker run -p 5000:5000 my-app
The -p
flag specifies that the container should expose port 5000 on the host machine. Once the container is running, you can access the web application at http://localhost:5000
Docker Volume :
A Docker volume is a directory that is not part of the container image, but is instead mounted into the container at runtime. This allows data to be stored outside of the container image, and to persist even after the container is stopped.
There are two types of Docker volumes:
Named volumes are volumes that have a specific name. Named volumes can be shared between containers, and can be backed up and restored.
Anonymous volumes are volumes that do not have a specific name. Anonymous volumes are only accessible to the container that creates them, and are not shared with other containers.
To create a named volume, you can use the docker volume create
command. For example, to create a named volume named my-volume
, you would use the following command:
docker volume create my-volume
To mount a named volume into a container, you can use the -v
flag when you start the container. For example, to mount the named volume my-volume
into a container created using my-image
, you would use the following command:
docker run -v my-volume:/data my-image
To create an anonymous volume, you can use the -v
flag when you start the container, and specify an empty string as the volume name.
docker run -v:/data my-image
Once a volume has been created, you can access it from within the container using the /data
directory. Any changes that you make to files in the /data
directory will be persisted to the volume, even after the container is stopped.
You can also use the docker volume inspect
command to get information about a specific volume such as the volume's name, size, and location. For example, to get information about the named volume my-volume
, you would use the following command:
docker volume inspect my-volume
P.S : Refer a friend and unlock exclusive offers waiting for you !
Docker Bind Mounts:
A Docker bind mount is a directory on the host machine that is mounted into a container. This allows you to share files and directories between the host and the container. Bind mounts are often used to share source code, configuration files, or data files between the host and the container.
To use a bind mount, you need to specify the source and destination directories when you start the container. The source directory is the directory on the host machine that you want to mount, and the destination directory is the path where the directory will be mounted in the container.
For example, to mount the directory /home/user/code
on the host machine into the container at /app
, you would use the following command:
docker run -it -v /home/user/code:/app my-image
You can also use the -v
flag to specify multiple bind mounts. For example, to mount the directories /home/user/code
and /home/user/data
into the container, you would use the following command:
docker run -it -v /home/user/code:/app -v /home/user/data:/data my-image
The -it
flag in Docker is used to run a container in interactive mode. This means that you will be able to interact with the container's shell, which will allow you to run commands and explore the container's filesystem.
Here are some of the limitations of using Docker bind mounts:
Bind mounts are not persistent. If you stop and start the container, the bind mounts will be lost.
Bind mounts can be a security risk. If you mount a directory that contains sensitive data, that data will be accessible to the container.
Ending Note :
I have not covered Docker networks, and Docker compose as these can be understood much better when you learn Kubernetes, which will be my next post.
However, you can use the official documentation anytime :
https://docs.docker.com/get-started/overview/
Here's additional resource :
https://www.freecodecamp.org/news/the-docker-handbook/
Docker is a powerful tool that can help you to improve the development, deployment, and management of your applications. If you are not already using Docker, I encourage you to give it a try.