Start a Project

How To Host Website On Docker Container

A docker container is a standard unit of software that packages up code and all its dependencies so that the application can run quickly from one computing environment to another.

images

Host App/Website On Docker Container

First, We have to create a Dockerfile for building an image for our website hosting.

Copy the code and save it as a file name of Dockerfile with your file editor.

Secondly, create a supervisor configuration file.

Copy the code and save it as file-name of supervisord.conf with your file editor.

Note: The file “supervisord.conf” is required to start the web-server service in the container and we can use this file to start other services as well.

Now, We are building a docker image by using the above-created Dockerfile.

The above command builds an image with the name wp-image. You can list all your docker images by using the command “docker images”.

After building the image, We have to run the build image on the container.

Firstly, We run a MySQL container to store the database.

And in the above command, We run the container with a detached terminal by “-d”, name of “mysql-cont” by using “–name”

Create a database by using “ -e MYSQL_DATABASE=wordpress”

Configure password in mysql root user by “-e MYSQL_ROOT_PASSWORD=” and using mysql:5.7 image in which “mysql” is the image name and “:5.7” is the version tag.

Then, We run a container of our build image.

In this command, Everything is the same as have we run the previous command of MySQL container except “–link”, “-it” and “-p”.

Here, “-it” means instruct docker to give terminal interaction to the container.

For creating links between containers, We use “–link” to link the WordPress container to the MySQL container for sharing resources of the database container.

In porting while running a container we use “-p” to attach the external port to the container’s port.

8080 — refers base machine’s port.
80 — refers container’s port.

After all the above processes, you will be able to browse the website at http://localhost:8080.

Dockerfile

A docker file is a text file that includes all the commands a user can call on the command line to bring together an image.

Commands Use In Dockerfile Environment

FROMUses for fetching and using images from the base machine if present, otherwise, it will get it from the docker registry.

RUN For running commands in shell form over the image.

CMDUse to execute the command and It will be written in an executable set as [parameter-1, parameter-2].

MAINTAINERUse for maintaining Dockerfile author.

EXPOSEFor exposing the port of the image.

WORKDIRFor setting the working directory of the image.

ADDUse for copying our local machine data to docker image as well as use URL to copy and extract tar directory from source to destination.

COPY For copying our local machine’s data to the docker image.

envFor setting the environment in the docker image.

IP Allocation To Container/Assign Static IP To Docker Container

First, we have to create a network on docker.

And then, we run our container by using this network and assigning the static IP.

Communication between docker containers

A bridge network is an easy and default way to communicate between containers. Containers which is under the same bridge network will able to communicate by name or alias.

By using the above command, you will able to list the docker network which includes Network ID, Network Name, Driver, and Scope.

Note: The container’s communication is based on IP within the same network of the bridge and also by name or alias of the container.

This is the default way to communicate or connect between containers within the default bridge.

User Defined Docker Network

In the first command, We are creating a network in the docker network as default with a bridge network using the name “Bnet”.

In the second and third commands, We are creating a container under the “Bnet” network.

The container of php:8.0-apache and wp-image will communicate because of containers within a user-defined bridge network.

Docker Volume

Let’s create a docker volume for persisting container data. So, We are going to create a volume to save MySQL data.

We are going to use the created volume to save data while starting a container of the “mysql:5.7” image.

After using the persisting volume “-v mysql-data:/var/lib/mysql”, the Data of MySQL will be saved in “mysql-data” docker volume.

If you are not using the attribute “-v” to create a docker volume, Docker will create a volume for your container.

Mount Directory To Docker Container

By default, docker will create volume when you run the container by using an attribute with “-v xyz:/path”. Here “xyz” is the volume of the docker created during the docker run with “-v” attribute.

If you use “-v /path-of-local-directory:/path-of-container-directory” then it mounts and persists data on the base machine’s directory to the container’s directory.

You can display a list of docker volumes by the following command :

If you want docker volume details then you will display them by inspecting that volume using the following command :

Why do we need persistent volume?

We need persistent volume to protect the data loss if the containers get destroyed or crash.

Therefore, the data will remain safe in the base machine even if the container gets destroyed because of the persisting volume and mount directory.

You may also visit our Magento development services and quality  Magento 2 Extensions.

For further help or queries, please contact us or raise a ticket.

Exit mobile version