Indonesia Website Awards
Indonesia Website Awards

In my post this time, I will discuss how to install nginx and certbot on docker. Why do I use docker for nginx and certbot installations because in my opinion using Docker the process is faster, setup is easy and simple. We can also save the image we created in our docker hub. Before we start, make sure Docker is installed where you are. To install docker you can read the docker docs – Install docker engine .

Okay, if you have docker installed at your place, we will continue to our main topic, here are the steps:

Step 1 – Create a Network Docker

The first is, we create a docker network first. The function of this network is to act as a liaison between the Docker containers later. the following commands:

docker network create -d bridge networkName

 

Step 2 – Create a shared directory

My suggestion, make a shared directory to store data related to docker, for example, I created a directory called myDocker in the root like this is located /root/myDocker.

mkdir /root/myDocker

 

Step 3 – Create an nginx directory and create an nginx.conf file in it

Next, we create a special directory for the nginx container and create an nginx.conf file in it, its function is, so that later we can easily modify the nginx.conf file.

mkdir /root/myDocker/nginx

vim /root/myDocker/nginx/nginx.conf

Enter this configuration into the nginx.conf file :

user nginx;

worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
        worker_connections 1024;
}

http {
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
        access_log /var/log/nginx/access.log main;
        sendfile on;
        #tcp_nopush on;
        keepalive_timeout 65;
        #gzip on;
        include /etc/nginx/conf.d/*.conf;
}

This is just an example of the nginx.conf file that you can use, you can modify it according to your needs.

Step 3 – Create a conf.d directory and create a default.conf configuration file

Next, we create a conf.d directory in the nginx directory that we created in step 2, then we also create a default.conf configuration file. later we can manage the nginx configuration in this conf.d.

mkdir /root/myDocker/nginx/conf.d

vim /root/myDocker/nginx/conf.d/default.conf

Enter this configuration into the default.conf file :

server {
	listen 80;
   	server_name yourIpAddress;

	root   /var/www;

	location / {
		index index.html;
   	}
}

This is just an example of a simple configuration, you can modify it according to your needs.

Step 4 – Create a docker container for nginx

Next we create a docker container for nginx by running :

docker run -itd --name containerName \
 --network networkName \
 --publish 80:80 \
 --publish 443:443 \
 --volume ~/myDocker/nginx/nginx.conf:/etc/nginx/nginx.conf \
 --volume ~/myDocker/nginx/conf.d:/etc/nginx/conf.d \
 --volume /var/www:/var/www \
 nginx:1.18-alpine

Explanation :

  • docker run -itd --name containerName –itd to run docker with interactive and auto-start when the pc / server reboots, –name containerName to give the container name.
  • --network networkName container docker will run on the network that we created in step 1.
  • --publish 80:80 and --publish 443:443 to determine the port usage between our PC / server and the port images installed in the container. here we can customize, for example, the default port nginx running on port 80 we want to run on port 8080 then we just change it to  --publish 8080:80 .
  • --volume ~/myDocker/nginx/nginx.conf:/etc/nginx/nginx.conf and other –volume functions to synchronize directories / files between our server and the directory in the docker container.
  • nginx:1.18-alpine is the nginx image that I use and I install in the docker container.

Yes, until now we have successfully installed Nginx in docker, you can check it by opening a browser and accessing the server_name or ip that you configured in default.conf.

Step 5 – Create a Dockerfile to install certbot

Then in the future, we will add certbot to the nginx docker that we have created, we will use Dockerfile. for example, create a Dockerfile in /root/myDocker/ :

cd /root/myDocker
vim Dockerfile

#Masukkan ini kedalam Dockerfile
FROM nginx:1.18-alpine
RUN apk add certbot certbot-nginx 
RUN mkdir /etc/letsencrypt
Step 6 – Build a Dockerfile

Next we run the Dockerfile with the docker build.

docker build -t yourDockerUser/imageName:tagName .

This process will create new Docker images (which already have certbot installed) which we can adjust the naming, for the example I made like this:

docker build -t lukman134/nginx-certbot .

If we don’t include tagName, then docker will read it as an image with the latest version.

Step 7 – Delete the container that was created in step 1, then recreate it using the build image from Dockerfile

After successfully building Dockerfile, we can use the image, because the container we created in step 1 is no longer useful (because there is no certbot yet) then we delete the container with the command docker rm -f containerName then we re-create the container by running this command :

docker run -itd --name containerName \
 --network networkName \
 --publish 80:80 \
 --publish 443:443 \
 --volume ~/myDocker/nginx/nginx.conf:/etc/nginx/nginx.conf \
 --volume ~/myDocker/nginx/conf.d:/etc/nginx/conf.d \
 --volume /var/www:/var/www \
 --volume ~/myDocker/nginx/ssl:/etc/ssl \
 --volume ~/myDocker/certbot/conf:/etc/letsencrypt \
 yourDockerUser/imageName:tagName

When finished and there are no errors, it means that you have successfully installed Nginx and certbot in docker. Then to use certbot you can run it with the command :

docker exec -ti containerName certbot [command]

Yes, those are the steps on how to install nginx and certbot on docker that I can share. If you have anything to ask, please ask in the comments column. Hopefully this article can be useful, Thank you 😀

 

Also Read :

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *