Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first released in 2013 and is developed by Docker, Inc. (Wikipedia)
A Dockerfile is a script that contains instructions for building a Docker image. When the Dockerfile is processed, it creates a Docker image, which is a lightweight, standalone package that includes the application code, libraries, and dependencies. From this image, a Docker container can be launched. A container is a running instance of the image, providing an isolated environment where the application executes consistently across different systems.
Dockerfile ---> Docker Image ---> Docker Container (recipe) (package) (running instance)
Here are some reasons why to use Docker:
You can read more about Docker and why to use it from https://www.knowledgehut.com/blog/devops/why-use-docker.
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. You can read more about Dockerfile from https://docs.docker.com/engine/reference/builder/
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 container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
In order to use Docker, you will need Docker Engine or Docker Desktop. Docker Desktop is a graphical tool which makes it easy to start and stop Containers.
You can download the installation files of Docker Desktop from https://docs.docker.com/engine/install/.
In Windows you will need to install also WSL in order to run Docker Desktop. You can find the instructions for installing WSL (wsl2) from https://learn.microsoft.com/en-us/windows/wsl/install.
The purpose of the .dockerignore file is to specify which files and directories should be excluded from the Docker build context — the set of files sent to the Docker daemon when building an image. This helps control what can be used in COPY
and ADD
instructions. It serves a similar function to the .gitignore
file used in version control system Git.
Main reasons for using .dockerignore:
.dockerignore
makes it clear which files are (and are not) part of the build process, improving maintainability.You can do almost everything you need with Docker Desktop, but you can also use the command line. Here are some of the commands you might find useful:
Compose is a tool for defining and running multi-container Docker applications.
With Compose, you use a docker-compose.yml
file to configure your application's services.
Then, with a single command, you can build and start the entire application stack.
Dockerfile ---> Docker Image
|
docker-compose.yml --+
|
v
Docker Containers (one or more)
Here are the commands that you will need
In Docker (and Docker Compose), adding the -d flag to a command runs it in detached mode, meaning it runs in the background.
You can find more commands from https://docs.docker.com/engine/reference/commandline/compose_up/Docker Hub (https://hub.docker.com/) is a service provided by Docker for finding and sharing container images.
It's the world’s largest repository of container images with an array of content sources including container community developers, open source projects, and independent software vendors (ISV) building and distributing their code in containers.
https://docs.docker.com/docker-hub/
You can pull MySQL image from Docker Hub and start the container using below commands
docker pull mysql:8.1 docker run --name docker_mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:8.1Then you can connect to MySQL using below command
docker exec -it docker_mysql mysql -u root -p
You can also upload your own container images to Docker Hub and then you can deploy those images to some cloud services. Below are the instructions how to deploy from Docker Hub to Render.
Render ( https://render.com/ ) is a unified cloud to build and run all your apps and websites.
You can deploy your application to Render like this:
In this example the Docker Hub account is dh_user. The user has made an application like in example Node.js example. So, here are the steps in order to deploy it to Render.
docker build -t dh_user/imagename:latest path_to_dockerfile/
docker push dh_user/imagename:latest
(*) Docker images can have tags to identify different versions. If no tag is specified, the default tag latest is used.
For example, both docker run dh_user/imagename
and docker run dh_user/imagename:latest
will run the same image.
If you want to assign a specific version tag, such as 1.0, you can build and push the image like this:
docker build -t dh_user/imagename:1.0 path_to_dockerfile/
docker push dh_user/imagename:1.0
You can configure Render to Deploy the image automatically whenever you push a new version to Docker Hub. You can do it like this