Docker – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 31 Dec 2022 11:25:48 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Running a Cronjob Inside Docker: A Beginner’s Guide https://tecadmin.net/running-a-cronjob-inside-docker/ https://tecadmin.net/running-a-cronjob-inside-docker/#respond Sat, 31 Dec 2022 11:15:45 +0000 https://tecadmin.net/?p=31291 When it comes to scheduling jobs and programs that automatically run at set intervals or can be triggered by another event, you have plenty of options. You can use a general-purpose utility like cron, the built-in scheduler in macOS or Linux, or a specialized tool like AWS Lambda. Cron, though not as powerful as AWS [...]

The post Running a Cronjob Inside Docker: A Beginner’s Guide appeared first on TecAdmin.

]]>
When it comes to scheduling jobs and programs that automatically run at set intervals or can be triggered by another event, you have plenty of options. You can use a general-purpose utility like cron, the built-in scheduler in macOS or Linux, or a specialized tool like AWS Lambda. Cron, though not as powerful as AWS Lambda, is a solid choice if you’re using containers because it’s designed to handle background tasks on Unix systems. With Docker, however, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to work.

How to Dockerize a Cron Job

To run a cron job inside a Docker container, you will need to use the cron service and run it in the foreground in your Docker container.

Here’s an example of how you can set this up:

  1. Create Cron File:
  2. Create a file that contains all the cron jobs to be run under the Docker container. For example, let’s call this file `cron`.

    cat cron 
    

    Our example file looks like:

    cron
    * * * * * echo "Current date is `date`" > /var/log/cron

    Add all of the crontab jobs in the above file that needs to be run under the Docker container.

  3. Create Dockerfile
  4. Next, create a `Dockerfile` that installs the cron service and copies the script into the container. Here we have provided 3 examples of Dockerfile using the different-2 operating system. The Alpine Linux is very lightweight and is sufficient for running cronjobs. But if you need to set up other environments Linux Ubuntu, Apache, and PHP. choose any of the below given Dockerfile templates based on your requirements.

    • Dockerfile with Alpine Linux
    • FROM alpine:3
      
      # Copy cron file to the container
      COPY cron /etc/cron.d/cron
      
      # Give the permission
      RUN chmod 0644 /etc/cron.d/cron
      
      # Add the cron job
      RUN crontab /etc/cron.d/cron
      
      # Link cron log file to stdout
      RUN ln -s /dev/stdout /var/log/cron
      
      # Run the cron service in the foreground
      CMD [ "crond", "-l", "2", "-f" ]

    • Dockerfile with Apache and PHP
    • FROM php:8.0-apache
      
      # Install cron
      RUN apt update && \
          apt -y install cron
      
      # Copy cron file to the container
      COPY cron /etc/cron.d/cron
      
      # Give the permission
      RUN chmod 0644 /etc/cron.d/cron
      
      # Add the cron job
      RUN crontab /etc/cron.d/cron
      
      # Link cron log file to stdout
      RUN ln -s /dev/stdout /var/log/cron
      
      # Start cron service
      RUN sed -i 's/^exec /service cron start\n\nexec /' /usr/local/bin/apache2-foreground

    • Dockerfile with Ubuntu Linux
    • FROM ubuntu:latest
      
      # Install cron deamon
      RUN apt update && apt install -y cron
      
      # Copy cron file to the container
      COPY cron /etc/cron.d/cron
      
      # Give the permission 
      RUN chmod 0644 /etc/cron.d/cron
      
      # Add the cron job
      RUN crontab /etc/cron.d/cron
      
      # Link cron log file to stdout
      RUN ln -s /dev/stdout /var/log/cron
      
      # Run the cron service in the foreground
      CMD ["cron", "-f"]

  5. Build and Run Container
  6. You have two files in your current directory. One is `cron` that contains the cronjobs and the second is Dockerfile that have the build instructions for Docker. Run the following command to build the Docker image using the Dockerfile:

    docker build -t my_cron . 
    

    Once the image is successfully built, You can launch a container using the image:

    docker run -d my_cron  
    

    This will start the cron daemon under the container, which will all the scheduled jobs defined in `cron` file.

  7. Test Setup
  8. The cronjobs should be successfully configured with the docker containers. As we have linked cron log file (/var/log/cron) to the /dev/stdout. Show all the logs generated by the cron service can be seed in `docker logs` command.

    First, find the container id or name using `docker ps` command.

    docker ps 
    

    Then check the log files of the Docker container.

    docker logs container_id
    

    In the cronjobs, I have printed the current date and written them to logs. You can see the below image that shows the current date in logs every minute.

    Running Cronjobs in Docker
    Running Cronjobs in Docker

    It means the cron jobs are running properly under the Docker container.

Wrapping Up

Cron jobs are a handy way to automate daily tasks on your computer, like backing up files. With Docker, though, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to run. When running a cron job in a docker container, you’ll run into a few challenges depending on how and where you want to run the container. The cron job interval is specified in seconds and can range from one minute to just over 100 years. The cron job frequency is specified in terms of the number of times the cron job should be executed every day.

When it comes to scheduling jobs and programs that automatically run at set intervals or can be triggered by another event, you have plenty of options. You can use a general-purpose utility like cron, the built-in scheduler in macOS or Linux, or a specialized tool like AWS Lambda. Cron, though not as powerful as AWS Lambda, is a solid choice if you’re using containers because it’s designed to handle background tasks on Unix systems. With Docker, however, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to work.

The post Running a Cronjob Inside Docker: A Beginner’s Guide appeared first on TecAdmin.

]]>
https://tecadmin.net/running-a-cronjob-inside-docker/feed/ 0
Docker Run: A Beginner’s Guide to Run Docker Containers https://tecadmin.net/docker-run/ https://tecadmin.net/docker-run/#respond Fri, 30 Dec 2022 09:08:28 +0000 https://tecadmin.net/?p=33260 Docker is a popular tool for packaging and deploying applications, and a key part of the Docker workflow is running Docker containers. In this beginner’s guide, we will explain what Docker containers are and how to run them. What is a Docker Container? A Docker container is a lightweight and standalone executable package that includes [...]

The post Docker Run: A Beginner’s Guide to Run Docker Containers appeared first on TecAdmin.

]]>
Docker is a popular tool for packaging and deploying applications, and a key part of the Docker workflow is running Docker containers. In this beginner’s guide, we will explain what Docker containers are and how to run them.

What is a Docker Container?

A Docker container is a lightweight and standalone executable package that includes everything needed to run an application, including the code, a runtime, libraries, environment variables, and config files.

Containers are built on top of Docker’s open-source containerization technology, which allows developers to package and deploy applications in a container, making it easier to run applications consistently on different environments.

How to Run a Docker Container

To run a Docker container, you need to have a Docker image, which is a packaged application that can be deployed in a container. You can build a Docker image using the Dockerfile and docker build commands, or you can pull an image from a registry, such as Docker Hub.

Once you have an image, you can use the docker run command to run the container. The docker run command takes the following syntax:

## Syntax
docker run [options] IMAGE [COMMAND] [ARG...]

Here are some common options for the docker run command:

  • -d: Run the container in detached mode, which means it will run in the background.
  • -p: Publish a container’s port to the host. For example, -p 8080:80 will expose port 80 in the container as port 8080 on the host.
  • -e: Set an environment variable in the container. For example, -e MY_VAR=value will set the MY_VAR environment variable to value in the container.
  • --name: Assign a name to the container.

Here’s an example of how to run a Docker container using the docker run command:

docker run -d -p 8080:80 --name my-web-server nginx 

This command will run the nginx image in detached mode, exposing port 80 in the container as port 8080 on the host, and giving the container the name my-web-server.

Running Multiple Containers

You can run multiple containers using the docker-compose tool, which allows you to define and run multi-container applications in a single command. To use docker-compose, you need to create a docker-compose.yml file that defines the containers and their dependencies.

Here’s an example docker-compose.yml file that runs a web server and a database:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: password

To run the containers defined in the docker-compose.yml file, you can use the docker-compose up command:

docker-compose up 

This will start the web server and database containers and run them in the foreground. To run the containers in detached mode, you can use the -d flag:

docker-compose up -d 

Stopping and Removing Containers

To stop a running container, you can use the docker stop command followed by the container name or ID. For example, to stop the my-web-server container, you can use the following command:

docker stop my-web-server 

To remove a stopped container, you can use the docker rm command followed by the container name or ID. For example, to remove the my-web-server container, you can use the following command:

docker rm my-web-server 

If you want to stop and remove all containers, you can use the docker container prune command. This will remove all stopped containers, as well as any networks and volumes that are not in use.

docker container prune 

Best Practices for Running Containers

Here are a few best practices to follow when running Docker containers:

  • Use the latest version of Docker: It’s important to keep Docker up to date to ensure that you have the latest features and security patches.
  • Use the right base image: Choose a base image that is appropriate for your application, such as an operating system image or a language runtime image.
  • Use environment variables: Instead of hardcoding values in the Dockerfile or docker-compose.yml file, use environment variables to make the container more flexible and easier to modify.
  • Use volumes: Volumes allow you to persist data outside of the container and make it available to other containers. This can be useful for storing data that needs to be preserved, such as a database.
  • Use the `--restart` option: The `--restart` option allows you to specify the behavior of the container when it exits. For example, you can use the --restart always option to automatically restart the container if it exits.
  • Use resource limits: You can use the `--cpus` and `--memory` options to limit the resources that a container can use, which can help prevent resource contention and improve performance.

By following these best practices, you can run Docker containers efficiently and effectively.

Conclusion

In this guide, we have covered the basics of running Docker containers and provided some best practices to follow. By using the docker run and docker-compose commands, you can easily deploy and manage containers in your environment.

The post Docker Run: A Beginner’s Guide to Run Docker Containers appeared first on TecAdmin.

]]>
https://tecadmin.net/docker-run/feed/ 0
Docker Build: A Beginner’s Guide to Building Docker Images https://tecadmin.net/docker-build/ https://tecadmin.net/docker-build/#respond Thu, 29 Dec 2022 03:44:34 +0000 https://tecadmin.net/?p=33255 Docker is a popular tool for packaging and deploying applications in an isolated environment, and a key part of the Docker workflow is building Docker images. We can build our own images with the help of base images and use them to create containers. We can also pull the images directly from the docker hub [...]

The post Docker Build: A Beginner’s Guide to Building Docker Images appeared first on TecAdmin.

]]>
Docker is a popular tool for packaging and deploying applications in an isolated environment, and a key part of the Docker workflow is building Docker images. We can build our own images with the help of base images and use them to create containers. We can also pull the images directly from the docker hub (https://hub.docker.com/) for our application. In this beginner’s guide, we will explain what Docker images are and how to build them.

What is a Docker Image?

A Docker image is a lightweight, stand-alone, and executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

Docker images are built on top of Docker’s open-source containerization technology, which allows developers to package and deploy applications in a container, making it easier to run applications consistently in different environments.

How to Build a Docker Image

To build a Docker image, you need to create a Dockerfile, which is a text file that contains a set of instructions for building the image. The Dockerfile tells Docker what to do when building the image, such as which files to include, which libraries to install, and which commands to run.

Here’s an example Dockerfile that creates a simple Python image:

FROM python:10-slim

COPY . /app
WORKDIR /app

RUN pip install -r requirements.txt

CMD ["python", "main.py"]

In this example:

  • The Dockerfile starts with a `FROM` instruction, which specifies the base image to use as the starting point for the new image. In this case, we are using the `python:3.10-slim` image, which includes a minimal version of Python 3.10.
  • The `COPY` instruction copies the files from the current directory (.) into the `/app` directory in the image.
  • The `WORKDIR` instruction sets the working directory to `/app`, so that any subsequent commands are run from that directory.
  • The `RUN` instruction runs a command to install the Python dependencies specified in the `requirements.txt` file.
  • Finally, the `CMD` instruction specifies the command to run when the container is started. In this case, it runs the `main.py` Python script.

To build the image, you can use the docker build command and specify the `Dockerfile` and a name for the image:

docker build -t my-python-app . 

This will build the image and give it the tag `my-python-app`. You can then run the image using the `docker run` command:

docker run my-python-app 

Manageing the Docker Images

Once you have built Docker images, there are a few tasks that you may need to perform to manage them. Here are some common tasks for managing Docker images:

  • Listing images:
  • You can use the `docker images` command to list all the images on your system. The output will include the repository, tag, and image ID for each image.

    docker images 
    
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    my-python-app       latest              fc6a1ed7b9fb        3 minutes ago       967MB
    python              3.10-slim            bd4e6cdb1e80        7 days ago          142MB
    

  • Removing images:
  • To remove an image, you can use the `docker rmi` command followed by the image ID or repository and tag. For example, to remove the my-python-app image, you can use the following command:

    docker rmi my-python-app 
    

  • Tagging images:
  • You can use tags to give your images descriptive names and easily manage multiple versions. To tag an image, you can use the `docker tag` command followed by the `image ID`, the repository and tag name, and the tag value. For example, to tag the `my-python-app` image with the `1.0` tag, you can use the following command:

    docker tag fc6a1ed7b9fb my-python-app:1.0 
    

  • Pushing images to a registry:
  • A registry is a storage location for Docker images, and you can use a registry to share your images with others or deploy them to a production environment. To push an image to a registry, you first need to log in to the registry using the `docker login` command, and then use the `docker push` command to upload the image. For example, to push the `my-python-app` image to the Docker Hub registry, you can use the following commands:

    docker login 
    docker push my-python-app 
    

  • Pulling images from a registry:
  • To pull an image from a registry, you can use the `docker pull` command followed by the repository and tag name. For example, to pull the my-python-app image from the Docker Hub registry, you can use the following command:

    docker pull my-python-app 
    

By using these commands, you can easily manage your Docker images and share them with others.

Best Practices for Building Docker Images

There are a few best practices to keep in mind when building Docker images:

  • Use a minimal base image: It’s generally a good idea to use a minimal base image, such as alpine or slim, to keep the size of the image as small as possible.
  • Keep the Dockerfile simple: Try to keep the Dockerfile as simple as possible, with each instruction doing one thing. This will make it easier to understand and maintain.
  • Use multistage builds: If your application has multiple build steps, such as building and testing, you can use multistage builds to create a single image that includes only the necessary components. This can help reduce the size of the image.
  • Use caching: Docker has a built-in cache mechanism that stores the intermediate results of each instruction in the `Dockerfile
  • Use a .dockerignore file: Similar to a .gitignore file, a .dockerignore file allows you to specify patterns for files and directories that should be excluded when building the image. This can help improve build performance and keep the image size smaller.
  • Use environment variables: Instead of hardcoding values in the Dockerfile, you can use environment variables to make the image more flexible and easier to modify.
  • Keep the image updated: It’s important to keep the base image and any installed packages up to date to ensure that you have the latest security patches and features.
  • Test the image: Before deploying the image, it’s a good idea to test it to make sure it works as expected. You can use Docker Compose to set up a local development environment and run the image in a container.

Conclusion

In this guide, we have covered the basics of building Docker images and provided some best practices to follow. By following these guidelines, you can create efficient and reliable Docker images that can be easily deployed and maintained.

The post Docker Build: A Beginner’s Guide to Building Docker Images appeared first on TecAdmin.

]]>
https://tecadmin.net/docker-build/feed/ 0
Discover the Power of Docker: Installing and Using Docker on Ubuntu and Debian! https://tecadmin.net/install-docker-on-ubuntu/ https://tecadmin.net/install-docker-on-ubuntu/#comments Mon, 05 Dec 2022 11:26:22 +0000 https://tecadmin.net/?p=9931 Docker is an open-source platform that enables developers to create, deploy, and manage applications in a lightweight, secure, and efficient manner. It uses containers, which are lightweight and portable, to package applications and related dependencies into isolated environments. Docker containers can be deployed on any operating system and can be used to run applications in [...]

The post Discover the Power of Docker: Installing and Using Docker on Ubuntu and Debian! appeared first on TecAdmin.

]]>
Docker is an open-source platform that enables developers to create, deploy, and manage applications in a lightweight, secure, and efficient manner. It uses containers, which are lightweight and portable, to package applications and related dependencies into isolated environments. Docker containers can be deployed on any operating system and can be used to run applications in any language or framework.

Docker is based on the idea of containerization, which is the process of packaging applications and their dependencies in isolated environments. This helps developers quickly and easily deploy applications without having to worry about managing dependencies and configuring system settings.

Docker containers are also highly portable and can be easily transferred from one system to another. This makes it possible to develop applications on one machine, deploy them on another, and move them between different hosting environments with minimal effort.

Advantages of Using Docker

Docker has several advantages over traditional application deployment methods.

  • First, Docker makes it easy to package and deploy applications in a consistent manner. This ensures that applications are always running the same version of the code and have the same configuration across all environments.
  • Second, Docker containers are lightweight, making them fast and easy to deploy. This makes it possible to quickly scale up or down as needed, reducing the cost of hosting applications.
  • Third, Docker containers are secure. Each container is isolated from other containers, making it difficult for malicious code to spread across systems. This also makes it easier to apply security patches, as only the containers affected by the patch need to be updated.
  • Finally, Docker makes it easy to manage applications at scale. With the help of orchestration tools such as Kubernetes, developers can easily manage the lifecycle of multiple containers and ensure that applications are running smoothly.

Installing Docker on Ubuntu & Debian

Installing Docker on Ubuntu is a straightforward process. Simply follow the below steps:

  1. First of all, remove the default version of Docker from your system:
    sudo apt remove docker docker-engine docker.io 
    
  2. Now, Install the following packages that are required to the next steps.
    sudo apt update 
    sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release 
    
  3. Configure the Docker’s GPG key on your system.
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 
    

    The Debian users must replace ubuntu with debian.

  4. Then you need to create the Docker repository file. This can be done by running the following command in a terminal window:
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
     https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
     sudo tee /etc/apt/sources.list.d/docker1.list > /dev/null  
    

    The Debian users must replace ubuntu with debian.

  5. Finally, install Docker community edition and other required packages on Ubuntu and Debian systems.
    sudo apt update 
    sudo apt install docker-ce docker-ce-cli containerd.io 
    
  6. Once the installation is complete, you can verify that Docker is installed by running the following command:
    docker --version
    
    Output:
    Docker version 20.10.21, build baeda1f

    This will output the version of Docker that is installed on the system.

Using Docker on Ubuntu & Debian

Once Docker is installed, you can use it to create and manage containers on your system. To start a container, use the following command:

docker run image_name 

This will start a container with the specified image. You can also specify additional options to customize the container, such as port mappings and environment variables.

Once the container is running, you can use the following command to view its status:

docker ps

This will display a list of all running containers, including the one that you just started.

Creating Docker Images

Docker images are the basis for containers. They are created from a Dockerfile, which is a text file that contains instructions for building the image.

To create a Docker image, first, create a Dockerfile and save it in the same folder as your application code. The Dockerfile should contain instructions for building the image. For example, the following Dockerfile will create an image with the latest version of Ubuntu and install the Apache web server:

FROM ubuntu:latest 
RUN apt update && apt install -y apache2 
EXPOSE 80 
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Once the Dockerfile is complete, you can build the image by running the following command:

docker build -t image_name  .
Installing and Using Docker on Ubuntu and Debian
Build docker image

This will create an image with the specified name.

Running Docker Containers

Once an image is created, you can use it to run containers. To start a container, use the following command:

docker run -d -it image_name 
Installing and Using Docker on Ubuntu and Debian
Creating docker container from image

This will start a container with the specified image. The -d parameter runs the container in daemon mode, So you get back to the terminal immediately. You can also specify additional options to customize the container, such as port mappings and environment variables.

Managing Docker Containers

Once a container is running, you can manage it using the Docker command-line interface. To view a list of running containers, use the following command:

docker ps 

This will display a list of all running containers, including the one that you just started.

Installing and Using Docker on Ubuntu and Debian
List running docker containers

To stop a container, use the following command:

docker stop container_id 

This will stop the specified container.

You can also view the log output from a running container by using the following command:

docker logs container_id 

This will output the log output from the specified container.

You can also delete the Docker container by using the following command:

docker delete container_id 

This will delete the specified Docker container.

Docker and Security

Docker containers are secure by default. Each container is isolated from other containers, making it difficult for malicious code to spread across systems.

However, it is still important to follow best practices when working with Docker. This includes using only trusted images, running containers with the least amount of privileges, and utilizing security tools such as SELinux and AppArmor to lock down containers.

Conclusion

In this article, we discussed how to install and use Docker on Ubuntu, one of the most popular Linux distributions. We discussed the benefits of using Docker, how to install it on Ubuntu, and how to create and manage Docker containers. Finally, we discussed how to ensure the security of your Docker containers.

If you are looking for an easy and efficient way to manage applications on Ubuntu, then Docker is a great choice. With Docker, you can quickly and easily deploy applications in a secure and consistent manner. So why not give it a try and discover the power of Docker for yourself?

The post Discover the Power of Docker: Installing and Using Docker on Ubuntu and Debian! appeared first on TecAdmin.

]]>
https://tecadmin.net/install-docker-on-ubuntu/feed/ 3
How to run “npm start” through Docker https://tecadmin.net/dockerfile-npm-start/ https://tecadmin.net/dockerfile-npm-start/#respond Fri, 01 Jul 2022 12:11:15 +0000 https://tecadmin.net/?p=30433 npm is a software package manager for JavaScript programming language. npm makes it easy for JavaScript developers to share the code they write. npm also provides a command-line interface to manage the dependencies in a project. Docker is a containerization platform that allows developers to package their applications and dependencies into a portable image. npm [...]

The post How to run “npm start” through Docker appeared first on TecAdmin.

]]>
npm is a software package manager for JavaScript programming language. npm makes it easy for JavaScript developers to share the code they write. npm also provides a command-line interface to manage the dependencies in a project. Docker is a containerization platform that allows developers to package their applications and dependencies into a portable image.

npm with Docker makes it easy to package and ship Node.js applications. npm with Docker also enables developers to share their code easily. npm with Docker is an excellent tool for JavaScript developers who want to share their code with others.

Dockerfile for npm start

npm start is frequently used command to run a node application like: Reactjs. Use can use the below Dockerfile for running node applications with Docker.

Create a file named Dockerfile in the project base directory and add the below code.

FROM node:16-alpine

RUN mkdir /app
WORKDIR /app
COPY package.json /app

RUN npm install
COPY . /app
EXPOSE 3000
CMD ["npm", "start"]

Make sure to change the value of EXPOSE to the port application runs on. Also assuming that your application runs with npm start command.

Now, build a docker image for your application. In a terminal, run the following command from the application base directory.

docker build -t image-name . 

Once the image build is completed, you can run your application.

sudo docker run -it -d image-name 

That’s it.

The post How to run “npm start” through Docker appeared first on TecAdmin.

]]>
https://tecadmin.net/dockerfile-npm-start/feed/ 0
Docker-compose with Persistent MySQL Data https://tecadmin.net/docker-compose-persistent-mysql-data/ https://tecadmin.net/docker-compose-persistent-mysql-data/#comments Wed, 01 Jul 2020 13:13:17 +0000 https://tecadmin.net/?p=21932 It is importent to keep data persistent for containers running databases. Docker provides you option to keep database files persistent over the docker volumes or storing files directly on host machine. Use one of the below options to keep MySQL data persistent even after recreating or deleting docker containers. Option 1 – Storing MySQL Data [...]

The post Docker-compose with Persistent MySQL Data appeared first on TecAdmin.

]]>
It is importent to keep data persistent for containers running databases. Docker provides you option to keep database files persistent over the docker volumes or storing files directly on host machine.

Use one of the below options to keep MySQL data persistent even after recreating or deleting docker containers.

Option 1 – Storing MySQL Data on Docker Volumes

The Docker volumes are preferred mechanism by the Docker for storing persistent data of Docker containers. You can easily create a Docker volume on your host machine and attach to a Docker containers.

Let’s create a docker-compose file on your system with the following content.

docker-compose.yml:

version: '3'

services:
  db:
    image: mysql:5.7
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_password
      MYSQL_DATABASE: app_db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_user_pass
    ports:
      - "6033:3306"
    volumes:
      - dbdata:/var/lib/mysql
volumes:
  dbdata:

The above configuration defined one data volume named “dbdata”, which is attached to MySQL container and mounted on /var/lib/mysql directory. This is the default directory used by MySQL to store all data files.

Next, run below command to launch Docker container.

docker-compose up -d

Output:

Creating network "db_default" with the default driver
Creating volume "db_dbdata" with default driver
Creating db ... done

You can view the docker volumes by running commnad:

docker volume ls

Option 2 – Storing MySQL Data on Host Machine

We recommend to use data volume instead of putting files on host machine. But, If you like, you can keep database files on the host machine. In any case docker container get terminated, you can relaunch container using the existing data files.

Create a directory to keep your MySQL data files. I am creating below directory structure under the current directory.

mkdir -p ./data/db

Then configure docker-compose.yml to use ./data/db as volume to store all files created by the MySQL server. Next create compose file in current directory.

docker-compose.yml:

version: '3'

services:
  db:
    image: mysql:5.7
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_password
      MYSQL_DATABASE: app_db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_user_pass
    ports:
      - "6033:3306"
    volumes:
      - ./data/db:/var/lib/mysql

After creating file, just run the below command to launch container.

docker-compose up -d

Output:

Creating network "db_default" with the default driver
Creating db ... done

In this case the MySQL container creats all files on host machine under ./data/db directory. To view these files, just run below command.

ls -l ./data/db

drwxr-x--- 2 systemd-coredump systemd-coredump     4096 Jul  1 11:07 app_db
-rw-r----- 1 systemd-coredump systemd-coredump       56 Jul  1 11:07 auto.cnf
-rw------- 1 systemd-coredump systemd-coredump     1676 Jul  1 11:07 ca-key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 Jul  1 11:07 ca.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 Jul  1 11:07 client-cert.pem
-rw------- 1 systemd-coredump systemd-coredump     1680 Jul  1 11:07 client-key.pem
-rw-r----- 1 systemd-coredump systemd-coredump     1346 Jul  1 11:07 ib_buffer_pool
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 Jul  1 11:07 ib_logfile0
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 Jul  1 11:07 ib_logfile1
-rw-r----- 1 systemd-coredump systemd-coredump 79691776 Jul  1 11:07 ibdata1
-rw-r----- 1 systemd-coredump systemd-coredump 12582912 Jul  1 11:07 ibtmp1
drwxr-x--- 2 systemd-coredump systemd-coredump     4096 Jul  1 11:07 mysql
drwxr-x--- 2 systemd-coredump systemd-coredump     4096 Jul  1 11:07 performance_schema
-rw------- 1 systemd-coredump systemd-coredump     1680 Jul  1 11:07 private_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump      452 Jul  1 11:07 public_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 Jul  1 11:07 server-cert.pem
-rw------- 1 systemd-coredump systemd-coredump     1680 Jul  1 11:07 server-key.pem
drwxr-x--- 2 systemd-coredump systemd-coredump    12288 Jul  1 11:07 sys

The post Docker-compose with Persistent MySQL Data appeared first on TecAdmin.

]]>
https://tecadmin.net/docker-compose-persistent-mysql-data/feed/ 1
How to Install Docker on Ubuntu 20.04 https://tecadmin.net/install-docker-on-ubuntu-20-04/ https://tecadmin.net/install-docker-on-ubuntu-20-04/#comments Fri, 19 Jun 2020 18:18:48 +0000 https://tecadmin.net/?p=21796 Docker is a container-based application framework, which wraps a specific application with all its dependencies in a container. Docker containers can easily to ship to the remote location on start there without making entire application setup. The Docker official team provides PPA for installing Docker on Ubuntu 20.04 using PPA. You just need to configure [...]

The post How to Install Docker on Ubuntu 20.04 appeared first on TecAdmin.

]]>
Docker is a container-based application framework, which wraps a specific application with all its dependencies in a container. Docker containers can easily to ship to the remote location on start there without making entire application setup.

The Docker official team provides PPA for installing Docker on Ubuntu 20.04 using PPA. You just need to configure Docker PPA to your system before installing Docker on Ubuntu system.

This tutorial will help you to install Docker on Ubuntu 20.04 LTS Focal Fossa systems.

Prerequisite

Login to your Ubuntu 20.04 system with sudo privileged user. Then run the following commands to install required packages.

sudo apt update 
sudo apt install curl apt-transport-https ca-certificates software-properties-common 

Step 1 – Installing Docker on Ubuntu

First of all, import the GPG key to your system to verify packages signature before installing them. To import key run the below command on terminal.

After that add the Docker repository on your Ubuntu system which contains Docker packages including its dependencies. You must have to enable this repository to install Docker on Ubuntu.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add 
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable" 

Your system is now ready for Docker installation. Next, execute the following commands to upgrade apt index and then install Docker on Ubuntu 20.04 Linux system.

sudo apt update 
sudo apt install docker-ce docker-ce-cli containerd.io 

After successful installation of Docker community edition, the service will start automatically, Use below command to verify service status.

Your system is now ready for running Docker containers. Use our Docker Tutorial for Beginners to working with Docker.

Step 2 – Manage Docker Service

As usual Docker service is also managed under the Systemd daemon. You can use systemctl commands to stop, start or view status of Docker service.

Run the below command to view service status:

sudo systemctl status docker 

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-06-17 16:41:20 UTC; 1min 58s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 926989 (dockerd)
      Tasks: 8
     Memory: 35.7M
     CGroup: /system.slice/docker.service
             └─926989 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 17 16:41:20 tecadmin systemd[1]: Started Docker Application Container Engine.
Jun 17 16:41:20 tecadmin dockerd[926989]: time="2020-06-17T16:41:20.247640882Z" level=info msg="API listen on /run/docker.sock"

Use below command to stop, start or restart Docker service:

sudo systemctl stop docker 
sudo systemctl start docker 
sudo systemctl restart docker 

Step 3 – Run Docker Hello World

You have successfully installed Docker on Ubuntu system. The Docker engine service is also running fine. Next, run a hello world example to verify that everything is fine.

To run Docker hello world example, open a terminal and type:

sudo docker run hello-world 

You will see the results like below. It means Docker is properly configured on your system.

how to install docker on ubuntu 20.04

Step 4 – Install Docker Compose

Download the latest version of Docker compose tool from Github. Use the below commands to download and install Docker compose 1.26.0. Before installing make sure compatibility with your docker version.

curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose 
chmod +x /usr/local/bin/docker-compose 

Conclusion

All done, you have successfully installed Docker on Ubuntu 20.04 system using PPA. Additionally, you also have configured Docker-compose on your system.

The post How to Install Docker on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-docker-on-ubuntu-20-04/feed/ 4
How to Install Docker on CentOS/RHEL 8 https://tecadmin.net/install-docker-centos-8/ https://tecadmin.net/install-docker-centos-8/#comments Sat, 11 Apr 2020 07:04:19 +0000 https://tecadmin.net/?p=19975 Docker is an OS level virtualization platform used for container-based application. Which wrap of a specific application with all its dependencies in a container. Docker containers can easily ship to a remote location on start there without making entire application setup. This tutorial will help you to install and manage Docker community edition on CentOS/RHEL [...]

The post How to Install Docker on CentOS/RHEL 8 appeared first on TecAdmin.

]]>
Docker is an OS level virtualization platform used for container-based application. Which wrap of a specific application with all its dependencies in a container. Docker containers can easily ship to a remote location on start there without making entire application setup. This tutorial will help you to install and manage Docker community edition on CentOS/RHEL 8 Linux system.

Step 1 – Enable Docker Repository

First of all, add the official Docker yum repository on your CentOS 8 system.

sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Step 2 – Install Docker on CentOS 8

After adding the yum repository to your CentOS system, update the yum cache by executing the following command.

sudo dnf makecache

Now install docker community edition package to install docker on your system. This is installed many of the required decencies on your system.

sudo dnf install --nobest docker-ce

The –nobest option instruct installer to not to limit for best candidate package dependency.

Step 3 – Manage Docker Service

Once the Docker successfully installed on your CentOS 8 system. Use the following commands to enable Docker serivce and start it.

sudo systemctl enable docker.service
sudo systemctl start docker.service

Then check the Docker service status.

sudo systemctl status docker.service

Result

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-04-10 05:26:46 UTC; 1s ago
     Docs: https://docs.docker.com
 Main PID: 23263 (dockerd)
    Tasks: 18
   Memory: 50.0M
   CGroup: /system.slice/docker.service
           ├─23263 /usr/bin/dockerd -H fd://
           └─23275 containerd --config /var/run/docker/containerd/containerd.toml --log-level info

Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.439527082Z" level=info msg="Graph migration to >
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.440174585Z" level=warning msg="Your kernel does>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.440197735Z" level=warning msg="Your kernel does>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.440723426Z" level=info msg="Loading containers:>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.677587189Z" level=info msg="Default bridge (doc>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.801904550Z" level=info msg="Loading containers:>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.861334755Z" level=info msg="Docker daemon" comm>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.864579987Z" level=info msg="Daemon has complete>
Apr 10 05:26:46 tecadmin dockerd[23263]: time="2020-04-10T05:26:46.881460358Z" level=info msg="API listen on /var/>
Apr 10 05:26:46 tecadmin systemd[1]: Started Docker Application Container Engine.

Docker has been installed and running on your CentOS 8 operating system. You can visit our Docker tutorial section to work with Docker containers.

Step 4 – Test Docker on CentOS 8

Search Docker Images

First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output

sudo docker search hello-world

Download Docker Images

Now download the Docker container with name Ubuntu on your local system using following commands.

sudo docker pull hello-world

Output:

Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

Now make sure that above images have been downloaded successfully on your system. Below command list all images.

sudo docker images

Output:

REPOSITORY          TAG          IMAGE ID          CREATED             SIZE
centos              latest       470671670cac      2 months ago        237MB
hello-world         latest       fce289e99eb9      15 months ago       1.84kB

Run Hello-World Docker Container

Use the following command to run a hello-world docker container. This container will print a message on screen and exit immediately.

docker run -i hello-world

You will see the results like below screenshot. The success message shows that Docker service is properly installed on your CentOS 8 system.

Install Docker CentOS 8

The post How to Install Docker on CentOS/RHEL 8 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-docker-centos-8/feed/ 3
How to Clear Log Files of A Docker Container https://tecadmin.net/truncate-docker-container-logfile/ https://tecadmin.net/truncate-docker-container-logfile/#comments Mon, 09 Mar 2020 17:16:10 +0000 https://tecadmin.net/?p=20944 This tutorial will help you clear the log file on a Docker container. If your system is getting out of disk space and you found that the docker container’s log files are consuming high disk space. you can find the log file location and clear them with the help of this tutorial. While clearing log [...]

The post How to Clear Log Files of A Docker Container appeared first on TecAdmin.

]]>
This tutorial will help you clear the log file on a Docker container. If your system is getting out of disk space and you found that the docker container’s log files are consuming high disk space. you can find the log file location and clear them with the help of this tutorial. While clearing log files of a docker container, you don’t need to stop it.

Clear Docker Container Log File

Below are the 3 different options to clear log files of Docker containers. Choose any one of the below options to truncate the docker container log files.

Some of the below options require a container id or name, which can be found with the docker ps -a command.

  • Option 1: In this option, first we will find the log file path and then truncate it. Use inspect option to find the log file name and location of a docker container.
    docker container inspect  --format='{{.LogPath}}' <container_name_or_id>
    

    As a result, you will get a log file path. Now truncate the log file with the following command.

    truncate -s 0 /path/to/logfile 
    

    Here -s is used to set the size of a file. You provided 0 as input, which means completely truncating the log file.

  • Option 2: You can combine both commands in a single command. Use the following command to truncate the log file of the specified Docker container.
    truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>) 
    
  • Option 3: Truncate the log files of all docker containers in your system.
    truncate -s 0 /var/lib/docker/containers/*/*-json.log 
    

You can quickly truncate the docker log files using one of the above options.

Wrap Up

In this blog post, you have learned to truncate (clear) log files of a Docker container.

The post How to Clear Log Files of A Docker Container appeared first on TecAdmin.

]]>
https://tecadmin.net/truncate-docker-container-logfile/feed/ 4
How to Install Docker on Debian 10 (Buster) https://tecadmin.net/install-docker-on-debian-10-buster/ https://tecadmin.net/install-docker-on-debian-10-buster/#respond Wed, 17 Jul 2019 17:58:45 +0000 https://tecadmin.net/?p=18901 Docker is a container-based application framework, which wraps a specific application with all its dependencies in a container. Docker containers can easily to ship to the remote location on start there without making entire application setup. This tutorial will help you to install Docker on Debian 10 Buster Linux distribution. Step 1 – Prerequisites First [...]

The post How to Install Docker on Debian 10 (Buster) appeared first on TecAdmin.

]]>
Docker is a container-based application framework, which wraps a specific application with all its dependencies in a container. Docker containers can easily to ship to the remote location on start there without making entire application setup. This tutorial will help you to install Docker on Debian 10 Buster Linux distribution.

Step 1 – Prerequisites

First of all, remove any default Docker packages from the system before installation Docker on a Linux VPS. Execute commands to remove unnecessary Docker versions.

sudo apt-get purge docker lxc-docker docker-engine docker.io

Now, install some required packages on your system for installing Docker on Debian system. Run the below commands to do this:

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

Step 2 – Setup Docker PPA

After that, you need to import dockers official GPG key to verify packages signature before installing them with apt-get. Run the below command on terminal.

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

After that add the Docker repository on your Debian system which contains Docker packages including its dependencies. You must have to enable this repository to install Docker on Debian.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian buster stable"

Step 3 – Install Docker on Debian 10

Your system is now ready for Docker installation. Run the following commands to upgrade apt index and then install Docker community edition on Debian.

sudo apt-get update
sudo apt-get install docker-ce

After successful installation of Docker community edition, the service will start automatically, Use below command to verify service status.

sudo systemctl status docker

Your system is now ready for running Docker containers. Use our Docker Tutorial for Beginners to working with Docker.

Step 4 – How to Use Docker

After installation of Docker on a Linux. Here are some basic details for search and download Docker images, launch containers and manage them.

Search Docker Images

docker search debian

Download Docker Images

docker pull debian

Now make sure that the above images have been downloaded successfully on your system. Below command list all images.

docker images

REPOSITORY    TAG          IMAGE ID            CREATED         SIZE
debian        latest       3bbb526d2608        4 weeks ago     101MB

Launch New Container with Image

docker run -i -t debian /bin/bash

To exit from docker container type CTRL + P + Q. This will leave container running in the background an provide you host system console. If you used the exit command, it will stop the current container.

After exiting from Docker container, execute below command to list all running containers.

docker ps

CONTAINER ID     IMAGE     COMMAND        CREATED        STATUS        PORTS    NAMES
g86370300af15     debian    "/bin/bash"    2 hours ago    Up 2 hours             first_debian

By default Above command will list only running containers. To list all containers (including stopped container) use the following command.

docker ps -a

Start/Stop/Attach Container

You can start, stop or attach to any containers with following commands. To start container use following command.

docker start <CONTAINER_ID>

To stop container use following command.

docker stop <CONTAINER_ID>

To attach to currently running container use following command.

docker attach <CONTAINER_ID>

Step 5 – Remove Docker

To remove docker from your Debian system run following command.

sudo apt purge docker-ce

The post How to Install Docker on Debian 10 (Buster) appeared first on TecAdmin.

]]>
https://tecadmin.net/install-docker-on-debian-10-buster/feed/ 0