Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running Nginx, a popular web server and reverse proxy server. We'll use the official Nginx image provided by the Docker community.
Prerequisites:
Pull the official Nginx image:
To pull the official Nginx image, run the following command:
docker pull nginx
This will download the latest version of the Nginx image to your system.
Create a new container with Nginx:
To create a new Docker container running Nginx, use the docker run
command. We'll map the container's port 80 (the default Nginx HTTP port) to the host's port 80, so you can access the Nginx server from your host machine:
docker run --name nginx-container -p 80:80 -d nginx
This command creates a new container named nginx-container
, maps the container's port 80 to the host's port 80, and runs the container in detached mode (-d
flag). The container will run the Nginx server using the default configuration.
Verify the Nginx container is running:
To check if the Nginx container is running, use the docker ps
command:
docker ps
You should see your nginx-container
listed as running.
Access the Nginx server:
Open your browser and navigate to http://localhost
. You should see the Nginx default welcome page.
Customize the Nginx configuration (Optional):
If you want to customize the Nginx configuration, you can create a local configuration file (e.g., nginx.conf
) and mount it as a volume inside the container. Additionally, you can mount a directory with your website files as a volume.
First, create a new directory for your configuration files and website files:
mkdir nginx-config mkdir website
Next, create a new nginx.conf
file in the nginx-config
directory and customize it as needed. You can use the default nginx.conf
file from the Nginx image as a starting point. To obtain the default configuration file, you can run the following command:
docker cp nginx-container:/etc/nginx/nginx.conf nginx-config/nginx.conf
This command copies the default nginx.conf
file from the running nginx-container
to your local nginx-config
directory.
Now, edit the nginx-config/nginx.conf
file using your preferred text editor, and apply your desired changes. Update the configuration file to point to the /website
directory inside the container for serving your website files.
After customizing the configuration file, stop and remove the existing nginx-container
:
docker stop nginx-container docker rm nginx-container
Finally, create a new container with the customized configuration, mounting the local nginx-config
directory and website
directory as volumes inside the container:
docker run --name custom-nginx-container -p 80:80 -v $(pwd)/nginx-config/nginx.conf:/etc/nginx/nginx.conf -v $(pwd)/website:/website -d nginx
This command creates a new container named custom-nginx-container
, maps the container's port 80 to the host's port 80, mounts the local nginx-config/nginx.conf
file as a volume at /etc/nginx/nginx.conf
and the website
directory as a volume at /website
.
Running Nginx in Docker Container:
docker run -d -p 80:80 --name my-nginx-container nginx:latest
How to Create a Docker Image with Nginx:
FROM nginx:latest # Add custom configurations or content
Docker-Compose Nginx Installation:
version: '3' services: nginx: image: nginx:latest ports: - "80:80"
Nginx Dockerfile Example:
FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf
Setting Up Nginx in a Docker Environment:
version: '3' services: nginx: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf
Configuring Nginx in Docker-Compose:
version: '3' services: nginx: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./html:/usr/share/nginx/html
Docker Nginx Image Customization:
FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf COPY ssl.crt /etc/nginx/ssl.crt COPY ssl.key /etc/nginx/ssl.key
Docker Nginx Volume Mounts:
version: '3' services: nginx: image: nginx:latest ports: - "80:80" volumes: - ./html:/usr/share/nginx/html - ./ssl:/etc/nginx/ssl
Custom Nginx Image Creation in Docker:
FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf COPY custom-content /usr/share/nginx/html
Docker Swarm and Nginx Service:
version: '3' services: nginx: image: nginx:latest deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting Nginx in Docker:
docker logs <nginx_container_id_or_name>