Dockerfile – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Wed, 04 Jan 2023 06:51:37 +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
How to Dockerize a Flask Application https://tecadmin.net/how-to-create-and-run-a-flask-application-using-docker/ https://tecadmin.net/how-to-create-and-run-a-flask-application-using-docker/#respond Tue, 05 Jul 2022 05:27:29 +0000 https://tecadmin.net/?p=30363 In this tutorial, you will learn how to create a basic Flask app with Docker. You will set up your app with a Dockerfile, and manage the images with an automated build process. In this process, you’ll also learn how to use multiple Python virtual environments and keep your source code organized. If you’re new [...]

The post How to Dockerize a Flask Application appeared first on TecAdmin.

]]>
In this tutorial, you will learn how to create a basic Flask app with Docker. You will set up your app with a Dockerfile, and manage the images with an automated build process.

In this process, you’ll also learn how to use multiple Python virtual environments and keep your source code organized. If you’re new to Python or Flask, you may want to check out our beginner guide to Python as well as our beginner guide to Flask first. They cover the basics of these frameworks so that you can follow along better in this tutorial. Let’s get started!

What is Flask?

Flask is a lightweight Python framework for building web applications. It is simple, flexible, and pragmatic. It can be easily extended with the use of extensions and plug-ins. Flask can be used to create small websites or large-scale, complex applications.

Flask is used by companies like Pinterest, Twitter, Walmart, and Cisco. One of the common uses of Flask is for REST APIs that are used to interact with other systems. Applications written in Flask can also easily be deployed to a server or a cloud.

Create a Basic Flask Application

Before you can create a Docker image with your application, you have to have a basic app that you can run locally or on a server.

In this section, you will create a basic app with Flask and then run it in a Docker container. You can use your preferred editor to create the app, or you can use the following command in your terminal to create a new app:

  1. Let’s begin with creating a new directory:
    mkdir flask-app && flask-app 
    
  2. Next, create the Python virtual environment and then activate the environment.
    python3 -m venv venv 
    source venv/bin/activate 
    
  3. Now install the Flask python module under the virtual environment.
    pip install Flask 
    
  4. The below command will create the requirements.txt file with the installed packages under the current environment. This file is useful for installing module at deployments.
    pip freeze > requirements.txt 
    
  5. Now, create a sample Flask application.. You can write your code in a .py file and run it with the python command.
    vim app.py 
    

    Add the below snippt.

    # Import flask module
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        return 'Hello to Flask!'
    
    # main driver function
    if __name__ == "__main__":
        app.run()

  6. Your sample Flask application is ready. You can run this script with Python now.
    flask run --host 0.0.0.0 --port 5000 
    
    Running Flask App via CLI
    Running flask application at command line

Now that you have a basic app, you can run it in a Docker container by creating a file called Dockerfile in the same directory as your app.py file.

Create a Dockerfile for Your Flask Application

A Dockerfile is a file that contains instructions on how to build your image. It describes the entire process of building your image from the installation of your Python app to the creation of your container.

  1. Let’s create a file named Dockerfile under the project directory. This is the file docker reads instructions to build image.
    vim Dockerfile 
    

    Add the following code:

    FROM python:3-alpine
    
    # Create app directory
    WORKDIR /app
    
    # Install app dependencies
    COPY requirements.txt ./
    
    RUN pip install -r requirements.txt
    
    # Bundle app source
    COPY . .
    
    EXPOSE 5000
    CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]
    

    Save the file and close it.

  2. Next, create the Docker image by running the below-mentioned command. Here “flask-app” is the image name.
    docker build -t flask-app . 
    
  3. This image will be created in local image registry. Then you can create a Docker container with the following command.
    sudo docker run -it -p 5000:5000 -d flask-app  
    
  4. Now, verify that container is running on your system.
    docker containers ls  
    

    Dockrise Flask Application

  5. Finally, open a browser and connect to localhost on port 5000 (or use your own defined port). You should see the flask application.

    Dockrise Flask Application

You can use a Dockerfile to automate the building and updating of your image. This is helpful if you’re working with a team and people are adding code to the same application.

Conclusion

In this tutorial, you learned how to create a basic Flask app and build a Docker image with it. You also learned how to create a private registry and automate the building and updating of your image. This is a great way to create reproducible builds and container images that are easy to share and deploy. This can be helpful if you need to set up servers or if you want to run your app on different machines.

The post How to Dockerize a Flask Application appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-create-and-run-a-flask-application-using-docker/feed/ 0
How To Install Docker on Ubuntu 22.04 https://tecadmin.net/how-to-install-docker-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-docker-on-ubuntu-22-04/#respond Sun, 03 Jul 2022 04:10:51 +0000 https://tecadmin.net/?p=29096 Docker is a containerization technology that allows you to run applications in isolated environments. This means that each application can run without affecting the others on the same system. Docker is popular because it makes it easy to package and ship applications. It is also easy to set up a Docker environment on your own [...]

The post How To Install Docker on Ubuntu 22.04 appeared first on TecAdmin.

]]>
Docker is a containerization technology that allows you to run applications in isolated environments. This means that each application can run without affecting the others on the same system. Docker is popular because it makes it easy to package and ship applications. It is also easy to set up a Docker environment on your own computer.

In this guide, we will show you how to install Docker on an Ubuntu 22.04 server.

We will start by updating the list of available packages and installing Docker’s dependencies. We will then download Docker’s stable release from their official repository. Finally, we will configure Docker to start automatically at boot time.

Let’s get started!

Step 1 – Installing Docker’s Dependencies

Before we start installing Docker, we need to make sure that our server has the latest list of available packages. We can do this by running the `apt update` command:

sudo apt update 

Docker requires a few dependencies in order to run properly on Ubuntu. We can install all of the required dependencies with the following command:

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

Step 2 – Adding the Docker Repository

Next, we need to add Docker’s official GPG key so that our server can trust the packages that we will download from Docker’s repository. We can do this with the following curl command:

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

Now that we have Docker’s GPG key, we can add the Docker repository to our server. We can do this with the `add-apt-repository` command:

sudo add-apt-repository "deb https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" 

Step 3 – Installing Docker on Ubuntu

Now that we have added the Docker repository to our server, we need to update the package index one more time:

sudo apt update 

Now that the package index is up-to-date, we can install Docker with the following command:

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

Docker should now be installed on your Ubuntu server.

Step 4 – Configuring Docker to Start at Boot

By default, Docker is not configured to start automatically when the server boots. We can change this behavior with the `systemctl` command:

sudo systemctl enable docker 

Now, Docker will start automatically any time that our server reboots.

Conclusion

In this guide, we have shown you how to install Docker on an Ubuntu 22.04 server. We have also shown you how to configure Docker to start automatically at boot time. Now that Docker is up and running, you can start using it to run your applications in isolated environments.

The post How To Install Docker on Ubuntu 22.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-docker-on-ubuntu-22-04/feed/ 0
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
How to Add a Comments in Dockerfile https://tecadmin.net/add-comments-in-dockerfile/ https://tecadmin.net/add-comments-in-dockerfile/#respond Thu, 14 May 2020 17:07:03 +0000 https://tecadmin.net/?p=21166 Comments are the important parts of any programming language. Which is used to describe details about the application. In this tutorial you will learn about using comments in Dockerfile. The Docker treats each line as comment begin with hash (#) sign, unless the line is a valid directive. The # marker must be the first [...]

The post How to Add a Comments in Dockerfile appeared first on TecAdmin.

]]>
Comments are the important parts of any programming language. Which is used to describe details about the application. In this tutorial you will learn about using comments in Dockerfile.

The Docker treats each line as comment begin with hash (#) sign, unless the line is a valid directive. The # marker must be the first character of of line be make it comments, anywhere else in a line is treated as an argument. The commends can use used as:

# This is a comment line
RUN echo 'Welcome to tecadmin.net'

In the above example # is placed at the beginning of the line. Any where the # symbol in line is used as an arguments.

Add comment in dockerfile

The Dockerfile also uses a parser directive which is written on first line of the file and begin with a # symbol. That line is also not count as comment.

The post How to Add a Comments in Dockerfile appeared first on TecAdmin.

]]>
https://tecadmin.net/add-comments-in-dockerfile/feed/ 0