Create a container with Nginx Docker

Introduction

Example where on the host Ubuntu for Windows some commands may differ slightly

It is useful to read about Nginx at https://hub.docker.com/_/nginx

nginx:alpine

docker pull nginx:alpine

If the image has not been downloaded yet - the download will begin. On this host it is already there

alpine: Pulling from library/nginx Digest: sha256:a74534e76ee1121d418fa7394ca930eb67440deda413848bc67c68138535b989 Status: Image is up to date for nginx:alpine docker.io/library/nginx:alpine

➭ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE nginx alpine b1c3acb28882 13 days ago 23.4MB

Let's start the container with the command run

docker run --name andrei-nginx -p 8080:80 -d nginx:alpine

30433f8ff2738593f44b9e95c243dd521d3d2effb879fc0126e4f0d88a2c08b8

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30433f8ff273 nginx:alpine "/docker-entrypoint.…" 3 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp andrei-nginx

In the browser, open

http://127.0.0.1:8080/

lynx localhost:8080

Welcome to nginx! Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx.

-v: Mounting a Volume

You can immediately put your own html page instead of the standard one using a volume

mkdir nginx
cd nginx
touch index.html
vi index.html

Let's create the simplest page, for example about bicycles

<html> <body> <h1>TopBicycle.ru</h1> <p> <a href="https://www.topbicycle.ru/m/russian/"> Russian Bikes </a> <br><br> <a href="https://www.topbicycle.ru/m/italian/"> Italian Bikes </a> </p> </body> </html>

Let's make sure the container is removed and start it again, but with the -v option that specifies which directory on the host should be substituted for the directory from the image.

docker rm andrei-nginx
docker run --name andrei-nginx -p 8080:80 -v $(pwd):/usr/share/nginx/html nginx:alpine

lynx localhost:8080

TopBicycle.ru Russian Bikes Italian Bikes

Second example

mkdir docker_lab
cd docker_lab

Basics

Let's download the image with Nginx

sudo docker pull nginx

sudo docker pull nginx Using default tag: latest latest: Pulling from library/nginx 7d63c13d9b9b: Pull complete 15641ef02d80: Pull complete 392f7fc24052: Pull complete 8765c7b04ad8: Pull complete 8ddfvc52b5c7: Pull complete 353f1014328a: Pull complete Digest: sha256:dfef797cyddfc01645503cef9036379f03ae920cac82d344d58b637ee861fda1 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest

Let's make sure the image appeared in the list

sudo docker images

REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 04661cdce581 13 hours ago 141MB nginx_nginx latest 915d22c6f9c6 21 hours ago 219MB nginxinc/nginx-unprivileged 1.21.0 d30c9c6fe1a0 5 months ago 133MB

Let's create an nginx folder and go into it

mkdir nginx
cd nginx

Let's create a file with ssh settings sshd_config and start editing it

touch sshd_config
nano sshd_config

Make a small config, for example like this:

Port 22 Protocol 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecda_key HostKey /etc/ssh/ssh_host_ed25519_key LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes UsePAM yes

Create a file with Docker settings - Dockerfile and edit it

touch Dockerfile
nano Dockerfile

FROM centos RUN yum -y update RUN yum install -y openssh-server RUN ssh-keygen -A ADD ./sshd_config /etc/ssh/sshd_config RUN echo root:Secret123 | chpasswd CMD /usr/sbin/sshd -D

Build the image

docker build -t andrei-ubuntu/centos_ssh .

-t is short for --tag, sets the name and tag in the 'name:tag' format

the tag can be omitted - it is optional

. means - in the current directory

Now run it

docker run -it -d --name node1 -p 2201:22 andrei-ubuntu/centos_ssh

Make sure its status is Up

docker ps -a

If you do not know what IP you are on, run

ip a

Connect over SSH

ssh root@192.168.0.100 -p 2201

Article author: Andrei Olegovich

Banner image