Docker
About Docker

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:

  • Consistency: Docker ensures that applications run the same way in different environments, from development to production.
  • Portability: Containers can run on any system that supports Docker, regardless of the underlying operating system.
  • Isolation: Each container runs independently, avoiding conflicts between applications and dependencies.
  • Scalability: Containers can be easily scaled up or down to handle changing workloads.
  • Efficiency: Docker uses fewer resources compared to traditional virtual machines, enabling faster startup times and better performance.

You can read more about Docker and why to use it from https://www.knowledgehut.com/blog/devops/why-use-docker.

Dockerfile

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 Images and Containers

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.

Docker Dektop

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.

.dockerignore

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:

  1. Efficiency: A smaller build context speeds up the image build process by reducing the amount of data sent to the Docker daemon.
  2. Security: It helps prevent accidental inclusion of sensitive files such as passwords, keys, or credentials.
  3. Smaller Images: By excluding unnecessary files from the context, you avoid copying them into the image, keeping the final image size smaller.
  4. Clarity: A well-maintained .dockerignore makes it clear which files are (and are not) part of the build process, improving maintainability.

Docker commands

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:

  • docker ps : list of running containers
  • docker ps -a : list of all containers (including stopped ones)
  • docker images : list of downloaded images
  • docker run -d -p 8080:80 imagename : run a new container from an image
  • docker stop container_name : stop a running container
  • docker start container_name : start a stopped container
  • docker rm container_name : remove a container
  • docker rmi image_name : remove an image
  • docker inspect container_name : detailed information about a container
  • docker exec -it container_name bash : open a terminal inside a running container
You will find more commands from https://docs.docker.com/engine/reference/commandline/docker/

Docker Compose

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)
  • Dockerfile defines how a single image is built.
  • Docker Image is the packaged application, ready to be run in a container.
  • docker-compose.yml specifies how multiple images and containers work together, including services, networks, and volumes.
  • Docker Containers are the running instances, often several working together as part of one application stack (e.g., an app with a database).
You can read more about it from https://docs.docker.com/compose/

Here are the commands that you will need

  • docker-compose up : Build images if needed and start containers
  • docker-compose up --build : Rebuild images and start containers
  • docker-compose down : Stop containers and remove them.
  • docker-compose down -v : Stop containers and remove them along with associated volumes (example databases are removed).
  • docker-compose down --rmi all : Stop containers and remove them along with all images.
  • docker-compose build : Build images without starting containers.
  • docker-compose restart : Restart containers.
  • docker-compose stop : Stop containers without removing them.
  • docker-compose rm : Remove stopped containers without using 'down'.
  • docker-compose logs -f : Follow real-time logs of containers.
  • docker-compose ps : List running containers.

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

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/

MySQL example

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.1
Then 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.

Deploy 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:

  1. Create an account to Docker Hub
  2. Create an account to Render
  3. In Docker Desktop login to your Docker Hub
  4. Create container image
  5. Push the image to Docker Hub
  6. In Render create Webservice and connect it to your Docker Hub repo

Example

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.

  1. Create the image with the command
    docker build -t dh_user/imagename:latest path_to_dockerfile/
    
  2. Push the image to Docker Hub with the command
    docker push dh_user/imagename:latest
    
  3. Follow the instructions from https://render.com/docs/deploy-an-image
NOTE:
  • dh_user is your username in Docker Hub
  • latest is the tagname (*)

(*) 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
Trigger Deploy

You can configure Render to Deploy the image automatically whenever you push a new version to Docker Hub. You can do it like this

  1. In Render open the settings of your service (example docker_express)
  2. Copy the Deploy Hook URL
  3. In Docker Hub open the Repository and click Webhooks from the menu
  4. Give some name for the New Webhook and paste the WebHook URL



Toggle Menu