Docker – Dockerfile – Setup Environment

0
87

Docker – Dockerfile – Setup Environment. Previous post Docker – Getting Started – From History – I have introduced Docker and Containerization technology. This is a great tool, very helpful, it is recommended to learn and use.

Docker - Dockerfile - Setup Environment

Docker – Dockerfile – Setup Environment

What is Dockerfile? Simply, it is a file :D. It is in text format, without a file extension, helping to set the structure for docker image by containing a set of commands.

From those commands, Docker can perform packing a docker images according to your own customizable requirements.

Docker - Dockerfile

So Dockerfile will specify where the Docker image is created from, including what’s in it.

How to write Dockerfile?

I created an example for Dockerfile:

Docker - Dockerfile

First, I will create Dockerfile to contain image and container configuration.

Step 1: Setup image

First, we need to declare where this image originated, using: FROM. The original image may be centos: 7, ubuntu: 16.04…

FROM ubuntu:16.04

Docker Hub is a place to store and share images. You can develop, install, change, modify, add and remove to create your own custom images.

When Docker reads this command, it will automatically find the image ubuntu:16.04 that already exists in the device, otherwise Docker will automatically pull this image back. In that ubuntu is the name of the image, 16.04 is the tag.

Step 2: Install apps

Now, we will install the application, set the necessary environment on ubuntu:16.04. You can install nginx, php, python, ruby, java … depending on your needs, use:

  • RUN: To execute a certain command during build images.
  • CMD: To execute a command during container loading. Each Dockerfile has only one CMD statement, if there is more than one CMD statement, only the last CMD statement is used. One question is if I want to start multiple applications when the start container, then think about ENTRYPOINT.
  • ENTRYPOINT: To execute some commands during the start container, these statements will be written in the .sh file.

Example:

# Update ubuntu
RUN apt-get update

# Install nginx
RUN apt-get install -y nginx

# Install mysql server
RUN echo "mysql-server mysql-server/root_password password root" | debconf-set-selections \
    && echo "mysql-server mysql-server/root_password_again password root" | debconf-set-selections \
    && apt-get install -y mysql-server

While installing nginx, a question will appear and you need to answer yes / no, then the -y option in RUN apt-get install -y nginx will show your yes option.

Step 3: Configuration

  • EXPOSE: The Container will listen on the designated network ports when running
  • ADD: Copy files, folders, remote files to add them to the filesystem of the image.
  • COPY: Copy files and folders from host machine to image. Can use the url for the file to copy.
  • WORKDIR: Define the directory for CMD
  • VOLUME: Mount folders from host machines into containers.

Create an .sh file As mentioned above, even if you only need one command, you still use ENTRYPOINT, so that you can easily customize and develop later.

Create start.sh file as follows:

#!/bin/bash
service nginx start
exec $@

We have an example for Dockerfile as follows:

FROM ubuntu:16.04

RUN DEBIAN_FRONTEND=noninteractive

RUN apt-get update

RUN apt-get install -y nginx

RUN echo "mysql-server mysql-server/root_password password root" | debconf-set-selections \
    && echo "mysql-server mysql-server/root_password_again password root" | debconf-set-selections \
    && apt-get install -y mysql-server

WORKDIR /venv

COPY start.sh /venv

RUN chmod a+x /venv/*

ENTRYPOINT ["/venv/start.sh"]

EXPOSE 80

How to use Dockerfile?

Build docker image from Dockerfile:

sudo docker build -t <image_name> .

Docker - Dockerfile build

Create container from image.

sudo docker run -v <forder_in_computer>:<forder_in_container> -p <port_in_computer>:<port_in_container> -it <image_name> /bin/bash
  • -v: Show mount volume, data from directory from real machine can be accessed from virtual machine directory.
  • -p: Network port from the real machine to lead to the network port of the virtual machine running.
  • -t: Run the container and open the terminal with /bin/bash

For example, the default localhost of nginx:

sudo docker run -p 9000:80 -it ubuntu-nginx /bin/bash

Docker - Dockerfile run nginx

Example to project directory in real machine:

  • I created an index.html file in my test_docker project folder.
  • RUN docker
docker run -v ~/Sites/test_docker:/var/www/html -p 9000:80 -it ubuntu-nginx /bin/bash

In which ~/Site/test_docker is my project directory. Here is result:

Docker - Dockerfile run project test

This article has introduced Docker – Dockerfile – Setup Environment, as well as how to use Dockerfile. Thank you for reading

Leave a Reply

avatar