Containerize and run Nginx with Docker

When developing native apps such as iOS and Android apps, and implementing a process that communicates with a web server via HTTP/HTTPS, there are many times when you want fixed data to be returned.

For example, when unit testing communication processes in XCTest. During implementation and debugging, there are many cases where communication over HTTP/HTTPS is all that is required.

In such cases, Docker is useful. You can containerize Nginx in Docker and communicate with it while working on the implementation.

TOC

How to containerize and run

For light use, such as a small amount of communication in the implementation of an application, the following minimal steps are sufficient. While using nginx, it is sufficient to add nginx configuration files as needed.

Creating a Docker Image

Create a file named Dockerfile and enter the following code.

FROM nginx:1.23

About Tags

The 1.23 is a version number, but the meaning in the Dockerfile is a tag. nginx tags version 1.23 with the name 1.23.

You can check the official image tags at dockerhub. nginx can be found at the following link.

Definition of Composer

Create a file named docker-compose.yml and enter the following code.

version: "3"
services:
    nginx:
        build: .
        ports:
            - 8080:80
        volumes:
            - ./content_home:/usr/share/nginx/html

Service Definition

This file defines a service named nginx.

Image Designation

The nginx Docker image is build: . so we specify to build based on the Dockerfile in the same folder as the docker-compose.yml file.

Port number

The port number of nginx communication is specified by ports. The 80 port inside the container is connected to the external 8080 port. When connecting from outside the container, if you connect to port 8080, you will be connected to port 80 inside.

Directory Designation

The volumes maps the /usr/share/nginx/html directory in nginx to the local directory content_home. Although it is a folder that has not yet been created, accessing /usr/share/nginx/html from within the container will access the content_home directory in the local file system.

Creating files to be placed on the Web server

Create the directory specified with volumes. content_home folder is created in the same directory as the Dockerfile and docker-compose.yml. content_home directory becomes the root directory of the web server. Place the files you want to place on the web server in the content_home directory.

For example, create a content_home/index.html file with the following contents.

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8" />
	<title>Simple HTML</title>
</head>
<body>
    <p>Simple HTML in the Docker container.</p>
</body>
</html>

Running Containers

In a terminal, go to the directory where docker-compose.yml is placed and run docker-compose as follows.

% docker-compose up -d

docker-compose up reads docker-compose.yml and runs the service. It is started as a daemon with the -d option.

Connection test

On the machine running Docker, open the following URL from a web browser.

http://localhost:8080/

If successful, the contents of index.html will be displayed.

Connecting from a Web Browser
Connecting from a Web Browser

Stopping the Container

In a terminal, go to the directory where docker-compose.yml is placed and run docker-compose as follows.

% docker-compose down

docker-compose down stops the service.

Conclusion

Docker makes it easier to prepare a test server than creating a virtual PC with Parallels Desktop, VMware, or Virtual Box.

It is also very convenient to be able to manage all the files in Git at once, including definition files and files to be deployed.

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC