Docker – 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 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 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 Install Docker Compose on Ubuntu 20.04 https://tecadmin.net/how-to-install-docker-compose-on-ubuntu-20-04/ https://tecadmin.net/how-to-install-docker-compose-on-ubuntu-20-04/#comments Mon, 24 May 2021 09:19:36 +0000 https://tecadmin.net/?p=25280 Docker compose is an useful tool for managing multiple docker containers. It helps us to launch, update and build a group of docker containers with single commands. In case of multi container application, docker compose helped us to manage it easier. This tutorial helps you to install Docker compose on a Ubuntu 20.04 LTS Linux [...]

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

]]>
Docker compose is an useful tool for managing multiple docker containers. It helps us to launch, update and build a group of docker containers with single commands. In case of multi container application, docker compose helped us to manage it easier.

This tutorial helps you to install Docker compose on a Ubuntu 20.04 LTS Linux system.

Prerequisites

Install Docker Compose on Ubuntu

Docker compose binary is available on official Github release. Use curl command line tool to download docker compose binary and place under the PATH environment to make it available globally.

First, make sure you have latest curl binary installed on your system.

sudo apt update 
sudo apt install -y curl 

Now, confirm the latest docker-compsoe binary version on Github releases page. At the time of this writing this tutorial, the current stable version is 1.29.2.

The following command will download the docker-compose binary version 1.29.2 and save this file at /usr/local/bin/docker-compose.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64" -o /usr/local/bin/docker-compose 

Make the binary docker-compose file executable

chmod +x /usr/local/bin/docker-compose 

Verify the the docker-compose version by running the followign command.

docker-compose --version 

Output:

docker-compose version 1.29.2, build 5becea4c

Run a Docker Compose Example

At this stage Docker compose have been configured on your Ubuntu system.

  1. First of all, create a directory, which will contain all the files related to this example.
    mkdir ~/myapp && cd ~/myapp
    
  2. Now, create a html directory. We will map this directory as document root of web server running under the Docker container
    mkdir html
    
  3. Now create a index.html file under html file.
    echo "<h2>Hello TecAdmin</h2>" > html/index.html 
    
  4. Next, create the most important configuration file docker-compose.yml. This is the file contains all settings used by the docker-compose. Basis of this file docker-compose setup the images, network and containers etc.

    I will discuss this in parts for the better understanding. Edit file in a text editor:

    sudo nano docker-compose.yml 
    

    Add the following content to file.

    version: '3'
     
    services:
      web:
        image: "php:8.0-apache"
        restart: 'always'
        volumes:
          - ./html:/var/www/html
        ports:
          - '8080:80'
    

    The above content shows that we are using latest version of docker compose version 3.

    Next, we defined a service “web” under sevices section. This will launch a docker container using pre-build “php:8.0-apache” AMI. The “html” directory in current folder will be mounted to containers /var/www/html directory.

    Keep your file in editing mode.

  5. Next add a MySQL database container in docker compose file.

    version: '3'
     
    services:
      web:
        image: "php:8.0-apache"
        restart: 'always'
        volumes:
          - ./html:/var/www/html
        ports:
          - '8080:80'
        depends_on:
          - mysql
        links:
          - mysql
      mysql:
        image: mysql:8
        container_name: mysql
        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:
    

    You can change the MySQL versions and access credentials. We have also updated web container configuration to link with database. This will allow web container to access database directly.

    Keep your file in editing mode…

  6. Optional but helpful to add phpmyadmin docker container. This will will help you to access database and its users.

    After adding the phpmyadmin, your docker-compose.yml look like below:

    version: '3'
     
    services:
      web:
        image: "php:8.0-apache"
        restart: 'always'
        volumes:
          - ./html:/var/www/html
        ports:
          - '8080:80'
        depends_on:
          - mysql
        links:
          - mysql
    
      mysql:
        image: mysql:8
        container_name: mysql
        environment:
          MYSQL_ROOT_PASSWORD: my_secret_password
          MYSQL_DATABASE: mydb
          MYSQL_USER: dbuser
          MYSQL_PASSWORD: dbuser_password
        ports:
          - "6033:3306"
        volumes:
          - dbdata:/var/lib/mysql
    
      phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin
        links:
          - mysql
        environment:
          PMA_HOST: mysql
          PMA_PORT: 3306
          PMA_ARBITRARY: 1
        restart: always
        ports:
          - 8081:80
    
    volumes:
      dbdata:
    

    Save you file and close it.

  7. Next, run the following command to pull all the images on your system.

    sudo docker-composer pull 
    

  8. Next run the build command to create docker images. For this example, you can skip this command as we are using pre build images only.

    sudo docker-composer build
    
  9. finally, run the docker-compose up command. Use “-d” option to deamonize container to keep running.

    sudo docker-composer up -d 
    

  10. The web server container is listening on port 8080, which we have bind with containers port 80. Access your host machine ip address on port 8080. You will see the index.html created under html directory.


    To make change just update html directory content and refresh page.

Conclusion

This tutorial helped you to install and use docker-compose on Ubuntu systems. The above instructions can be used on Ubuntu 16.04 LTS and above systems.

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

]]>
https://tecadmin.net/how-to-install-docker-compose-on-ubuntu-20-04/feed/ 1
Docker-compose for MySQL with phpMyAdmin https://tecadmin.net/docker-compose-for-mysql-with-phpmyadmin/ https://tecadmin.net/docker-compose-for-mysql-with-phpmyadmin/#comments Fri, 03 Jul 2020 10:12:31 +0000 https://tecadmin.net/?p=21940 Docker-compose is an useful utility for managing multi-container docker applications. In our previous tutorial, I had discussed about the keep persistent data of MySQL docker containers using Docker volumes. Once you launched a MySQL container can be connect via terminal directly. But the phpMyAdmin lovers may need the web interface for managing databases. In this [...]

The post Docker-compose for MySQL with phpMyAdmin appeared first on TecAdmin.

]]>
Docker-compose is an useful utility for managing multi-container docker applications. In our previous tutorial, I had discussed about the keep persistent data of MySQL docker containers using Docker volumes. Once you launched a MySQL container can be connect via terminal directly. But the phpMyAdmin lovers may need the web interface for managing databases.

In this tutorial, you will learn to launch MySQL Docker containers along with phpMyAdmin docker container using docker-compose command.

Prerequisites

This guide assumes that you have already done the followings:

  1. You have installed Docker service on your System
  2. Also, have configured docker-compose utility on your system

How to Create MySQL with phpMyAdmin Docker Container

phpMyAdmin is an most popular web application for managing MySQL database servers. In this tutorial, we just use an example of Docker container for MySQL and phpMyAdmin.

So first create a docker-compose.yml 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
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: pma
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80
volumes:
  dbdata:

Save you file and close it.

Next, run the following command to create Docker containers using the docker-compose.yml configuration file.

docker-compose up -d

The above command will launch two Docker containers, one for MySQL database server and one for phpMyAdmin. Also a data volume will be created, which is attached with MySQL container to make data persistent.

Now, access the phpMyAdmin using the web browser. I am running this example on my local machine. So used host as localhost with port 8081 defined in docker compose configuration. You need to change localhost with your server ip address to access it remotely.

http://localhost:8081

Docker compose for mysql and phpmyadmin

Conclusion

In this tutorial, you have learned to launch a MySQL docker container with a phpMyAdmin. Which help help you to manage databases on MySQL docker container.

The post Docker-compose for MySQL with phpMyAdmin appeared first on TecAdmin.

]]>
https://tecadmin.net/docker-compose-for-mysql-with-phpmyadmin/feed/ 3
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