📜  docker on linux - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:29.063000             🧑  作者: Mango

Docker on Linux - Shell/Bash

Introduction

Docker is an open-source containerization platform that allows developers to package their applications into small, portable containers that can be deployed anywhere.

In this tutorial, we will learn how to use Docker on Linux using Shell/Bash scripts to create, run, and manage containers.

Prerequisites

Before we begin, make sure that you have the following installed on your system:

  • Linux (Ubuntu, Debian, CentOS or any other distribution)
  • Docker
Getting Started

First, let's verify that Docker is installed and working properly. Open a terminal window and enter the following command:

docker version

This should display the version of the Docker client and server installed on your system.

Now, let's create a simple Docker container using a Shell/Bash script. First, create a new file called Dockerfile:

touch Dockerfile

Then, open it in a text editor and add the following content:

# Dockerfile

FROM ubuntu:latest

RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    libapache2-mod-php \
    && rm -rf /var/lib/apt/lists/*

COPY index.php /var/www/html/

EXPOSE 80

CMD ["apache2ctl", "-D", "FOREGROUND"]

This Dockerfile will create a container based on the latest version of Ubuntu and install Apache, PHP, and the necessary Apache modules. It will also copy an index.php file to the /var/www/html directory and expose port 80. Finally, it will start Apache using the apache2ctl command.

Save the file and then create the index.php file with the following content:

<?php

phpinfo();

?>

Now, let's build the Docker image using the following command:

docker build -t myapp .

This will use the Dockerfile to create a new image called myapp.

Next, let's run the Docker container using the following command:

docker run -d -p 80:80 myapp

This will start the container in detached mode and map port 80 of the container to port 80 of the host.

Finally, let's verify that the container is running by opening a web browser and navigating to the IP address or hostname of the Linux machine.

Conclusion

In this tutorial, we learned how to use Docker on Linux using Shell/Bash scripts to create, run, and manage containers. Docker is a powerful tool that can help you quickly and easily package and deploy your applications. With a little bit of Shell/Bash scripting, you can automate the entire process and streamline your development workflow.